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

@ -16,8 +16,9 @@ import (
// A Client communicates with bactdb's HTTP API.
type Client struct {
Users UsersService
Genera GeneraService
Users UsersService
Genera GeneraService
Species SpeciesService
// BaseURL for HTTP requests to bactdb's API.
BaseURL *url.URL
@ -47,6 +48,7 @@ func NewClient(httpClient *http.Client) *Client {
}
c.Users = &usersService{c}
c.Genera = &generaService{c}
c.Species = &speciesService{c}
return c
}

69
models/species.go Normal file
View file

@ -0,0 +1,69 @@
package models
import (
"errors"
"strconv"
"time"
"github.com/thermokarst/bactdb/router"
)
// A Species is a high-level classifier in bactdb.
type Species struct {
Id int64 `json:"id,omitempty"`
GenusId int64 `json:"genus_id"`
SpeciesName string `json:"species_name"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt time.Time `json:"deleted_at"`
}
// SpeciesService interacts with the species-related endpoints in bactdb's API.
type SpeciesService interface {
// Get a species
Get(id int64) (*Species, error)
}
var (
ErrSpeciesNotFound = errors.New("species not found")
)
type speciesService struct {
client *Client
}
func (s *speciesService) Get(id int64) (*Species, error) {
// Pass in key value pairs as strings, sp that the gorilla mux URL generation is happy
strId := strconv.FormatInt(id, 10)
url, err := s.client.url(router.Species, map[string]string{"Id": strId}, nil)
if err != nil {
return nil, err
}
req, err := s.client.NewRequest("GET", url.String(), nil)
if err != nil {
return nil, err
}
var species *Species
_, err = s.client.Do(req, &species)
if err != nil {
return nil, err
}
return species, nil
}
type MockSpeciesService struct {
Get_ func(id int64) (*Species, error)
}
var _ SpeciesService = &MockSpeciesService{}
func (s *MockSpeciesService) Get(id int64) (*Species, error) {
if s.Get_ == nil {
return nil, nil
}
return s.Get_(id)
}

39
models/species_test.go Normal file
View file

@ -0,0 +1,39 @@
package models
import (
"net/http"
"reflect"
"testing"
"github.com/thermokarst/bactdb/router"
)
func TestSpeciesService_Get(t *testing.T) {
setup()
defer teardown()
want := &Species{Id: 1, GenusId: 1, SpeciesName: "Test Species"}
var called bool
mux.HandleFunc(urlPath(t, router.Species, map[string]string{"Id": "1"}), func(w http.ResponseWriter, r *http.Request) {
called = true
testMethod(t, r, "GET")
writeJSON(w, want)
})
species, err := client.Species.Get(1)
if err != nil {
t.Errorf("Species.Get returned error: %v", err)
}
if !called {
t.Fatal("!called")
}
normalizeTime(&want.CreatedAt, &want.UpdatedAt, &want.DeletedAt)
if !reflect.DeepEqual(species, want) {
t.Errorf("Species.Get returned %+v, want %+v", species, want)
}
}