Get an observation.
This commit is contained in:
parent
01036de04d
commit
a018d2bd7a
11 changed files with 262 additions and 0 deletions
|
@ -14,6 +14,7 @@ type Datastore struct {
|
|||
Species models.SpeciesService
|
||||
Strains models.StrainsService
|
||||
ObservationTypes models.ObservationTypesService
|
||||
Observations models.ObservationsService
|
||||
dbh modl.SqlExecutor
|
||||
}
|
||||
|
||||
|
@ -35,6 +36,7 @@ func NewDatastore(dbh modl.SqlExecutor) *Datastore {
|
|||
d.Species = &speciesStore{d}
|
||||
d.Strains = &strainsStore{d}
|
||||
d.ObservationTypes = &observationTypesStore{d}
|
||||
d.Observations = &observationsStore{d}
|
||||
return d
|
||||
}
|
||||
|
||||
|
@ -45,5 +47,6 @@ func NewMockDatastore() *Datastore {
|
|||
Species: &models.MockSpeciesService{},
|
||||
Strains: &models.MockStrainsService{},
|
||||
ObservationTypes: &models.MockObservationTypesService{},
|
||||
Observations: &models.MockObservationsService{},
|
||||
}
|
||||
}
|
||||
|
|
22
datastore/observations.go
Normal file
22
datastore/observations.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package datastore
|
||||
|
||||
import "github.com/thermokarst/bactdb/models"
|
||||
|
||||
func init() {
|
||||
DB.AddTableWithName(models.Observation{}, "observations").SetKeys(true, "Id")
|
||||
}
|
||||
|
||||
type observationsStore struct {
|
||||
*Datastore
|
||||
}
|
||||
|
||||
func (s *observationsStore) Get(id int64) (*models.Observation, error) {
|
||||
var observation []*models.Observation
|
||||
if err := s.dbh.Select(&observation, `SELECT * FROM observations WHERE id=$1;`, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(observation) == 0 {
|
||||
return nil, models.ErrObservationNotFound
|
||||
}
|
||||
return observation[0], nil
|
||||
}
|
47
datastore/observations_test.go
Normal file
47
datastore/observations_test.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package datastore
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/jmoiron/modl"
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func insertObservation(t *testing.T, tx *modl.Transaction) *models.Observation {
|
||||
// clean up our target table
|
||||
tx.Exec(`DELETE FROM observations;`)
|
||||
observation := newObservation(t, tx)
|
||||
if err := tx.Insert(observation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return observation
|
||||
}
|
||||
|
||||
func newObservation(t *testing.T, tx *modl.Transaction) *models.Observation {
|
||||
// we want to create and insert an observation type record, too.
|
||||
observation_type := insertObservationType(t, tx)
|
||||
return &models.Observation{ObservationName: "Test Observation",
|
||||
ObservationTypeId: observation_type.Id}
|
||||
}
|
||||
|
||||
func TestObservationsStore_Get_db(t *testing.T) {
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
want := insertObservation(t, tx)
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
observation, err := d.Observations.Get(want.Id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
|
||||
normalizeTime(&observation.CreatedAt, &observation.UpdatedAt, &observation.DeletedAt)
|
||||
|
||||
if !reflect.DeepEqual(observation, want) {
|
||||
t.Errorf("got observation %+v, want %+v", observation, want)
|
||||
}
|
||||
}
|
Reference in a new issue