Get a strain.
This commit is contained in:
parent
20d65bd561
commit
6c118f47f7
11 changed files with 267 additions and 0 deletions
|
@ -12,6 +12,7 @@ type Datastore struct {
|
|||
Users models.UsersService
|
||||
Genera models.GeneraService
|
||||
Species models.SpeciesService
|
||||
Strains models.StrainsService
|
||||
dbh modl.SqlExecutor
|
||||
}
|
||||
|
||||
|
@ -31,6 +32,7 @@ func NewDatastore(dbh modl.SqlExecutor) *Datastore {
|
|||
d.Users = &usersStore{d}
|
||||
d.Genera = &generaStore{d}
|
||||
d.Species = &speciesStore{d}
|
||||
d.Strains = &strainsStore{d}
|
||||
return d
|
||||
}
|
||||
|
||||
|
@ -39,5 +41,6 @@ func NewMockDatastore() *Datastore {
|
|||
Users: &models.MockUsersService{},
|
||||
Genera: &models.MockGeneraService{},
|
||||
Species: &models.MockSpeciesService{},
|
||||
Strains: &models.MockStrainsService{},
|
||||
}
|
||||
}
|
||||
|
|
22
datastore/strains.go
Normal file
22
datastore/strains.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package datastore
|
||||
|
||||
import "github.com/thermokarst/bactdb/models"
|
||||
|
||||
func init() {
|
||||
DB.AddTableWithName(models.Strain{}, "strains").SetKeys(true, "Id")
|
||||
}
|
||||
|
||||
type strainsStore struct {
|
||||
*Datastore
|
||||
}
|
||||
|
||||
func (s *strainsStore) Get(id int64) (*models.Strain, error) {
|
||||
var strain []*models.Strain
|
||||
if err := s.dbh.Select(&strain, `SELECT * FROM strains WHERE id=$1;`, id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(strain) == 0 {
|
||||
return nil, models.ErrStrainNotFound
|
||||
}
|
||||
return strain[0], nil
|
||||
}
|
47
datastore/strains_test.go
Normal file
47
datastore/strains_test.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package datastore
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/jmoiron/modl"
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func insertStrain(t *testing.T, tx *modl.Transaction) *models.Strain {
|
||||
// clean up our target table
|
||||
tx.Exec(`DELETE FROM strains;`)
|
||||
strain := newStrain(t, tx)
|
||||
if err := tx.Insert(strain); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return strain
|
||||
}
|
||||
|
||||
func newStrain(t *testing.T, tx *modl.Transaction) *models.Strain {
|
||||
// we want to create and insert a species (and genus) record too
|
||||
species := insertSpecies(t, tx)
|
||||
return &models.Strain{SpeciesId: species.Id, StrainName: "Test Strain",
|
||||
StrainType: "Test Type", Etymology: "Test Etymology",
|
||||
AccessionBanks: "Test Bank", GenbankEmblDdb: "Test Genbank"}
|
||||
}
|
||||
|
||||
func TestStrainsStore_Get_db(t *testing.T) {
|
||||
tx, _ := DB.Begin()
|
||||
defer tx.Rollback()
|
||||
|
||||
want := insertStrain(t, tx)
|
||||
|
||||
d := NewDatastore(tx)
|
||||
|
||||
strain, err := d.Strains.Get(want.Id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
|
||||
|
||||
if !reflect.DeepEqual(strain, want) {
|
||||
t.Errorf("got strain %+v, want %+v", strain, want)
|
||||
}
|
||||
}
|
Reference in a new issue