Get a unit type

This commit is contained in:
Matthew Dillon 2014-11-29 17:20:47 -09:00
parent 431ba7ed91
commit c2bfc8b93b
11 changed files with 260 additions and 0 deletions

View file

@ -23,6 +23,7 @@ type Client struct {
ObservationTypes ObservationTypesService
Observations ObservationsService
TextMeasurementTypes TextMeasurementTypesService
UnitTypes UnitTypesService
// BaseURL for HTTP requests to bactdb's API.
BaseURL *url.URL
@ -57,6 +58,7 @@ func NewClient(httpClient *http.Client) *Client {
c.ObservationTypes = &observationTypesService{c}
c.Observations = &observationsService{c}
c.TextMeasurementTypes = &textMeasurementTypesService{c}
c.UnitTypes = &unitTypesService{c}
return c
}

75
models/unit_types.go Normal file
View file

@ -0,0 +1,75 @@
package models
import (
"errors"
"strconv"
"time"
"github.com/lib/pq"
"github.com/thermokarst/bactdb/router"
)
// A UnitType is a lookup type
type UnitType struct {
Id int64 `json:id,omitempty"`
Name string `db:"name" json:"name"`
Symbol string `db:"symbol" json:"symbol"`
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 NewUnitType() *UnitType {
return &UnitType{
Name: "Test Unit Type",
Symbol: "x",
}
}
type UnitTypesService interface {
// Get a unit type
Get(id int64) (*UnitType, error)
}
var (
ErrUnitTypeNotFound = errors.New("unit type not found")
)
type unitTypesService struct {
client *Client
}
func (s *unitTypesService) Get(id int64) (*UnitType, error) {
strId := strconv.FormatInt(id, 10)
url, err := s.client.url(router.UnitType, 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 unit_type *UnitType
_, err = s.client.Do(req, &unit_type)
if err != nil {
return nil, err
}
return unit_type, nil
}
type MockUnitTypesService struct {
Get_ func(id int64) (*UnitType, error)
}
var _ UnitTypesService = &MockUnitTypesService{}
func (s *MockUnitTypesService) Get(id int64) (*UnitType, error) {
if s.Get_ == nil {
return nil, nil
}
return s.Get_(id)
}

45
models/unit_types_test.go Normal file
View file

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