-
Notifications
You must be signed in to change notification settings - Fork 1
/
grotto_test.go
161 lines (129 loc) · 4.07 KB
/
grotto_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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"testing"
"net/http"
"net/http/httptest"
"github.com/labstack/echo"
"github.com/DATA-DOG/go-sqlmock"
)
func mockGrotto() (g *Grotto, m sqlmock.Sqlmock) {
db, m, _ := sqlmock.New()
g = &Grotto{
HTTP: echo.New(),
DB: db,
}
g.Route()
return
}
func mockServe(g *Grotto, request *http.Request) *httptest.ResponseRecorder {
recorder := httptest.NewRecorder()
g.HTTP.ServeHTTP(recorder, request)
return recorder
}
func TestReadResource(t *testing.T) {
g, m := mockGrotto()
defer g.DB.Close()
resource := "resources"
id := "1"
data := fmt.Sprintf(`{"id":%s,"key":"value"}`, id)
m.ExpectQuery(".*").
WithArgs(id).
WillReturnRows(sqlmock.NewRows([]string{"data"}).AddRow(data))
// Generate test request
r, _ := http.NewRequest("GET", fmt.Sprintf("/%s/%s", resource, id), nil)
response := mockServe(g, r)
expectResponseCode(t, 200, response.Code)
body := fmt.Sprintf(`{"data":%s}`, data)
expectResponseBody(t, body, response.Body)
if err := m.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expections: %s", err)
}
}
func TestReadNotFound(t *testing.T) {
g, m := mockGrotto()
defer g.DB.Close()
resource := "resources"
id := "2"
m.ExpectQuery(".*").
WithArgs(id).
WillReturnRows(sqlmock.NewRows([]string{"data"}))
// Generate test request
r, _ := http.NewRequest("GET", fmt.Sprintf("/%s/%s", resource, id), nil)
response := mockServe(g, r)
expectResponseRowNotFound(t, response)
if err := m.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expections: %s", err)
}
}
func testAllResources(t *testing.T) {
g, m := mockGrotto()
defer g.DB.Close()
resource := "resources"
data := fmt.Sprintf(`[{"id":1,"key":"value"},{"id":2,"key":"value2"}]`)
m.ExpectQuery(".*").
WillReturnRows(sqlmock.NewRows([]string{"data"}).AddRow(data))
// Generate test request
r, _ := http.NewRequest("GET", fmt.Sprintf("/%s", resource), nil)
response := mockServe(g, r)
expectResponseCode(t, 200, response.Code)
body := fmt.Sprintf(`{"data":%s}`, data)
expectResponseBody(t, body, response.Body)
if err := m.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expections: %s", err)
}
}
func testAllNotFound(t *testing.T) {
g, m := mockGrotto()
defer g.DB.Close()
resource := "resources"
m.ExpectQuery(".*").
WillReturnError(fmt.Errorf("sql: table not found"))
// Generate test request
r, _ := http.NewRequest("GET", fmt.Sprintf("/%s", resource), nil)
response := mockServe(g, r)
expectResponseTableNotFound(t, response)
if err := m.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expections: %s", err)
}
}
func testAllEmpty(t *testing.T) {
g, m := mockGrotto()
defer g.DB.Close()
resource := "resources"
m.ExpectQuery(".*").
WillReturnRows(sqlmock.NewRows([]string{"data"}))
// Generate test request
r, _ := http.NewRequest("GET", fmt.Sprintf("/%s", resource), nil)
response := mockServe(g, r)
expectResponseCode(t, 200, response.Code)
body := `{"data":[]}`
expectResponseBody(t, body, response.Body)
if err := m.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expections: %s", err)
}
}
func expectResponseTableNotFound(t *testing.T, r *httptest.ResponseRecorder) {
expectResponseCode(t, 404, r.Code)
body := `{"error":"Table not found."}`
expectResponseBody(t, body, r.Body)
}
func expectResponseRowNotFound(t *testing.T, r *httptest.ResponseRecorder) {
expectResponseCode(t, 404, r.Code)
body := `{"error":"Row not found."}`
expectResponseBody(t, body, r.Body)
}
func expectResponseBody(t *testing.T, expected string, data *bytes.Buffer) {
bs, _ := ioutil.ReadAll(data)
received := string(bs)
if expected != received {
t.Errorf("\nUnexpected response body:\n%s\n\nExpected:\n%s\n", received, expected)
}
}
func expectResponseCode(t *testing.T, expected int, received int) {
if expected != received {
t.Errorf("\nUnexpected response code: %d\nExpected %d\n", received, expected)
}
}