create an observation type

This commit is contained in:
Matthew Dillon 2014-11-04 16:13:55 -09:00
parent 4fd7bf8eba
commit 0e767390b5
9 changed files with 155 additions and 4 deletions

View file

@ -42,6 +42,7 @@ func Handler() *mux.Router {
m.Get(router.DeleteStrain).Handler(handler(serveDeleteStrain))
m.Get(router.ObservationType).Handler(handler(serveObservationType))
m.Get(router.CreateObservationType).Handler(handler(serveCreateObservationType))
return m
}

View file

@ -1,10 +1,12 @@
package api
import (
"encoding/json"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/thermokarst/bactdb/models"
)
func serveObservationType(w http.ResponseWriter, r *http.Request) error {
@ -20,3 +22,21 @@ func serveObservationType(w http.ResponseWriter, r *http.Request) error {
return writeJSON(w, observation_type)
}
func serveCreateObservationType(w http.ResponseWriter, r *http.Request) error {
var observation_type models.ObservationType
err := json.NewDecoder(r.Body).Decode(&observation_type)
if err != nil {
return err
}
created, err := store.ObservationTypes.Create(&observation_type)
if err != nil {
return err
}
if created {
w.WriteHeader(http.StatusCreated)
}
return writeJSON(w, observation_type)
}

View file

@ -38,3 +38,30 @@ func TestObservationType_Get(t *testing.T) {
t.Errorf("got %+v but wanted %+v", got, want)
}
}
func TestObservationType_Create(t *testing.T) {
setup()
want := newObservationType()
calledPost := false
store.ObservationTypes.(*models.MockObservationTypesService).Create_ = func(observation_type *models.ObservationType) (bool, error) {
if !normalizeDeepEqual(want, observation_type) {
t.Errorf("wanted request for observation_type %d but got %d", want, observation_type)
}
calledPost = true
return true, nil
}
success, err := apiClient.ObservationTypes.Create(want)
if err != nil {
t.Fatal(err)
}
if !calledPost {
t.Error("!calledPost")
}
if !success {
t.Error("!success")
}
}

View file

@ -1,6 +1,10 @@
package datastore
import "github.com/thermokarst/bactdb/models"
import (
"time"
"github.com/thermokarst/bactdb/models"
)
func init() {
DB.AddTableWithName(models.ObservationType{}, "observation_types").SetKeys(true, "Id")
@ -20,3 +24,13 @@ func (s *observationTypesStore) Get(id int64) (*models.ObservationType, error) {
}
return observation_type[0], nil
}
func (s *observationTypesStore) Create(observation_type *models.ObservationType) (bool, error) {
currentTime := time.Now()
observation_type.CreatedAt = currentTime
observation_type.UpdatedAt = currentTime
if err := s.dbh.Insert(observation_type); err != nil {
return false, err
}
return true, nil
}

View file

@ -42,3 +42,23 @@ func TestObservationTypesStore_Get_db(t *testing.T) {
t.Errorf("got observation_type %+v, want %+v", observation_type, want)
}
}
func TestObservationTypesStore_Create_db(t *testing.T) {
tx, _ := DB.Begin()
defer tx.Rollback()
observation_type := newObservationType(t, tx)
d := NewDatastore(tx)
created, err := d.ObservationTypes.Create(observation_type)
if err != nil {
t.Fatal(err)
}
if !created {
t.Error("!created")
}
if observation_type.Id == 0 {
t.Error("want nonzero observation_type.Id after submitting")
}
}

View file

@ -2,6 +2,7 @@ package models
import (
"errors"
"net/http"
"strconv"
"time"
@ -27,6 +28,9 @@ func NewObservationType() *ObservationType {
type ObservationTypesService interface {
// Get an observation type
Get(id int64) (*ObservationType, error)
// Create an observation type record
Create(observation_type *ObservationType) (bool, error)
}
var (
@ -59,8 +63,28 @@ func (s *observationTypesService) Get(id int64) (*ObservationType, error) {
return observation_type, nil
}
func (s *observationTypesService) Create(observation_type *ObservationType) (bool, error) {
url, err := s.client.url(router.CreateObservationType, nil, nil)
if err != nil {
return false, err
}
req, err := s.client.NewRequest("POST", url.String(), observation_type)
if err != nil {
return false, err
}
resp, err := s.client.Do(req, &observation_type)
if err != nil {
return false, err
}
return resp.StatusCode == http.StatusCreated, nil
}
type MockObservationTypesService struct {
Get_ func(id int64) (*ObservationType, error)
Get_ func(id int64) (*ObservationType, error)
Create_ func(observation_type *ObservationType) (bool, error)
}
var _ ObservationTypesService = &MockObservationTypesService{}
@ -71,3 +95,10 @@ func (s *MockObservationTypesService) Get(id int64) (*ObservationType, error) {
}
return s.Get_(id)
}
func (s *MockObservationTypesService) Create(observation_type *ObservationType) (bool, error) {
if s.Create_ == nil {
return false, nil
}
return s.Create_(observation_type)
}

View file

@ -14,7 +14,7 @@ func newObservationType() *ObservationType {
return observation_type
}
func TestObservation_TypeService_Get(t *testing.T) {
func TestObservationTypeService_Get(t *testing.T) {
setup()
defer teardown()
@ -43,3 +43,39 @@ func TestObservation_TypeService_Get(t *testing.T) {
t.Errorf("ObservationTypes.Get return %+v, want %+v", observation_type, want)
}
}
func TestObservationTypeService_Create(t *testing.T) {
setup()
defer teardown()
want := newObservationType()
var called bool
mux.HandleFunc(urlPath(t, router.CreateObservationType, nil), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "POST")
testBody(t, r, `{"id":1,"observation_type_name":"Test Obs Type","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":{"Time":"0001-01-01T00:00:00Z","Valid":false}}`+"\n")
w.WriteHeader(http.StatusCreated)
writeJSON(w, want)
})
observation_type := newObservationType()
created, err := client.ObservationTypes.Create(observation_type)
if err != nil {
t.Errorf("ObservationTypes.Create returned error: %v", err)
}
if !created {
t.Error("!created")
}
if !called {
t.Fatal("!called")
}
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
if !reflect.DeepEqual(observation_type, want) {
t.Errorf("ObservationTypes.Create returned %+v, want %+v", observation_type, want)
}
}

View file

@ -32,6 +32,7 @@ func API() *mux.Router {
m.Path("/strains/{Id:.+}").Methods("DELETE").Name(DeleteStrain)
// ObservationTypes
m.Path("/observation_types").Methods("POST").Name(CreateObservationType)
m.Path("/observation_types/{Id:.+}").Methods("GET").Name(ObservationType)
return m

View file

@ -23,5 +23,6 @@ const (
UpdateStrain = "strain:update"
DeleteStrain = "strain:delete"
ObservationType = "observation_type:get"
ObservationType = "observation_type:get"
CreateObservationType = "observation_type:create"
)