This repository has been archived on 2025-03-30. You can view files and clone it, but cannot push or open issues or pull requests.
bactdb/api/species_test.go
2014-10-24 16:02:19 -08:00

154 lines
3 KiB
Go

package api
import (
"testing"
"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 := newSpecies()
calledGet := false
store.Species.(*models.MockSpeciesService).Get_ = func(id int64) (*models.Species, error) {
if id != want.Id {
t.Errorf("wanted request for species %d but got %d", want.Id, id)
}
calledGet = true
return want, nil
}
got, err := apiClient.Species.Get(want.Id)
if err != nil {
t.Fatal(err)
}
if !calledGet {
t.Error("!calledGet")
}
if !normalizeDeepEqual(want, got) {
t.Errorf("got species %+v but wanted species %+v", got, want)
}
}
func TestSpecies_Create(t *testing.T) {
setup()
want := newSpecies()
calledPost := false
store.Species.(*models.MockSpeciesService).Create_ = func(species *models.Species) (bool, error) {
if !normalizeDeepEqual(want, species) {
t.Errorf("wanted request for species %d but got %d", want, species)
}
calledPost = true
return true, nil
}
success, err := apiClient.Species.Create(want)
if err != nil {
t.Fatal(err)
}
if !calledPost {
t.Error("!calledPost")
}
if !success {
t.Error("!success")
}
}
func TestSpecies_List(t *testing.T) {
setup()
want := []*models.Species{newSpecies()}
wantOpt := &models.SpeciesListOptions{ListOptions: models.ListOptions{Page: 1, PerPage: 10}}
calledList := false
store.Species.(*models.MockSpeciesService).List_ = func(opt *models.SpeciesListOptions) ([]*models.Species, error) {
if !normalizeDeepEqual(wantOpt, opt) {
t.Errorf("wanted options %d but got %d", wantOpt, opt)
}
calledList = true
return want, nil
}
species, err := apiClient.Species.List(wantOpt)
if err != nil {
t.Fatal(err)
}
if !calledList {
t.Error("!calledList")
}
if !normalizeDeepEqual(&want, &species) {
t.Errorf("got species %+v but wanted species %+v", species, want)
}
}
func TestSpecies_Update(t *testing.T) {
setup()
want := newSpecies()
calledPut := false
store.Species.(*models.MockSpeciesService).Update_ = func(id int64, species *models.Species) (bool, error) {
if id != want.Id {
t.Errorf("wanted request for species %d but got %d", want.Id, id)
}
if !normalizeDeepEqual(want, species) {
t.Errorf("wanted request for species %d but got %d", want, species)
}
calledPut = true
return true, nil
}
success, err := apiClient.Species.Update(1, want)
if err != nil {
t.Fatal(err)
}
if !calledPut {
t.Error("!calledPut")
}
if !success {
t.Error("!success")
}
}
func TestSpecies_Delete(t *testing.T) {
setup()
want := newSpecies()
calledDelete := false
store.Species.(*models.MockSpeciesService).Delete_ = func(id int64) (bool, error) {
if id != want.Id {
t.Errorf("wanted request for species %d but got %d", want.Id, id)
}
calledDelete = true
return true, nil
}
success, err := apiClient.Species.Delete(1)
if err != nil {
t.Fatal(err)
}
if !calledDelete {
t.Error("!calledDelete")
}
if !success {
t.Error("!success")
}
}