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

View file

@ -18,6 +18,10 @@ type Genus struct {
DeletedAt time.Time `db:"deleted_at" json:"deleted_at"`
}
func NewGenus() *Genus {
return &Genus{GenusName: "Test Genus"}
}
// GeneraService interacts with the genus-related endpoints in bactdb's API.
type GeneraService interface {
// Get a genus.

View file

@ -8,11 +8,17 @@ import (
"github.com/thermokarst/bactdb/router"
)
func newGenus() *Genus {
genus := NewGenus()
genus.Id = 1
return genus
}
func TestGeneraService_Get(t *testing.T) {
setup()
defer teardown()
want := &Genus{Id: 1, GenusName: "Test Genus"}
want := newGenus()
var called bool
mux.HandleFunc(urlPath(t, router.Genus, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
@ -22,7 +28,7 @@ func TestGeneraService_Get(t *testing.T) {
writeJSON(w, want)
})
genus, err := client.Genera.Get(1)
genus, err := client.Genera.Get(want.Id)
if err != nil {
t.Errorf("Genera.Get returned error: %v", err)
}
@ -42,7 +48,7 @@ func TestGeneraService_Create(t *testing.T) {
setup()
defer teardown()
want := &Genus{Id: 1, GenusName: "Test Genus"}
want := newGenus()
var called bool
mux.HandleFunc(urlPath(t, router.CreateGenus, nil), func(w http.ResponseWriter, r *http.Request) {
@ -54,16 +60,14 @@ func TestGeneraService_Create(t *testing.T) {
writeJSON(w, want)
})
genus := &Genus{Id: 1, GenusName: "Test Genus"}
genus := newGenus()
created, err := client.Genera.Create(genus)
if err != nil {
t.Errorf("Genera.Create returned error: %v", err)
}
if !created {
t.Error("!created")
}
if !called {
t.Fatal("!called")
}
@ -78,7 +82,7 @@ func TestGeneraService_List(t *testing.T) {
setup()
defer teardown()
want := []*Genus{{Id: 1, GenusName: "Test Genus"}}
want := []*Genus{newGenus()}
var called bool
mux.HandleFunc(urlPath(t, router.Genera, nil), func(w http.ResponseWriter, r *http.Request) {
@ -110,7 +114,7 @@ func TestGeneraService_Update(t *testing.T) {
setup()
defer teardown()
want := &Genus{Id: 1, GenusName: "Test Genus"}
want := newGenus()
var called bool
mux.HandleFunc(urlPath(t, router.UpdateGenus, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
@ -122,8 +126,9 @@ func TestGeneraService_Update(t *testing.T) {
writeJSON(w, want)
})
genus := &Genus{Id: 1, GenusName: "Test Genus Updated"}
updated, err := client.Genera.Update(1, genus)
genus := newGenus()
genus.GenusName = "Test Genus Updated"
updated, err := client.Genera.Update(genus.Id, genus)
if err != nil {
t.Errorf("Genera.Update returned error: %v", err)
}
@ -141,7 +146,7 @@ func TestGeneraService_Delete(t *testing.T) {
setup()
defer teardown()
want := &Genus{Id: 1, GenusName: "Test Genus"}
want := newGenus()
var called bool
mux.HandleFunc(urlPath(t, router.DeleteGenus, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
@ -156,11 +161,9 @@ func TestGeneraService_Delete(t *testing.T) {
if err != nil {
t.Errorf("Genera.Delete returned error: %v", err)
}
if !deleted {
t.Error("!deleted")
}
if !called {
t.Fatal("!called")
}

View file

@ -19,6 +19,10 @@ type Species struct {
DeletedAt time.Time `db:"deleted_at" json:"deleted_at"`
}
func NewSpecies() *Species {
return &Species{SpeciesName: "Test Species"}
}
// SpeciesService interacts with the species-related endpoints in bactdb's API.
type SpeciesService interface {
// Get a species

View file

@ -8,11 +8,18 @@ import (
"github.com/thermokarst/bactdb/router"
)
func newSpecies() *Species {
species := NewSpecies()
species.Id = 1
species.GenusId = 1
return species
}
func TestSpeciesService_Get(t *testing.T) {
setup()
defer teardown()
want := &Species{Id: 1, GenusId: 1, SpeciesName: "Test Species"}
want := newSpecies()
var called bool
mux.HandleFunc(urlPath(t, router.Species, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
@ -22,11 +29,10 @@ func TestSpeciesService_Get(t *testing.T) {
writeJSON(w, want)
})
species, err := client.Species.Get(1)
species, err := client.Species.Get(want.Id)
if err != nil {
t.Errorf("Species.Get returned error: %v", err)
}
if !called {
t.Fatal("!called")
}
@ -42,7 +48,7 @@ func TestSpeciesService_Create(t *testing.T) {
setup()
defer teardown()
want := &Species{Id: 1, GenusId: 1, SpeciesName: "Test Species"}
want := newSpecies()
var called bool
mux.HandleFunc(urlPath(t, router.CreateSpecies, nil), func(w http.ResponseWriter, r *http.Request) {
@ -54,16 +60,14 @@ func TestSpeciesService_Create(t *testing.T) {
writeJSON(w, want)
})
species := &Species{Id: 1, GenusId: 1, SpeciesName: "Test Species"}
species := newSpecies()
created, err := client.Species.Create(species)
if err != nil {
t.Errorf("Species.Create returned error: %v", err)
}
if !created {
t.Error("!created")
}
if !called {
t.Fatal("!called")
}
@ -78,7 +82,7 @@ func TestSpeciesService_List(t *testing.T) {
setup()
defer teardown()
want := []*Species{{Id: 1, GenusId: 1, SpeciesName: "Test Species"}}
want := []*Species{newSpecies()}
var called bool
mux.HandleFunc(urlPath(t, router.SpeciesList, nil), func(w http.ResponseWriter, r *http.Request) {
@ -110,7 +114,7 @@ func TestSpeciesService_Update(t *testing.T) {
setup()
defer teardown()
want := &Species{Id: 1, GenusId: 1, SpeciesName: "Test Species"}
want := newSpecies()
var called bool
mux.HandleFunc(urlPath(t, router.UpdateSpecies, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
@ -122,16 +126,15 @@ func TestSpeciesService_Update(t *testing.T) {
writeJSON(w, want)
})
species := &Species{Id: 1, GenusId: 1, SpeciesName: "Test Species Updated"}
updated, err := client.Species.Update(1, species)
species := newSpecies()
species.SpeciesName = "Test Species Updated"
updated, err := client.Species.Update(species.Id, species)
if err != nil {
t.Errorf("Species.Update returned error: %v", err)
}
if !updated {
t.Error("!updated")
}
if !called {
t.Fatal("!called")
}
@ -141,7 +144,7 @@ func TestSpeciesService_Delete(t *testing.T) {
setup()
defer teardown()
want := &Species{Id: 1, GenusId: 1, SpeciesName: "Test Species"}
want := newSpecies()
var called bool
mux.HandleFunc(urlPath(t, router.DeleteSpecies, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
@ -152,15 +155,13 @@ func TestSpeciesService_Delete(t *testing.T) {
writeJSON(w, want)
})
deleted, err := client.Species.Delete(1)
deleted, err := client.Species.Delete(want.Id)
if err != nil {
t.Errorf("Species.Delete returned error: %v", err)
}
if !deleted {
t.Error("!deleted")
}
if !called {
t.Fatal("!called")
}

View file

@ -18,6 +18,10 @@ type User struct {
DeletedAt time.Time `db:"deleted_at" json:"deleted_at"`
}
func NewUser() *User {
return &User{UserName: "Test User"}
}
// UsersService interacts with the user-related endpoints in bactdb's API.
type UsersService interface {
// Get a user.

View file

@ -8,11 +8,17 @@ import (
"github.com/thermokarst/bactdb/router"
)
func newUser() *User {
user := NewUser()
user.Id = 1
return user
}
func TestUsersService_Get(t *testing.T) {
setup()
defer teardown()
want := &User{Id: 1, UserName: "Test User"}
want := newUser()
var called bool
mux.HandleFunc(urlPath(t, router.User, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
@ -42,7 +48,7 @@ func TestUsersService_Create(t *testing.T) {
setup()
defer teardown()
want := &User{Id: 1, UserName: "Test User"}
want := newUser()
var called bool
mux.HandleFunc(urlPath(t, router.CreateUser, nil), func(w http.ResponseWriter, r *http.Request) {
@ -54,7 +60,7 @@ func TestUsersService_Create(t *testing.T) {
writeJSON(w, want)
})
user := &User{Id: 1, UserName: "Test User"}
user := newUser()
created, err := client.Users.Create(user)
if err != nil {
t.Errorf("Users.Create returned error: %v", err)
@ -78,7 +84,7 @@ func TestUsersService_List(t *testing.T) {
setup()
defer teardown()
want := []*User{{Id: 1, UserName: "Test User"}}
want := []*User{newUser()}
var called bool
mux.HandleFunc(urlPath(t, router.Users, nil), func(w http.ResponseWriter, r *http.Request) {