Species Read - Order of ops:

router/routes.go
router/api.go
models/species_test.go
models/species.go
models/client.go
datastore/migrations/addspecies.sql
datastore/migrations/dropspecies.sql
datastore/species_test.go
datastore/species.go
datastore/datastore.go
api/species_test.go
api/species.go
api/handler.go
This commit is contained in:
Matthew Dillon 2014-10-15 13:01:11 -08:00
parent 6fe6d5d189
commit 830a8805c9
13 changed files with 263 additions and 7 deletions

View file

@ -29,6 +29,8 @@ func Handler() *mux.Router {
m.Get(router.UpdateGenus).Handler(handler(serveUpdateGenus))
m.Get(router.DeleteGenus).Handler(handler(serveDeleteGenus))
m.Get(router.Species).Handler(handler(serveSpecies))
return m
}

22
api/species.go Normal file
View file

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

34
api/species_test.go Normal file
View file

@ -0,0 +1,34 @@
package api
import (
"testing"
"github.com/thermokarst/bactdb/models"
)
func TestSpecies_Get(t *testing.T) {
setup()
want := &models.Species{Id: 1, GenusId: 1, SpeciesName: "Test Species"}
calledGet := false
store.Species.(*models.MockSpeciesService).Get_ = func(id int64) (*models.Species, error) {
if id != want.Id {
t.Errorf("wanted request for species %d but got %d", want.Id, id)
}
calledGet = true
return want, nil
}
_, err := apiClient.Species.Get(want.Id)
if err != nil {
t.Fatal(err)
}
// if !calledGet {
// t.Error("!calledGet")
// }
// if !normalizeDeepEqual(want, got) {
// t.Errorf("got species %+v but wanted species %+v", got, want)
// }
}