Get a strain.
This commit is contained in:
parent
20d65bd561
commit
6c118f47f7
11 changed files with 267 additions and 0 deletions
|
@ -35,6 +35,8 @@ func Handler() *mux.Router {
|
|||
m.Get(router.UpdateSpecies).Handler(handler(serveUpdateSpecies))
|
||||
m.Get(router.DeleteSpecies).Handler(handler(serveDeleteSpecies))
|
||||
|
||||
m.Get(router.Strain).Handler(handler(serveStrain))
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
|
|
22
api/strains.go
Normal file
22
api/strains.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
func serveStrain(w http.ResponseWriter, r *http.Request) error {
|
||||
id, err := strconv.ParseInt(mux.Vars(r)["Id"], 10, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
strain, err := store.Strains.Get(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return writeJSON(w, strain)
|
||||
}
|
42
api/strains_test.go
Normal file
42
api/strains_test.go
Normal file
|
@ -0,0 +1,42 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/thermokarst/bactdb/models"
|
||||
)
|
||||
|
||||
func newStrain() *models.Strain {
|
||||
strain := models.NewStrain()
|
||||
strain.Id = 1
|
||||
strain.SpeciesId = 1
|
||||
return strain
|
||||
}
|
||||
|
||||
func TestStrain_Get(t *testing.T) {
|
||||
setup()
|
||||
|
||||
want := newStrain()
|
||||
|
||||
calledGet := false
|
||||
|
||||
store.Strains.(*models.MockStrainsService).Get_ = func(id int64) (*models.Strain, error) {
|
||||
if id != want.Id {
|
||||
t.Errorf("wanted request for strain %d but got %d", want.Id, id)
|
||||
}
|
||||
calledGet = true
|
||||
return want, nil
|
||||
}
|
||||
|
||||
got, err := apiClient.Strains.Get(want.Id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !calledGet {
|
||||
t.Error("!calledGet")
|
||||
}
|
||||
if !normalizeDeepEqual(want, got) {
|
||||
t.Errorf("got strain %+v but wanted strain %+v", got, want)
|
||||
}
|
||||
}
|
Reference in a new issue