Species Read - Order of ops:
router/routes.go router/api.go models/species_test.go models/species.go models/client.go datastore/migrations/addspecies.sql datastore/migrations/dropspecies.sql datastore/species_test.go datastore/species.go datastore/datastore.go api/species_test.go api/species.go api/handler.go
This commit is contained in:
parent
6fe6d5d189
commit
830a8805c9
13 changed files with 263 additions and 7 deletions
|
@ -9,9 +9,10 @@ import (
|
|||
|
||||
// A datastore access point (in PostgreSQL)
|
||||
type Datastore struct {
|
||||
Users models.UsersService
|
||||
Genera models.GeneraService
|
||||
dbh modl.SqlExecutor
|
||||
Users models.UsersService
|
||||
Genera models.GeneraService
|
||||
Species models.SpeciesService
|
||||
dbh modl.SqlExecutor
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -29,12 +30,14 @@ func NewDatastore(dbh modl.SqlExecutor) *Datastore {
|
|||
d := &Datastore{dbh: dbh}
|
||||
d.Users = &usersStore{d}
|
||||
d.Genera = &generaStore{d}
|
||||
d.Species = &speciesStore{d}
|
||||
return d
|
||||
}
|
||||
|
||||
func NewMockDatastore() *Datastore {
|
||||
return &Datastore{
|
||||
Users: &models.MockUsersService{},
|
||||
Genera: &models.MockGeneraService{},
|
||||
Users: &models.MockUsersService{},
|
||||
Genera: &models.MockGeneraService{},
|
||||
Species: &models.MockSpeciesService{},
|
||||
}
|
||||
}
|
||||
|
|
5
datastore/migrations/00003_AddSpecies_down.sql
Normal file
5
datastore/migrations/00003_AddSpecies_down.sql
Normal file
|
@ -0,0 +1,5 @@
|
|||
-- bactdb
|
||||
-- Matthew R Dillon
|
||||
|
||||
DROP TABLE species;
|
||||
|
15
datastore/migrations/00003_AddSpecies_up.sql
Normal file
15
datastore/migrations/00003_AddSpecies_up.sql
Normal file
|
@ -0,0 +1,15 @@
|
|||
-- bactdb
|
||||
-- Matthew Dillon
|
||||
|
||||
CREATE TABLE species (
|
||||
id BIGSERIAL NOT NULL,
|
||||
genusid BIGINT,
|
||||
speciesname CHARACTER VARYING(100),
|
||||
|
||||
createdat TIMESTAMP WITH TIME ZONE,
|
||||
updatedat TIMESTAMP WITH TIME ZONE,
|
||||
deletedat TIMESTAMP WITH TIME ZONE,
|
||||
|
||||
CONSTRAINT species_pkey PRIMARY KEY (id),
|
||||
FOREIGN KEY (genusid) REFERENCES genera(id)
|
||||
);
|
22
datastore/species.go
Normal file
22
datastore/species.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package datastore
|
||||
|
||||
import "github.com/thermokarst/bactdb/models"
|
||||
|
||||
func init() {
|
||||
DB.AddTableWithName(models.Species{}, "species").SetKeys(true, "Id")
|
||||
}
|
||||
|
||||
type speciesStore struct {
|
||||
*Datastore
|
||||
}
|
||||
|
||||
func (s *speciesStore) Get(id int64) (*models.Species, error) {
|
||||
var species []*models.Species
|
||||
if err := s.dbh.Select(&species, `SELECT * FROM species WHERE id=$1;`, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(species) == 0 {
|
||||
return nil, models.ErrSpeciesNotFound
|
||||
}
|
||||
return species[0], nil
|
||||
}
|
37
datastore/species_test.go
Normal file
37
datastore/species_test.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package datastore
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func TestSpeciesStore_Get_db(t *testing.T) {
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
// Test on a clean database
|
||||
tx.Exec(`DELETE FROM species;`)
|
||||
|
||||
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)
|
||||
species, err := d.Species.Get(1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
|
||||
if !reflect.DeepEqual(species, want) {
|
||||
t.Errorf("got species %+v, want %+v", species, want)
|
||||
}
|
||||
}
|
Reference in a new issue