-
Notifications
You must be signed in to change notification settings - Fork 12
/
main_test.go
164 lines (144 loc) · 4.16 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/digitalocean-apps/sample-with-database/pkg/storer"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
uuidRegex = regexp.MustCompile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-4[a-fA-F0-9]{3}-[8|9|aA|bB][a-fA-F0-9]{3}-[a-fA-F0-9]{12}$")
)
func TestNotesHandler(t *testing.T) {
tcs := []struct {
name string
requestMethod string
requestBody string
mock func(m sqlmock.Sqlmock)
expectedStatus int
expectedBody string
}{
{
name: "GET",
requestMethod: "GET",
expectedStatus: http.StatusOK,
expectedBody: "POST a note",
},
{
name: "POST empty",
requestMethod: "POST",
expectedStatus: http.StatusUnprocessableEntity,
expectedBody: "invalid empty note",
},
{
name: "POST with body",
requestMethod: "POST",
requestBody: "foobar",
mock: func(m sqlmock.Sqlmock) {
m.ExpectBegin()
m.ExpectQuery(regexp.QuoteMeta(`INSERT INTO "notes" ("created_at","updated_at","deleted_at","uuid","body") VALUES ($1,$2,$3,$4,$5) RETURNING "notes"."id"`)).
WithArgs(sqlmock.AnyArg(), sqlmock.AnyArg(), nil, sqlmock.AnyArg(), "foobar").
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow(1))
m.ExpectCommit()
},
expectedStatus: http.StatusOK,
expectedBody: uuidRegex.String(),
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
req, err := http.NewRequest(tc.requestMethod, "/", strings.NewReader(tc.requestBody))
require.NoError(t, err)
db, mock, err := sqlmock.New()
require.NoError(t, err)
gdb, err := gorm.Open("postgres", db)
require.NoError(t, err)
gdb.LogMode(true)
if tc.mock != nil {
tc.mock(mock)
}
storerClient := &storer.PG{
DB: gdb,
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(notesHandler(storerClient))
handler.ServeHTTP(rr, req)
assert.Equal(t, tc.expectedStatus, rr.Code)
r := regexp.MustCompile(tc.expectedBody)
body := rr.Body.String()
assert.True(t, r.MatchString(body), fmt.Sprintf("%s does not match %s", body, r.String()))
err = mock.ExpectationsWereMet()
require.NoError(t, err)
})
}
}
func TestNoteHandler(t *testing.T) {
tcs := []struct {
name string
noteID string
mock func(m sqlmock.Sqlmock)
expectedStatus int
expectedBody string
}{
{
name: "GET empty arg",
expectedStatus: http.StatusUnprocessableEntity,
expectedBody: "note_id required",
},
{
name: "GET not found",
expectedStatus: http.StatusNotFound,
noteID: "note-uuid",
mock: func(m sqlmock.Sqlmock) {
m.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "notes" WHERE "notes"."deleted_at" IS NULL AND ((uuid = $1)) LIMIT 1`)).
WithArgs("note-uuid").
WillReturnError(gorm.ErrRecordNotFound)
},
},
{
name: "GET found",
noteID: "note-uuid",
mock: func(m sqlmock.Sqlmock) {
m.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "notes" WHERE "notes"."deleted_at" IS NULL AND ((uuid = $1)) LIMIT 1`)).
WithArgs("note-uuid").
WillReturnRows(sqlmock.NewRows([]string{"uuid", "body"}).AddRow("note-uuid", "foobar"))
},
expectedStatus: http.StatusOK,
expectedBody: "foobar",
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
req, err := http.NewRequest("GET", fmt.Sprintf("/%s", "note-uuid"), nil)
require.NoError(t, err)
req = mux.SetURLVars(req, map[string]string{
"note_id": tc.noteID,
})
db, mock, err := sqlmock.New()
require.NoError(t, err)
gdb, err := gorm.Open("postgres", db)
require.NoError(t, err)
gdb.LogMode(true)
if tc.mock != nil {
tc.mock(mock)
}
storerClient := &storer.PG{
DB: gdb,
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(noteHandler(storerClient))
handler.ServeHTTP(rr, req)
assert.Equal(t, tc.expectedStatus, rr.Code)
assert.Equal(t, tc.expectedBody, rr.Body.String())
err = mock.ExpectationsWereMet()
require.NoError(t, err)
})
}
}