Refactoring model & api tests.

This commit is contained in:
Matthew Dillon 2014-10-24 15:40:55 -08:00
parent cb383d84e7
commit c088386a00
9 changed files with 88 additions and 48 deletions

View file

@ -6,10 +6,16 @@ import (
"github.com/thermokarst/bactdb/models"
)
func newGenus() *models.Genus {
genus := models.NewGenus()
genus.Id = 1
return genus
}
func TestGenus_Get(t *testing.T) {
setup()
want := &models.Genus{Id: 1, GenusName: "Test Genus"}
want := newGenus()
calledGet := false
store.Genera.(*models.MockGeneraService).Get_ = func(id int64) (*models.Genus, error) {
@ -36,7 +42,7 @@ func TestGenus_Get(t *testing.T) {
func TestGenus_Create(t *testing.T) {
setup()
want := &models.Genus{Id: 1, GenusName: "Test Genus"}
want := newGenus()
calledPost := false
store.Genera.(*models.MockGeneraService).Create_ = func(genus *models.Genus) (bool, error) {
@ -63,7 +69,7 @@ func TestGenus_Create(t *testing.T) {
func TestGenus_List(t *testing.T) {
setup()
want := []*models.Genus{{Id: 1, GenusName: "Test Genus"}}
want := []*models.Genus{newGenus()}
wantOpt := &models.GenusListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}}
calledList := false
@ -83,7 +89,6 @@ func TestGenus_List(t *testing.T) {
if !calledList {
t.Error("!calledList")
}
if !normalizeDeepEqual(&want, &genera) {
t.Errorf("got genera %+v but wanted genera %+v", genera, want)
}
@ -92,7 +97,7 @@ func TestGenus_List(t *testing.T) {
func TestGenus_Update(t *testing.T) {
setup()
want := &models.Genus{Id: 1, GenusName: "Test Genus"}
want := newGenus()
calledPut := false
store.Genera.(*models.MockGeneraService).Update_ = func(id int64, genus *models.Genus) (bool, error) {
@ -122,7 +127,7 @@ func TestGenus_Update(t *testing.T) {
func TestGenus_Delete(t *testing.T) {
setup()
want := &models.Genus{Id: 1, GenusName: "Test Genus"}
want := newGenus()
calledDelete := false
store.Genera.(*models.MockGeneraService).Delete_ = func(id int64) (bool, error) {

View file

@ -6,10 +6,17 @@ import (
"github.com/thermokarst/bactdb/models"
)
func newSpecies() *models.Species {
species := models.NewSpecies()
species.Id = 1
species.GenusId = 1
return species
}
func TestSpecies_Get(t *testing.T) {
setup()
want := &models.Species{Id: 1, GenusId: 1, SpeciesName: "Test Species"}
want := newSpecies()
calledGet := false
store.Species.(*models.MockSpeciesService).Get_ = func(id int64) (*models.Species, error) {
@ -36,7 +43,7 @@ func TestSpecies_Get(t *testing.T) {
func TestSpecies_Create(t *testing.T) {
setup()
want := &models.Species{Id: 1, GenusId: 1, SpeciesName: "Test Species"}
want := newSpecies()
calledPost := false
store.Species.(*models.MockSpeciesService).Create_ = func(species *models.Species) (bool, error) {
@ -63,7 +70,7 @@ func TestSpecies_Create(t *testing.T) {
func TestSpecies_List(t *testing.T) {
setup()
want := []*models.Species{{Id: 1, GenusId: 1, SpeciesName: "Test Species"}}
want := []*models.Species{newSpecies()}
wantOpt := &models.SpeciesListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}}
calledList := false
@ -92,7 +99,7 @@ func TestSpecies_List(t *testing.T) {
func TestSpecies_Update(t *testing.T) {
setup()
want := &models.Species{Id: 1, GenusId: 1, SpeciesName: "Test Species"}
want := newSpecies()
calledPut := false
store.Species.(*models.MockSpeciesService).Update_ = func(id int64, species *models.Species) (bool, error) {
@ -122,7 +129,7 @@ func TestSpecies_Update(t *testing.T) {
func TestSpecies_Delete(t *testing.T) {
setup()
want := &models.Species{Id: 1, GenusId: 1, SpeciesName: "Test Species"}
want := newSpecies()
calledDelete := false
store.Species.(*models.MockSpeciesService).Delete_ = func(id int64) (bool, error) {

View file

@ -6,10 +6,16 @@ import (
"github.com/thermokarst/bactdb/models"
)
func newUser() *models.User {
user := models.NewUser()
user.Id = 1
return user
}
func TestUser_Get(t *testing.T) {
setup()
wantUser := &models.User{Id: 1, UserName: "Test User"}
wantUser := newUser()
calledGet := false
store.Users.(*models.MockUsersService).Get_ = func(id int64) (*models.User, error) {
@ -36,7 +42,7 @@ func TestUser_Get(t *testing.T) {
func TestUser_Create(t *testing.T) {
setup()
wantUser := &models.User{Id: 1, UserName: "Test User"}
wantUser := newUser()
calledPost := false
store.Users.(*models.MockUsersService).Create_ = func(user *models.User) (bool, error) {
@ -63,7 +69,7 @@ func TestUser_Create(t *testing.T) {
func TestUser_List(t *testing.T) {
setup()
wantUsers := []*models.User{{Id: 1, UserName: "Test User"}}
wantUsers := []*models.User{newUser()}
wantOpt := &models.UserListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}}
calledList := false