diff --git a/allsrv/server.go b/allsrv/server.go index d889d2b..579e1e9 100644 --- a/allsrv/server.go +++ b/allsrv/server.go @@ -45,12 +45,12 @@ import ( */ type Server struct { - db *inmemDB // 1) + db *InmemDB // 1) user, pass string // 3) } -func NewServer(db *inmemDB, user, pass string) *Server { +func NewServer(db *InmemDB, user, pass string) *Server { s := Server{ db: db, user: user, @@ -161,11 +161,12 @@ func (s *Server) delFoo(w http.ResponseWriter, r *http.Request) { } } -type inmemDB struct { +// InmemDB is an in-memory store. +type InmemDB struct { m []Foo // 12) } -func (db *inmemDB) createFoo(f Foo) (string, error) { +func (db *InmemDB) createFoo(f Foo) (string, error) { f.ID = uuid.Must(uuid.NewV4()).String() // 11) for _, existing := range db.m { @@ -179,7 +180,7 @@ func (db *inmemDB) createFoo(f Foo) (string, error) { return f.ID, nil } -func (db *inmemDB) readFoo(id string) (Foo, error) { +func (db *InmemDB) readFoo(id string) (Foo, error) { for _, f := range db.m { if id == f.ID { return f, nil @@ -188,7 +189,7 @@ func (db *inmemDB) readFoo(id string) (Foo, error) { return Foo{}, errors.New("foo not found for id: " + id) // 8) } -func (db *inmemDB) updateFoo(f Foo) error { +func (db *InmemDB) updateFoo(f Foo) error { for i, existing := range db.m { if f.ID == existing.ID { db.m[i] = f @@ -198,7 +199,7 @@ func (db *inmemDB) updateFoo(f Foo) error { return errors.New("foo not found for id: " + f.ID) // 8) } -func (db *inmemDB) delFoo(id string) error { +func (db *InmemDB) delFoo(id string) error { for i, f := range db.m { if id == f.ID { db.m = append(db.m[:i], db.m[i+1:]...) diff --git a/allsrv/server_test.go b/allsrv/server_test.go new file mode 100644 index 0000000..c87201f --- /dev/null +++ b/allsrv/server_test.go @@ -0,0 +1,66 @@ +package allsrv_test + +import ( + "bytes" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/jsteenb2/mess/allsrv" +) + +func TestServer(t *testing.T) { + t.Run("foo create", func(t *testing.T) { + t.Run("when provided a valid foo should pass", func(t *testing.T) { + db := new(allsrv.InmemDB) + svr := allsrv.NewServer(db, "dodgers@stink.com", "PaSsWoRd") + + req := httptest.NewRequest("POST", "/foo", newJSONBody(t, allsrv.Foo{ + Name: "first-foo", + Note: "some note", + })) + req.SetBasicAuth("dodgers@stink.com", "PaSsWoRd") + rec := httptest.NewRecorder() + + svr.ServeHTTP(rec, req) + + assert.Equal(t, http.StatusCreated, rec.Code) + expectJSONBody(t, rec.Body, func(t *testing.T, got allsrv.Foo) { + assert.NotZero(t, got.ID) + got.ID = "" // this hurts :-( + + want := allsrv.Foo{ + ID: "", // ruh ohhh + Name: "first-foo", + Note: "some note", + } + assert.Equal(t, want, got) + }) + }) + }) +} + +func newJSONBody(t *testing.T, v any) *bytes.Buffer { + t.Helper() + + var buf bytes.Buffer + err := json.NewEncoder(&buf).Encode(v) + require.NoError(t, err) + + return &buf +} + +func expectJSONBody[T any](t *testing.T, r io.Reader, assertFn func(t *testing.T, got T)) { + t.Helper() + + var out T + err := json.NewDecoder(r).Decode(&out) + require.NoError(t, err) + + assertFn(t, out) +} diff --git a/go.mod b/go.mod index bd4bf8f..7b40841 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,13 @@ module github.com/jsteenb2/mess go 1.22 -require github.com/gofrs/uuid v4.4.0+incompatible +require ( + github.com/gofrs/uuid v4.4.0+incompatible + github.com/stretchr/testify v1.8.4 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum index c0ad687..f8b8f5e 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,12 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA= github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=