Get an observation type

This commit is contained in:
Matthew Dillon 2014-11-03 16:12:50 -09:00
parent bedddfdec1
commit 4fd7bf8eba
11 changed files with 271 additions and 13 deletions

View file

@ -9,11 +9,12 @@ import (
// A datastore access point (in PostgreSQL)
type Datastore struct {
Users models.UsersService
Genera models.GeneraService
Species models.SpeciesService
Strains models.StrainsService
dbh modl.SqlExecutor
Users models.UsersService
Genera models.GeneraService
Species models.SpeciesService
Strains models.StrainsService
ObservationTypes models.ObservationTypesService
dbh modl.SqlExecutor
}
var (
@ -33,14 +34,16 @@ func NewDatastore(dbh modl.SqlExecutor) *Datastore {
d.Genera = &generaStore{d}
d.Species = &speciesStore{d}
d.Strains = &strainsStore{d}
d.ObservationTypes = &observationTypesStore{d}
return d
}
func NewMockDatastore() *Datastore {
return &Datastore{
Users: &models.MockUsersService{},
Genera: &models.MockGeneraService{},
Species: &models.MockSpeciesService{},
Strains: &models.MockStrainsService{},
Users: &models.MockUsersService{},
Genera: &models.MockGeneraService{},
Species: &models.MockSpeciesService{},
Strains: &models.MockStrainsService{},
ObservationTypes: &models.MockObservationTypesService{},
}
}

View file

@ -0,0 +1,22 @@
package datastore
import "github.com/thermokarst/bactdb/models"
func init() {
DB.AddTableWithName(models.ObservationType{}, "observation_types").SetKeys(true, "Id")
}
type observationTypesStore struct {
*Datastore
}
func (s *observationTypesStore) Get(id int64) (*models.ObservationType, error) {
var observation_type []*models.ObservationType
if err := s.dbh.Select(&observation_type, `SELECT * FROM observation_types WHERE id=$1;`, id); err != nil {
return nil, err
}
if len(observation_type) == 0 {
return nil, models.ErrObservationTypeNotFound
}
return observation_type[0], nil
}

View file

@ -0,0 +1,44 @@
package datastore
import (
"reflect"
"testing"
"github.com/jmoiron/modl"
"github.com/thermokarst/bactdb/models"
)
func insertObservationType(t *testing.T, tx *modl.Transaction) *models.ObservationType {
// clean up our target table
tx.Exec(`DELETE FROM observation_types;`)
observation_type := newObservationType(t, tx)
if err := tx.Insert(observation_type); err != nil {
t.Fatal(err)
}
return observation_type
}
func newObservationType(t *testing.T, tx *modl.Transaction) *models.ObservationType {
return &models.ObservationType{ObservationTypeName: "Test Obs"}
}
func TestObservationTypesStore_Get_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
want := insertObservationType(t, tx)
d := NewDatastore(tx)
observation_type, err := d.ObservationTypes.Get(want.Id)
if err != nil {
t.Fatal(err)
}
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
normalizeTime(&observation_type.CreatedAt, &observation_type.UpdatedAt, &observation_type.DeletedAt)
if !reflect.DeepEqual(observation_type, want) {
t.Errorf("got observation_type %+v, want %+v", observation_type, want)
}
}