Get an observation.

This commit is contained in:
Matthew Dillon 2014-11-18 10:41:24 -09:00
parent 01036de04d
commit a018d2bd7a
11 changed files with 262 additions and 0 deletions

View file

@ -47,6 +47,8 @@ func Handler() *mux.Router {
m.Get(router.UpdateObservationType).Handler(handler(serveUpdateObservationType))
m.Get(router.DeleteObservationType).Handler(handler(serveDeleteObservationType))
m.Get(router.Observation).Handler(handler(serveObservation))
return m
}

22
api/observations.go Normal file
View file

@ -0,0 +1,22 @@
package api
import (
"net/http"
"strconv"
"github.com/gorilla/mux"
)
func serveObservation(w http.ResponseWriter, r *http.Request) error {
id, err := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
if err != nil {
return err
}
observation, err := store.Observations.Get(id)
if err != nil {
return err
}
return writeJSON(w, observation)
}

40
api/observations_test.go Normal file
View file

@ -0,0 +1,40 @@
package api
import (
"testing"
"github.com/thermokarst/bactdb/models"
)
func newObservation() *models.Observation {
observation := models.NewObservation()
return observation
}
func TestObservation_Get(t *testing.T) {
setup()
want := newObservation()
calledGet := false
store.Observations.(*models.MockObservationsService).Get_ = func(id int64) (*models.Observation, error) {
if id != want.Id {
t.Errorf("wanted request for observation %d but got %d", want.Id, id)
}
calledGet = true
return want, nil
}
got, err := apiClient.Observations.Get(want.Id)
if err != nil {
t.Fatal(err)
}
if !calledGet {
t.Error("!calledGet")
}
if !normalizeDeepEqual(want, got) {
t.Errorf("got %+v but wanted %+v", got, want)
}
}