Skip to content

Commit efa38f4

Browse files
committed
chore(allsvr): panic trying to add a test for the read API
What's going on here? Take a moment to reflect on this. What is the simplest possible fix here?
1 parent 1a50c7e commit efa38f4

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

allsrv/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (s *Server) createFoo(w http.ResponseWriter, r *http.Request) {
112112

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

115-
if err := s.db.createFoo(f); err != nil {
115+
if err := s.db.CreateFoo(f); err != nil {
116116
w.WriteHeader(http.StatusInternalServerError) // 9)
117117
return
118118
}
@@ -178,7 +178,7 @@ type InmemDB struct {
178178
m []Foo // 12)
179179
}
180180

181-
func (db *InmemDB) createFoo(f Foo) error {
181+
func (db *InmemDB) CreateFoo(f Foo) error {
182182
for _, existing := range db.m {
183183
if f.Name == existing.Name {
184184
return errors.New("foo " + f.Name + " exists") // 8)

allsrv/server_test.go

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"net/http"
88
"net/http/httptest"
99
"testing"
10-
10+
1111
"github.com/stretchr/testify/assert"
1212
"github.com/stretchr/testify/require"
13-
13+
1414
"github.com/jsteenb2/mess/allsrv"
1515
)
1616

@@ -21,16 +21,16 @@ func TestServer(t *testing.T) {
2121
svr := allsrv.NewServer(db, "dodgers@stink.com", "PaSsWoRd", allsrv.WithIDFn(func() string {
2222
return "id1"
2323
}))
24-
24+
2525
req := httptest.NewRequest("POST", "/foo", newJSONBody(t, allsrv.Foo{
2626
Name: "first-foo",
2727
Note: "some note",
2828
}))
2929
req.SetBasicAuth("dodgers@stink.com", "PaSsWoRd")
3030
rec := httptest.NewRecorder()
31-
31+
3232
svr.ServeHTTP(rec, req)
33-
33+
3434
assert.Equal(t, http.StatusCreated, rec.Code)
3535
expectJSONBody(t, rec.Body, func(t *testing.T, got allsrv.Foo) {
3636
want := allsrv.Foo{
@@ -42,24 +42,54 @@ func TestServer(t *testing.T) {
4242
})
4343
})
4444
})
45+
46+
t.Run("foo read", func(t *testing.T) {
47+
t.Run("when querying for existing foo id should pass", func(t *testing.T) {
48+
db := new(allsrv.InmemDB)
49+
err := db.CreateFoo(allsrv.Foo{
50+
ID: "reader1",
51+
Name: "read",
52+
Note: "another note",
53+
})
54+
require.NoError(t, err)
55+
56+
svr := allsrv.NewServer(db, "dodgers@stink.com", "PaSsWoRd")
57+
58+
req := httptest.NewRequest("GET", "/foo?id=reader1", nil)
59+
req.SetBasicAuth("dodgers@stink.com", "PaSsWoRd")
60+
rec := httptest.NewRecorder()
61+
62+
svr.ServeHTTP(rec, req)
63+
64+
assert.Equal(t, http.StatusOK, rec.Code)
65+
expectJSONBody(t, rec.Body, func(t *testing.T, got allsrv.Foo) {
66+
want := allsrv.Foo{
67+
ID: "reader1",
68+
Name: "read",
69+
Note: "another note",
70+
}
71+
assert.Equal(t, want, got)
72+
})
73+
})
74+
})
4575
}
4676

4777
func newJSONBody(t *testing.T, v any) *bytes.Buffer {
4878
t.Helper()
49-
79+
5080
var buf bytes.Buffer
5181
err := json.NewEncoder(&buf).Encode(v)
5282
require.NoError(t, err)
53-
83+
5484
return &buf
5585
}
5686

5787
func expectJSONBody[T any](t *testing.T, r io.Reader, assertFn func(t *testing.T, got T)) {
5888
t.Helper()
59-
89+
6090
var out T
6191
err := json.NewDecoder(r).Decode(&out)
6292
require.NoError(t, err)
63-
93+
6494
assertFn(t, out)
6595
}

0 commit comments

Comments
 (0)