Get a text measurement type

This commit is contained in:
Matthew Dillon 2014-11-26 21:56:25 -09:00
parent 72078cf6e4
commit 3e0ca9df8c
11 changed files with 277 additions and 19 deletions

View file

@ -16,12 +16,13 @@ import (
// A Client communicates with bactdb's HTTP API.
type Client struct {
Users UsersService
Genera GeneraService
Species SpeciesService
Strains StrainsService
ObservationTypes ObservationTypesService
Observations ObservationsService
Users UsersService
Genera GeneraService
Species SpeciesService
Strains StrainsService
ObservationTypes ObservationTypesService
Observations ObservationsService
TextMeasurementTypes TextMeasurementTypesService
// BaseURL for HTTP requests to bactdb's API.
BaseURL *url.URL
@ -55,6 +56,7 @@ func NewClient(httpClient *http.Client) *Client {
c.Strains = &strainsService{c}
c.ObservationTypes = &observationTypesService{c}
c.Observations = &observationsService{c}
c.TextMeasurementTypes = &textMeasurementTypesService{c}
return c
}

View file

@ -0,0 +1,73 @@
package models
import (
"errors"
"strconv"
"time"
"github.com/lib/pq"
"github.com/thermokarst/bactdb/router"
)
// A TextMeasurementType is a lookup type
type TextMeasurementType struct {
Id int64 `json:"id,omitempty"`
TextMeasurementName string `db:"text_measurement_name" json:"textMeasurementName"`
CreatedAt time.Time `db:"created_at" json:"createdAt"`
UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
DeletedAt pq.NullTime `db:"deleted_at" json:"deletedAt"`
}
func NewTextMeasurementType() *TextMeasurementType {
return &TextMeasurementType{
TextMeasurementName: "Test Text Measurement Type",
}
}
type TextMeasurementTypesService interface {
// Get a text measurement type
Get(id int64) (*TextMeasurementType, error)
}
var (
ErrTextMeasurementTypeNotFound = errors.New("text measurement type not found")
)
type textMeasurementTypesService struct {
client *Client
}
func (s *textMeasurementTypesService) Get(id int64) (*TextMeasurementType, error) {
strId := strconv.FormatInt(id, 10)
url, err := s.client.url(router.TextMeasurementType, 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 text_measurement_type *TextMeasurementType
_, err = s.client.Do(req, &text_measurement_type)
if err != nil {
return nil, err
}
return text_measurement_type, nil
}
type MockTextMeasurementTypesService struct {
Get_ func(id int64) (*TextMeasurementType, error)
}
var _ TextMeasurementTypesService = &MockTextMeasurementTypesService{}
func (s *MockTextMeasurementTypesService) Get(id int64) (*TextMeasurementType, error) {
if s.Get_ == nil {
return nil, nil
}
return s.Get_(id)
}

View file

@ -0,0 +1,45 @@
package models
import (
"net/http"
"reflect"
"testing"
"github.com/thermokarst/bactdb/router"
)
func newTextMeasurementType() *TextMeasurementType {
text_measurement_type := NewTextMeasurementType()
text_measurement_type.Id = 1
return text_measurement_type
}
func TestTextMeasurementTypeService_Get(t *testing.T) {
setup()
defer teardown()
want := newTextMeasurementType()
var called bool
mux.HandleFunc(urlPath(t, router.TextMeasurementType, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "GET")
writeJSON(w, want)
})
text_measurement_type, err := client.TextMeasurementTypes.Get(want.Id)
if err != nil {
t.Errorf("TextMeasurementTypes.Get returned error: %v", err)
}
if !called {
t.Fatal("!called")
}
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
if !reflect.DeepEqual(text_measurement_type, want) {
t.Errorf("TextMeasurementTypes.Get return %+v, want %+v", text_measurement_type, want)
}
}