Create a new observation
This commit is contained in:
parent
a018d2bd7a
commit
f867e5c424
9 changed files with 154 additions and 3 deletions
|
@ -2,6 +2,7 @@ package models
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
|
@ -28,6 +29,9 @@ func NewObservation() *Observation {
|
|||
type ObservationsService interface {
|
||||
// Get an observation
|
||||
Get(id int64) (*Observation, error)
|
||||
|
||||
// Create an observation
|
||||
Create(observation *Observation) (bool, error)
|
||||
}
|
||||
|
||||
var (
|
||||
|
@ -60,8 +64,28 @@ func (s *observationsService) Get(id int64) (*Observation, error) {
|
|||
return observation, nil
|
||||
}
|
||||
|
||||
func (s *observationsService) Create(observation *Observation) (bool, error) {
|
||||
url, err := s.client.url(router.CreateObservation, nil, nil)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
req, err := s.client.NewRequest("POST", url.String(), observation)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
resp, err := s.client.Do(req, &observation)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return resp.StatusCode == http.StatusCreated, nil
|
||||
}
|
||||
|
||||
type MockObservationsService struct {
|
||||
Get_ func(id int64) (*Observation, error)
|
||||
Get_ func(id int64) (*Observation, error)
|
||||
Create_ func(observation *Observation) (bool, error)
|
||||
}
|
||||
|
||||
var _ObservationsService = &MockObservationsService{}
|
||||
|
@ -72,3 +96,10 @@ func (s *MockObservationsService) Get(id int64) (*Observation, error) {
|
|||
}
|
||||
return s.Get_(id)
|
||||
}
|
||||
|
||||
func (s *MockObservationsService) Create(observation *Observation) (bool, error) {
|
||||
if s.Create_ == nil {
|
||||
return false, nil
|
||||
}
|
||||
return s.Create_(observation)
|
||||
}
|
||||
|
|
|
@ -43,3 +43,39 @@ func TestObservationService_Get(t *testing.T) {
|
|||
t.Errorf("Observations.Get return %+v, want %+v", observation, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestObservationService_Create(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
want := newObservation()
|
||||
|
||||
var called bool
|
||||
mux.HandleFunc(urlPath(t, router.CreateObservation, nil), func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
testMethod(t, r, "POST")
|
||||
testBody(t, r, `{"id":1,"observation_name":"Test Observation","observation_type_id":0,"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 := newObservation()
|
||||
created, err := client.Observations.Create(observation)
|
||||
if err != nil {
|
||||
t.Errorf("Observations.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, want) {
|
||||
t.Errorf("Observations.Create returned %+v, want %+v", observation, want)
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue