#7: datastore convenience functions.
This commit is contained in:
parent
b50084c7a6
commit
cb383d84e7
3 changed files with 76 additions and 136 deletions
|
@ -4,23 +4,33 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jmoiron/modl"
|
||||||
"github.com/thermokarst/bactdb/models"
|
"github.com/thermokarst/bactdb/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGeneraStore_Get_db(t *testing.T) {
|
func insertGenus(t *testing.T, tx *modl.Transaction) *models.Genus {
|
||||||
want := &models.Genus{Id: 1, GenusName: "Test Genus"}
|
// Test on a clean database
|
||||||
|
tx.Exec(`DELETE FROM genera;`)
|
||||||
|
|
||||||
|
genus := newGenus()
|
||||||
|
if err := tx.Insert(genus); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return genus
|
||||||
|
}
|
||||||
|
|
||||||
|
func newGenus() *models.Genus {
|
||||||
|
return &models.Genus{GenusName: "Test Genus"}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGeneraStore_Get_db(t *testing.T) {
|
||||||
tx, _ := DB.Begin()
|
tx, _ := DB.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
// Test on a clean database
|
want := insertGenus(t, tx)
|
||||||
tx.Exec(`DELETE FROM genera;`)
|
|
||||||
if err := tx.Insert(want); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
d := NewDatastore(tx)
|
d := NewDatastore(tx)
|
||||||
genus, err := d.Genera.Get(1)
|
genus, err := d.Genera.Get(want.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -32,13 +42,10 @@ func TestGeneraStore_Get_db(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGeneraStore_Create_db(t *testing.T) {
|
func TestGeneraStore_Create_db(t *testing.T) {
|
||||||
genus := &models.Genus{Id: 1, GenusName: "Test Genus"}
|
|
||||||
|
|
||||||
tx, _ := DB.Begin()
|
tx, _ := DB.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
// Test on a clean database
|
genus := newGenus()
|
||||||
tx.Exec(`DELETE FROM genera;`)
|
|
||||||
|
|
||||||
d := NewDatastore(tx)
|
d := NewDatastore(tx)
|
||||||
created, err := d.Genera.Create(genus)
|
created, err := d.Genera.Create(genus)
|
||||||
|
@ -55,16 +62,11 @@ func TestGeneraStore_Create_db(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGeneraStore_List_db(t *testing.T) {
|
func TestGeneraStore_List_db(t *testing.T) {
|
||||||
want := []*models.Genus{{Id: 1, GenusName: "Test Genus"}}
|
|
||||||
|
|
||||||
tx, _ := DB.Begin()
|
tx, _ := DB.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
// Test on a clean database
|
genus := insertGenus(t, tx)
|
||||||
tx.Exec(`DELETE FROM genera;`)
|
want := []*models.Genus{genus}
|
||||||
if err := tx.Insert(want[0]); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
d := NewDatastore(tx)
|
d := NewDatastore(tx)
|
||||||
genera, err := d.Genera.List(&models.GenusListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}})
|
genera, err := d.Genera.List(&models.GenusListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}})
|
||||||
|
@ -84,19 +86,9 @@ func TestGeneraStore_Update_db(t *testing.T) {
|
||||||
tx, _ := DB.Begin()
|
tx, _ := DB.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
// Test on a clean database
|
genus := insertGenus(t, tx)
|
||||||
tx.Exec(`DELETE FROM genera;`)
|
|
||||||
|
|
||||||
d := NewDatastore(nil)
|
d := NewDatastore(tx)
|
||||||
// Add a new record
|
|
||||||
genus := &models.Genus{GenusName: "Test Genus"}
|
|
||||||
created, err := d.Genera.Create(genus)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !created {
|
|
||||||
t.Error("!created")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tweak it
|
// Tweak it
|
||||||
genus.GenusName = "Updated Genus"
|
genus.GenusName = "Updated Genus"
|
||||||
|
@ -114,19 +106,9 @@ func TestGeneraStore_Delete_db(t *testing.T) {
|
||||||
tx, _ := DB.Begin()
|
tx, _ := DB.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
// Test on a clean database
|
genus := insertGenus(t, tx)
|
||||||
tx.Exec(`DELETE FROM genera;`)
|
|
||||||
|
|
||||||
d := NewDatastore(tx)
|
d := NewDatastore(tx)
|
||||||
// Add a new record
|
|
||||||
genus := &models.Genus{GenusName: "Test Genus"}
|
|
||||||
created, err := d.Genera.Create(genus)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !created {
|
|
||||||
t.Error("!created")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete it
|
// Delete it
|
||||||
deleted, err := d.Genera.Delete(genus.Id)
|
deleted, err := d.Genera.Delete(genus.Id)
|
||||||
|
|
|
@ -4,29 +4,35 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jmoiron/modl"
|
||||||
"github.com/thermokarst/bactdb/models"
|
"github.com/thermokarst/bactdb/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func insertSpecies(t *testing.T, tx *modl.Transaction) *models.Species {
|
||||||
|
// clean up our target table
|
||||||
|
tx.Exec(`DELETE FROM species;`)
|
||||||
|
species := newSpecies(t, tx)
|
||||||
|
if err := tx.Insert(species); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return species
|
||||||
|
}
|
||||||
|
|
||||||
|
func newSpecies(t *testing.T, tx *modl.Transaction) *models.Species {
|
||||||
|
// we want to create and insert a genus record, too
|
||||||
|
genus := insertGenus(t, tx)
|
||||||
|
return &models.Species{GenusId: genus.Id, SpeciesName: "Test Species"}
|
||||||
|
}
|
||||||
|
|
||||||
func TestSpeciesStore_Get_db(t *testing.T) {
|
func TestSpeciesStore_Get_db(t *testing.T) {
|
||||||
tx, _ := DB.Begin()
|
tx, _ := DB.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
// Test on a clean database
|
want := insertSpecies(t, tx)
|
||||||
tx.Exec(`DELETE FROM species;`)
|
|
||||||
tx.Exec(`DELETE FROM genera;`)
|
|
||||||
|
|
||||||
wantGenus := &models.Genus{GenusName: "Test Genus"}
|
|
||||||
if err := tx.Insert(wantGenus); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
want := &models.Species{Id: 1, GenusId: wantGenus.Id, SpeciesName: "Test Species"}
|
|
||||||
if err := tx.Insert(want); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
d := NewDatastore(tx)
|
d := NewDatastore(tx)
|
||||||
species, err := d.Species.Get(1)
|
|
||||||
|
species, err := d.Species.Get(want.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -41,23 +47,14 @@ func TestSpeciesStore_Create_db(t *testing.T) {
|
||||||
tx, _ := DB.Begin()
|
tx, _ := DB.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
// Test on a clean database
|
species := newSpecies(t, tx)
|
||||||
tx.Exec(`DELETE FROM species;`)
|
|
||||||
tx.Exec(`DELETE FROM genera;`)
|
|
||||||
|
|
||||||
genus := &models.Genus{}
|
|
||||||
if err := tx.Insert(genus); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
species := &models.Species{Id: 1, GenusId: genus.Id, SpeciesName: "Test Species"}
|
|
||||||
|
|
||||||
d := NewDatastore(tx)
|
d := NewDatastore(tx)
|
||||||
|
|
||||||
created, err := d.Species.Create(species)
|
created, err := d.Species.Create(species)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !created {
|
if !created {
|
||||||
t.Error("!created")
|
t.Error("!created")
|
||||||
}
|
}
|
||||||
|
@ -70,23 +67,11 @@ func TestSpeciesStore_List_db(t *testing.T) {
|
||||||
tx, _ := DB.Begin()
|
tx, _ := DB.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
// Test on a clean database
|
want_species := insertSpecies(t, tx)
|
||||||
tx.Exec(`DELETE FROM species;`)
|
want := []*models.Species{want_species}
|
||||||
tx.Exec(`DELETE FROM genera;`)
|
|
||||||
|
|
||||||
genus := &models.Genus{}
|
|
||||||
|
|
||||||
if err := tx.Insert(genus); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
want := []*models.Species{{Id: 1, GenusId: genus.Id, SpeciesName: "Test Species"}}
|
|
||||||
|
|
||||||
if err := tx.Insert(want[0]); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
d := NewDatastore(tx)
|
d := NewDatastore(tx)
|
||||||
|
|
||||||
species, err := d.Species.List(&models.SpeciesListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}})
|
species, err := d.Species.List(&models.SpeciesListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -104,25 +89,9 @@ func TestSpeciesStore_Update_db(t *testing.T) {
|
||||||
tx, _ := DB.Begin()
|
tx, _ := DB.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
// Test on a clean database
|
species := insertSpecies(t, tx)
|
||||||
tx.Exec(`DELETE FROM species;`)
|
|
||||||
tx.Exec(`DELETE FROM genera;`)
|
|
||||||
|
|
||||||
d := NewDatastore(nil)
|
d := NewDatastore(tx)
|
||||||
// Add a new record
|
|
||||||
genus := &models.Genus{GenusName: "Test Genus"}
|
|
||||||
_, err := d.Genera.Create(genus)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
species := &models.Species{GenusId: genus.Id, SpeciesName: "Test Species"}
|
|
||||||
created, err := d.Species.Create(species)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !created {
|
|
||||||
t.Error("!created")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tweak it
|
// Tweak it
|
||||||
species.SpeciesName = "Updated Species"
|
species.SpeciesName = "Updated Species"
|
||||||
|
@ -140,25 +109,9 @@ func TestSpeciesStore_Delete_db(t *testing.T) {
|
||||||
tx, _ := DB.Begin()
|
tx, _ := DB.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
// Test on a clean database
|
species := insertSpecies(t, tx)
|
||||||
tx.Exec(`DELETE FROM species;`)
|
|
||||||
tx.Exec(`DELETE FROM genera;`)
|
|
||||||
|
|
||||||
d := NewDatastore(tx)
|
d := NewDatastore(tx)
|
||||||
// Add a new record
|
|
||||||
genus := &models.Genus{GenusName: "Test Genus"}
|
|
||||||
_, err := d.Genera.Create(genus)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
species := &models.Species{GenusId: genus.Id, SpeciesName: "Test Species"}
|
|
||||||
created, err := d.Species.Create(species)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
if !created {
|
|
||||||
t.Error("!created")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete it
|
// Delete it
|
||||||
deleted, err := d.Species.Delete(species.Id)
|
deleted, err := d.Species.Delete(species.Id)
|
||||||
|
|
|
@ -4,23 +4,34 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jmoiron/modl"
|
||||||
"github.com/thermokarst/bactdb/models"
|
"github.com/thermokarst/bactdb/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUsersStore_Get_db(t *testing.T) {
|
func insertUser(t *testing.T, tx *modl.Transaction) *models.User {
|
||||||
want := &models.User{Id: 1, UserName: "Test User"}
|
// Test on a clean database
|
||||||
|
tx.Exec(`DELETE FROM users;`)
|
||||||
|
|
||||||
|
user := newUser()
|
||||||
|
if err := tx.Insert(user); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return user
|
||||||
|
}
|
||||||
|
|
||||||
|
func newUser() *models.User {
|
||||||
|
return &models.User{UserName: "Test User"}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUsersStore_Get_db(t *testing.T) {
|
||||||
tx, _ := DB.Begin()
|
tx, _ := DB.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
// Test on a clean database
|
want := insertUser(t, tx)
|
||||||
tx.Exec(`DELETE FROM users;`)
|
|
||||||
if err := tx.Insert(want); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
d := NewDatastore(tx)
|
d := NewDatastore(tx)
|
||||||
user, err := d.Users.Get(1)
|
|
||||||
|
user, err := d.Users.Get(want.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -32,15 +43,13 @@ func TestUsersStore_Get_db(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUsersStore_Create_db(t *testing.T) {
|
func TestUsersStore_Create_db(t *testing.T) {
|
||||||
user := &models.User{Id: 1, UserName: "Test User"}
|
|
||||||
|
|
||||||
tx, _ := DB.Begin()
|
tx, _ := DB.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
// Test on a clean database
|
user := newUser()
|
||||||
tx.Exec(`DELETE FROM users;`)
|
|
||||||
|
|
||||||
d := NewDatastore(tx)
|
d := NewDatastore(tx)
|
||||||
|
|
||||||
created, err := d.Users.Create(user)
|
created, err := d.Users.Create(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -55,18 +64,14 @@ func TestUsersStore_Create_db(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUsersStore_List_db(t *testing.T) {
|
func TestUsersStore_List_db(t *testing.T) {
|
||||||
want := []*models.User{{Id: 1, UserName: "Test User"}}
|
|
||||||
|
|
||||||
tx, _ := DB.Begin()
|
tx, _ := DB.Begin()
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
// Test on a clean database
|
user := insertUser(t, tx)
|
||||||
tx.Exec(`DELETE FROM users;`)
|
want := []*models.User{user}
|
||||||
if err := tx.Insert(want[0]); err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
d := NewDatastore(tx)
|
d := NewDatastore(tx)
|
||||||
|
|
||||||
users, err := d.Users.List(&models.UserListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}})
|
users, err := d.Users.List(&models.UserListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
Reference in a new issue