-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
40 lines (30 loc) · 1.13 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
// A quick [smoke] test that verifies a single API route and uses real database (read).
// The test starts and terminates HTTP server automatically, i.e. no need to execute "go run main.go".
// More tests to verify API routes are kept in routers/routers_test.go file, they use mock DB.
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/nsavelyeva/go-shopping/models"
"github.com/nsavelyeva/go-shopping/routers"
"github.com/stretchr/testify/assert"
)
func TestFindItemRoute(t *testing.T) {
router := routers.Setup()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/items/1", nil)
router.ServeHTTP(w, req)
var want models.ItemResponse
var got models.ItemResponse
err := json.Unmarshal([]byte(`{"data":{"ID":1,"name":"Aladdin's lamp","price":999,"sold":true}}`), &want)
assert.Nil(t, err)
err = json.Unmarshal(w.Body.Bytes(), &got)
assert.Nil(t, err)
assert.Equal(t, 200, w.Code)
assert.Equal(t, want.Data.ID, got.Data.ID)
assert.Equal(t, want.Data.Name, got.Data.Name)
assert.Equal(t, want.Data.Price, got.Data.Price)
assert.Equal(t, want.Data.Sold, got.Data.Sold)
}