#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"
|
||||
"testing"
|
||||
|
||||
"github.com/jmoiron/modl"
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func TestGeneraStore_Get_db(t *testing.T) {
|
||||
want := &models.Genus{Id: 1, GenusName: "Test Genus"}
|
||||
func insertGenus(t *testing.T, tx *modl.Transaction) *models.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()
|
||||
defer tx.Rollback()
|
||||
|
||||
// Test on a clean database
|
||||
tx.Exec(`DELETE FROM genera;`)
|
||||
if err := tx.Insert(want); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := insertGenus(t, tx)
|
||||
|
||||
d := NewDatastore(tx)
|
||||
genus, err := d.Genera.Get(1)
|
||||
genus, err := d.Genera.Get(want.Id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -32,13 +42,10 @@ func TestGeneraStore_Get_db(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGeneraStore_Create_db(t *testing.T) {
|
||||
genus := &models.Genus{Id: 1, GenusName: "Test Genus"}
|
||||
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
// Test on a clean database
|
||||
tx.Exec(`DELETE FROM genera;`)
|
||||
genus := newGenus()
|
||||
|
||||
d := NewDatastore(tx)
|
||||
created, err := d.Genera.Create(genus)
|
||||
|
@ -55,16 +62,11 @@ func TestGeneraStore_Create_db(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGeneraStore_List_db(t *testing.T) {
|
||||
want := []*models.Genus{{Id: 1, GenusName: "Test Genus"}}
|
||||
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
// Test on a clean database
|
||||
tx.Exec(`DELETE FROM genera;`)
|
||||
if err := tx.Insert(want[0]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
genus := insertGenus(t, tx)
|
||||
want := []*models.Genus{genus}
|
||||
|
||||
d := NewDatastore(tx)
|
||||
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()
|
||||
defer tx.Rollback()
|
||||
|
||||
// Test on a clean database
|
||||
tx.Exec(`DELETE FROM genera;`)
|
||||
genus := insertGenus(t, tx)
|
||||
|
||||
d := NewDatastore(nil)
|
||||
// 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")
|
||||
}
|
||||
d := NewDatastore(tx)
|
||||
|
||||
// Tweak it
|
||||
genus.GenusName = "Updated Genus"
|
||||
|
@ -114,19 +106,9 @@ func TestGeneraStore_Delete_db(t *testing.T) {
|
|||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
// Test on a clean database
|
||||
tx.Exec(`DELETE FROM genera;`)
|
||||
genus := insertGenus(t, 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
|
||||
deleted, err := d.Genera.Delete(genus.Id)
|
||||
|
|
|
@ -4,29 +4,35 @@ import (
|
|||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/jmoiron/modl"
|
||||
"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) {
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
// Test on a clean database
|
||||
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)
|
||||
}
|
||||
want := insertSpecies(t, tx)
|
||||
|
||||
d := NewDatastore(tx)
|
||||
species, err := d.Species.Get(1)
|
||||
|
||||
species, err := d.Species.Get(want.Id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -41,23 +47,14 @@ func TestSpeciesStore_Create_db(t *testing.T) {
|
|||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
// Test on a clean database
|
||||
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"}
|
||||
species := newSpecies(t, tx)
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
created, err := d.Species.Create(species)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !created {
|
||||
t.Error("!created")
|
||||
}
|
||||
|
@ -70,23 +67,11 @@ func TestSpeciesStore_List_db(t *testing.T) {
|
|||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
// Test on a clean database
|
||||
tx.Exec(`DELETE FROM 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)
|
||||
}
|
||||
want_species := insertSpecies(t, tx)
|
||||
want := []*models.Species{want_species}
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
species, err := d.Species.List(&models.SpeciesListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -104,25 +89,9 @@ func TestSpeciesStore_Update_db(t *testing.T) {
|
|||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
// Test on a clean database
|
||||
tx.Exec(`DELETE FROM species;`)
|
||||
tx.Exec(`DELETE FROM genera;`)
|
||||
species := insertSpecies(t, tx)
|
||||
|
||||
d := NewDatastore(nil)
|
||||
// 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")
|
||||
}
|
||||
d := NewDatastore(tx)
|
||||
|
||||
// Tweak it
|
||||
species.SpeciesName = "Updated Species"
|
||||
|
@ -140,25 +109,9 @@ func TestSpeciesStore_Delete_db(t *testing.T) {
|
|||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
// Test on a clean database
|
||||
tx.Exec(`DELETE FROM species;`)
|
||||
tx.Exec(`DELETE FROM genera;`)
|
||||
species := insertSpecies(t, 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
|
||||
deleted, err := d.Species.Delete(species.Id)
|
||||
|
|
|
@ -4,23 +4,34 @@ import (
|
|||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/jmoiron/modl"
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func TestUsersStore_Get_db(t *testing.T) {
|
||||
want := &models.User{Id: 1, UserName: "Test User"}
|
||||
func insertUser(t *testing.T, tx *modl.Transaction) *models.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()
|
||||
defer tx.Rollback()
|
||||
|
||||
// Test on a clean database
|
||||
tx.Exec(`DELETE FROM users;`)
|
||||
if err := tx.Insert(want); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := insertUser(t, tx)
|
||||
|
||||
d := NewDatastore(tx)
|
||||
user, err := d.Users.Get(1)
|
||||
|
||||
user, err := d.Users.Get(want.Id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
@ -32,15 +43,13 @@ func TestUsersStore_Get_db(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestUsersStore_Create_db(t *testing.T) {
|
||||
user := &models.User{Id: 1, UserName: "Test User"}
|
||||
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
// Test on a clean database
|
||||
tx.Exec(`DELETE FROM users;`)
|
||||
user := newUser()
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
created, err := d.Users.Create(user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
@ -55,18 +64,14 @@ func TestUsersStore_Create_db(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestUsersStore_List_db(t *testing.T) {
|
||||
want := []*models.User{{Id: 1, UserName: "Test User"}}
|
||||
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
// Test on a clean database
|
||||
tx.Exec(`DELETE FROM users;`)
|
||||
if err := tx.Insert(want[0]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
user := insertUser(t, tx)
|
||||
want := []*models.User{user}
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
users, err := d.Users.List(&models.UserListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
Reference in a new issue