Get an observation type
This commit is contained in:
parent
bedddfdec1
commit
4fd7bf8eba
11 changed files with 271 additions and 13 deletions
|
@ -16,10 +16,11 @@ import (
|
|||
|
||||
// A Client communicates with bactdb's HTTP API.
|
||||
type Client struct {
|
||||
Users UsersService
|
||||
Genera GeneraService
|
||||
Species SpeciesService
|
||||
Strains StrainsService
|
||||
Users UsersService
|
||||
Genera GeneraService
|
||||
Species SpeciesService
|
||||
Strains StrainsService
|
||||
ObservationTypes ObservationTypesService
|
||||
|
||||
// BaseURL for HTTP requests to bactdb's API.
|
||||
BaseURL *url.URL
|
||||
|
@ -51,6 +52,7 @@ func NewClient(httpClient *http.Client) *Client {
|
|||
c.Genera = &generaService{c}
|
||||
c.Species = &speciesService{c}
|
||||
c.Strains = &strainsService{c}
|
||||
c.ObservationTypes = &observationTypesService{c}
|
||||
return c
|
||||
}
|
||||
|
||||
|
|
73
models/observation_types.go
Normal file
73
models/observation_types.go
Normal file
|
@ -0,0 +1,73 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/lib/pq"
|
||||
"github.com/thermokarst/bactdb/router"
|
||||
)
|
||||
|
||||
// An Observation Type is a lookup type
|
||||
type ObservationType struct {
|
||||
Id int64 `json:"id,omitempty"`
|
||||
ObservationTypeName string `db:"observation_type_name" json:"observation_type_name"`
|
||||
CreatedAt time.Time `db:"created_at" json:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
|
||||
DeletedAt pq.NullTime `db:"deleted_at" json:"deleted_at"`
|
||||
}
|
||||
|
||||
func NewObservationType() *ObservationType {
|
||||
return &ObservationType{
|
||||
ObservationTypeName: "Test Obs Type",
|
||||
}
|
||||
}
|
||||
|
||||
type ObservationTypesService interface {
|
||||
// Get an observation type
|
||||
Get(id int64) (*ObservationType, error)
|
||||
}
|
||||
|
||||
var (
|
||||
ErrObservationTypeNotFound = errors.New("observation type not found")
|
||||
)
|
||||
|
||||
type observationTypesService struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
func (s *observationTypesService) Get(id int64) (*ObservationType, error) {
|
||||
strId := strconv.FormatInt(id, 10)
|
||||
|
||||
url, err := s.client.url(router.ObservationType, 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 observation_type *ObservationType
|
||||
_, err = s.client.Do(req, &observation_type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return observation_type, nil
|
||||
}
|
||||
|
||||
type MockObservationTypesService struct {
|
||||
Get_ func(id int64) (*ObservationType, error)
|
||||
}
|
||||
|
||||
var _ ObservationTypesService = &MockObservationTypesService{}
|
||||
|
||||
func (s *MockObservationTypesService) Get(id int64) (*ObservationType, error) {
|
||||
if s.Get_ == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return s.Get_(id)
|
||||
}
|
45
models/observation_types_test.go
Normal file
45
models/observation_types_test.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/thermokarst/bactdb/router"
|
||||
)
|
||||
|
||||
func newObservationType() *ObservationType {
|
||||
observation_type := NewObservationType()
|
||||
observation_type.Id = 1
|
||||
return observation_type
|
||||
}
|
||||
|
||||
func TestObservation_TypeService_Get(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := newObservationType()
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.ObservationType, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "GET")
|
||||
|
||||
writeJSON(w, want)
|
||||
})
|
||||
|
||||
observation_type, err := client.ObservationTypes.Get(want.Id)
|
||||
if err != nil {
|
||||
t.Errorf("ObservationTypes.Get returned error: %v", err)
|
||||
}
|
||||
|
||||
if !called {
|
||||
t.Fatal("!called")
|
||||
}
|
||||
|
||||
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
|
||||
|
||||
if !reflect.DeepEqual(observation_type, want) {
|
||||
t.Errorf("ObservationTypes.Get return %+v, want %+v", observation_type, want)
|
||||
}
|
||||
}
|
Reference in a new issue