-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(allsrv): add tests for create foo endpoint
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
Showing
4 changed files
with
94 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |