Skip to content

Commit

Permalink
chore(allsvr): panic trying to add a test for the read API
Browse files Browse the repository at this point in the history
What's going on here? Take a moment to reflect on this. What is the simplest
possible fix here?
  • Loading branch information
jsteenb2 committed Jul 5, 2024
1 parent 1a50c7e commit efa38f4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
4 changes: 2 additions & 2 deletions allsrv/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (s *Server) createFoo(w http.ResponseWriter, r *http.Request) {

f.ID = s.idFn() // 11)

if err := s.db.createFoo(f); err != nil {
if err := s.db.CreateFoo(f); err != nil {
w.WriteHeader(http.StatusInternalServerError) // 9)
return
}
Expand Down Expand Up @@ -178,7 +178,7 @@ type InmemDB struct {
m []Foo // 12)
}

func (db *InmemDB) createFoo(f Foo) error {
func (db *InmemDB) CreateFoo(f Foo) error {
for _, existing := range db.m {
if f.Name == existing.Name {
return errors.New("foo " + f.Name + " exists") // 8)
Expand Down
48 changes: 39 additions & 9 deletions allsrv/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/jsteenb2/mess/allsrv"
)

Expand All @@ -21,16 +21,16 @@ func TestServer(t *testing.T) {
svr := allsrv.NewServer(db, "[email protected]", "PaSsWoRd", allsrv.WithIDFn(func() string {
return "id1"
}))

req := httptest.NewRequest("POST", "/foo", newJSONBody(t, allsrv.Foo{
Name: "first-foo",
Note: "some note",
}))
req.SetBasicAuth("[email protected]", "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) {
want := allsrv.Foo{
Expand All @@ -42,24 +42,54 @@ func TestServer(t *testing.T) {
})
})
})

t.Run("foo read", func(t *testing.T) {
t.Run("when querying for existing foo id should pass", func(t *testing.T) {
db := new(allsrv.InmemDB)
err := db.CreateFoo(allsrv.Foo{
ID: "reader1",
Name: "read",
Note: "another note",
})
require.NoError(t, err)

svr := allsrv.NewServer(db, "[email protected]", "PaSsWoRd")

req := httptest.NewRequest("GET", "/foo?id=reader1", nil)
req.SetBasicAuth("[email protected]", "PaSsWoRd")
rec := httptest.NewRecorder()

svr.ServeHTTP(rec, req)

assert.Equal(t, http.StatusOK, rec.Code)
expectJSONBody(t, rec.Body, func(t *testing.T, got allsrv.Foo) {
want := allsrv.Foo{
ID: "reader1",
Name: "read",
Note: "another 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)
}

0 comments on commit efa38f4

Please sign in to comment.