Get a strain.
This commit is contained in:
parent
20d65bd561
commit
6c118f47f7
11 changed files with 267 additions and 0 deletions
|
@ -19,6 +19,7 @@ type Client struct {
|
|||
Users UsersService
|
||||
Genera GeneraService
|
||||
Species SpeciesService
|
||||
Strains StrainsService
|
||||
|
||||
// BaseURL for HTTP requests to bactdb's API.
|
||||
BaseURL *url.URL
|
||||
|
@ -49,6 +50,7 @@ func NewClient(httpClient *http.Client) *Client {
|
|||
c.Users = &usersService{c}
|
||||
c.Genera = &generaService{c}
|
||||
c.Species = &speciesService{c}
|
||||
c.Strains = &strainsService{c}
|
||||
return c
|
||||
}
|
||||
|
||||
|
|
76
models/strains.go
Normal file
76
models/strains.go
Normal file
|
@ -0,0 +1,76 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/thermokarst/bactdb/router"
|
||||
)
|
||||
|
||||
// A Strain is a subclass of species
|
||||
type Strain struct {
|
||||
Id int64 `json:"id,omitempty"`
|
||||
SpeciesId int64 `db:"species_id" json:"species_id"`
|
||||
StrainName string `db:"strain_name" json:"strain_name"`
|
||||
StrainType string `db:"strain_type" json:"strain_type"`
|
||||
Etymology string `db:"etymology" json:"etymology"`
|
||||
AccessionBanks string `db:"accession_banks" json:"accession_banks"`
|
||||
GenbankEmblDdb string `db:"genbank_embl_ddb" json:"genbank_eml_ddb"`
|
||||
CreatedAt time.Time `db:"created_at" json:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
|
||||
DeletedAt time.Time `db:"deleted_at" json:"deleted_at"`
|
||||
}
|
||||
|
||||
func NewStrain() *Strain {
|
||||
return &Strain{StrainName: "Test Strain", StrainType: "Test Type", Etymology: "Test Etymology", AccessionBanks: "Test Accession", GenbankEmblDdb: "Test Genbank"}
|
||||
}
|
||||
|
||||
// StrainService interacts with the strain-related endpoints in bactdb's API
|
||||
type StrainsService interface {
|
||||
// Get a strain
|
||||
Get(id int64) (*Strain, error)
|
||||
}
|
||||
|
||||
var (
|
||||
ErrStrainNotFound = errors.New("strain not found")
|
||||
)
|
||||
|
||||
type strainsService struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
func (s *strainsService) Get(id int64) (*Strain, error) {
|
||||
strId := strconv.FormatInt(id, 10)
|
||||
|
||||
url, err := s.client.url(router.Strain, map[string]string{"Id": strId}, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("GET", url.String(), nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var strain *Strain
|
||||
_, err = s.client.Do(req, &strain)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return strain, nil
|
||||
}
|
||||
|
||||
type MockStrainsService struct {
|
||||
Get_ func(id int64) (*Strain, error)
|
||||
}
|
||||
|
||||
var _ StrainsService = &MockStrainsService{}
|
||||
|
||||
func (s *MockStrainsService) Get(id int64) (*Strain, error) {
|
||||
if s.Get_ == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return s.Get_(id)
|
||||
}
|
46
models/strains_test.go
Normal file
46
models/strains_test.go
Normal file
|
@ -0,0 +1,46 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/thermokarst/bactdb/router"
|
||||
)
|
||||
|
||||
func newStrain() *Strain {
|
||||
strain := NewStrain()
|
||||
strain.Id = 1
|
||||
strain.SpeciesId = 1
|
||||
return strain
|
||||
}
|
||||
|
||||
func TestStrainService_Get(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := newStrain()
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.Strain, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "GET")
|
||||
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
||||
strain, err := client.Strains.Get(want.Id)
|
||||
if err != nil {
|
||||
t.Errorf("Strain.Get returned error: %v", err)
|
||||
}
|
||||
|
||||
if !called {
|
||||
t.Fatal("!called")
|
||||
}
|
||||
|
||||
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
|
||||
|
||||
if !reflect.DeepEqual(strain, want) {
|
||||
t.Errorf("Strain.Get return %+v, want %+v", strain, want)
|
||||
}
|
||||
}
|
Reference in a new issue