Skip to content

Commit

Permalink
chore(allsrv): add tests for create foo endpoint
Browse files Browse the repository at this point in the history
We are up against the wall with the UUID generation
hardcoded in the db create. This makes the test
non-deterministic.

Question: How do we make UUID generation determinstic?
  • Loading branch information
jsteenb2 committed Jul 5, 2024
1 parent 733884c commit e9374a0
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 8 deletions.
15 changes: 8 additions & 7 deletions allsrv/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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:]...)
Expand Down
66 changes: 66 additions & 0 deletions allsrv/server_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
11 changes: 10 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=

0 comments on commit e9374a0

Please sign in to comment.