This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers_test.go
308 lines (286 loc) · 11 KB
/
handlers_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/apdforward/apdf_api/models"
"github.com/gorilla/mux"
)
type Pages struct {
Message []int
}
type mockDB struct{}
func (mdb *mockDB) AllParagraphs(lang interface{}) ([]*models.Paragraph, error) {
ps := make([]*models.Paragraph, 0)
ps = append(ps, &models.Paragraph{
ID: 42,
ParagraphNumber: 42,
ParagraphTitle: "test",
ParagraphText: "test",
})
return ps, nil
}
func (mdb *mockDB) GetParagraph(lang interface{}, paragraph models.Paragraph) (*models.Paragraph, error) {
p := &models.Paragraph{}
if paragraph.ID == 14 {
p.ID = 14
p.ParagraphNumber = 14
p.ParagraphTitle = "test"
p.ParagraphText = "test"
return p, nil
}
err := errors.New("No such paragraph")
return nil, err
}
func (mdb *mockDB) GetParagraphsByCategoryTag(lang interface{}, categoryTag models.CategoryTag) ([]*models.Paragraph, error) {
ps := make([]*models.Paragraph, 0)
if categoryTag.ID == 1 {
ps = append(ps, &models.Paragraph{
ID: 42,
ParagraphNumber: 42,
ParagraphTitle: "test",
ParagraphText: "test",
})
return ps, nil
}
err := errors.New("No such paragraph")
return nil, err
}
func (mdb *mockDB) GetParagraphsBySpecificTag(lang interface{}, specificTag models.SpecificTag) ([]*models.Paragraph, error) {
ps := make([]*models.Paragraph, 0)
if specificTag.ID == 1 {
ps = append(ps, &models.Paragraph{
ID: 42,
ParagraphNumber: 42,
ParagraphTitle: "test",
ParagraphText: "test",
})
return ps, nil
}
err := errors.New("No such paragraph")
return nil, err
}
func (mdb *mockDB) AllCompliances(lang interface{}) ([]*models.Compliance, error) {
cs := make([]*models.Compliance, 0)
pages := []int{14, 15}
byte1, err := json.Marshal(pages)
if err != nil {
fmt.Println(err)
}
raw := json.RawMessage(byte1)
cs = append(cs, &models.Compliance{
ReportID: 2,
ParagraphID: 3,
PrimaryCompliance: "In Compliance",
SecondaryCompliance: "Not In Compliance",
OperationCompliance: "Not In Compliance",
Pages: raw,
})
return cs, nil
}
func (mdb *mockDB) GetCompliancesByParagraph(lang interface{}, paragraph models.Paragraph) ([]*models.Compliance, error) {
cs := make([]*models.Compliance, 0)
pages := []int{14, 15}
byte1, err := json.Marshal(pages)
if err != nil {
fmt.Println(err)
}
raw := json.RawMessage(byte1)
if paragraph.ID == 14 {
cs = append(cs, &models.Compliance{
ReportID: 2,
ParagraphID: 14,
PrimaryCompliance: "In Compliance",
SecondaryCompliance: "Not In Compliance",
OperationCompliance: "Not In Compliance",
Pages: raw,
})
return cs, nil
}
err = errors.New("No such compliances")
return nil, err
}
func (mdb *mockDB) GetCompliancesByReport(lang interface{}, report models.Report) ([]*models.Compliance, error) {
cs := make([]*models.Compliance, 0)
pages := []int{14, 15}
byte1, err := json.Marshal(pages)
if err != nil {
fmt.Println(err)
}
raw := json.RawMessage(byte1)
if report.ID == 2 {
cs = append(cs, &models.Compliance{
ReportID: 2,
ParagraphID: 14,
PrimaryCompliance: "In Compliance",
SecondaryCompliance: "Not In Compliance",
OperationCompliance: "Not In Compliance",
Pages: raw,
})
return cs, nil
}
err = errors.New("No such compliances")
return nil, err
}
func (mdb *mockDB) AllReports(lang interface{}) ([]*models.Report, error) {
rpts := make([]*models.Report, 0)
rpts = append(rpts, &models.Report{
ID: 1,
ReportName: "IMR-1",
ReportTitle: "Monitor's First Report",
PublishDate: "2015-11-23",
PeriodBegin: "2015-02-01",
PeriodEnd: "2015-05-31",
})
return rpts, nil
}
func (mdb *mockDB) GetReport(lang interface{}, report models.Report) (*models.Report, error) {
rpt := &models.Report{}
if report.ID == 1 {
rpt.ID = 1
rpt.ReportName = "IMR-1"
rpt.ReportTitle = "Monitor's First Report"
rpt.PublishDate = "2015-11-23"
rpt.PeriodBegin = "2015-02-01"
rpt.PeriodEnd = "2015-05-31"
return rpt, nil
}
err := errors.New("No such report")
return nil, err
}
func (mdb *mockDB) AllCategoryTags(lang interface{}) ([]*models.CategoryTag, error) {
cts := make([]*models.CategoryTag, 0)
cts = append(cts, &models.CategoryTag{
ID: 1,
Value: "I. Use of Force",
})
return cts, nil
}
func (mdb *mockDB) GetCategoryTag(lang interface{}, categoryTag models.CategoryTag) (*models.CategoryTag, error) {
ct := &models.CategoryTag{}
if categoryTag.ID == 1 {
ct.ID = 1
ct.Value = "I. Use of Force"
return ct, nil
}
err := errors.New("No such category tag")
return nil, err
}
func (mdb *mockDB) GetCategoryTagsByParagraph(lang interface{}, paragraph models.Paragraph) ([]*models.CategoryTag, error) {
cts := make([]*models.CategoryTag, 0)
if paragraph.ID == 14 {
cts = append(cts, &models.CategoryTag{
ID: 1,
Value: "I. Use of Force",
})
return cts, nil
}
err := errors.New("invalid paragraph")
return nil, err
}
func (mdb *mockDB) AllSpecificTags(lang interface{}) ([]*models.SpecificTag, error) {
sts := make([]*models.SpecificTag, 0)
sts = append(sts, &models.SpecificTag{
ID: 1,
Value: "Use of Force Principles",
CategoryID: 1,
})
return sts, nil
}
func (mdb *mockDB) GetSpecificTag(lang interface{}, specificTag models.SpecificTag) (*models.SpecificTag, error) {
st := &models.SpecificTag{}
if specificTag.ID == 1 {
st.ID = 1
st.Value = "Use of Force Principles"
st.CategoryID = 1
return st, nil
}
err := errors.New("No such specific tag")
return nil, err
}
func (mdb *mockDB) GetSpecificTagsByParagraph(lang interface{}, paragraph models.Paragraph) ([]*models.SpecificTag, error) {
sts := make([]*models.SpecificTag, 0)
if paragraph.ID == 14 {
sts = append(sts, &models.SpecificTag{
ID: 1,
Value: "Use of Force Principles",
CategoryID: 1,
})
return sts, nil
}
err := errors.New("invalid paragraph")
return nil, err
}
func TestHandlers(t *testing.T) {
router := mux.NewRouter()
router.Use(handleKey)
env := Env{db: &mockDB{}}
router.HandleFunc("/paragraphs", env.paragraphs)
router.HandleFunc("/paragraphs/{key}", env.paragraph)
router.HandleFunc("/paragraphs/{key}/category-tags", env.categoryTagsByParagraph)
router.HandleFunc("/paragraphs/{key}/specific-tags", env.specificTagsByParagraph)
router.HandleFunc("/paragraphs/{key}/compliances", env.compliancesByParagraph)
router.HandleFunc("/compliances", env.compliances)
router.HandleFunc("/reports", env.reports)
router.HandleFunc("/reports/{key}", env.report)
router.HandleFunc("/reports/{key}/compliances", env.compliancesByReport)
router.HandleFunc("/category-tags", env.categoryTags)
router.HandleFunc("/category-tags/{key}", env.categoryTag)
router.HandleFunc("/category-tags/{key}/paragraphs", env.paragraphsByCategoryTag)
router.HandleFunc("/specific-tags", env.specificTags)
router.HandleFunc("/specific-tags/{key}", env.specificTag)
router.HandleFunc("/specific-tags/{key}/paragraphs", env.paragraphsBySpecificTag)
tests := []struct {
description string
Code int
URL string
expected string
}{
{"all paragraphs", 200, "/paragraphs", "{\"data\":[{\"id\":42,\"paragraphNumber\":42,\"paragraphTitle\":\"test\",\"paragraphText\":\"test\"}]}"},
{"paragraph by key", 200, "/paragraphs/14", "{\"data\":{\"id\":14,\"paragraphNumber\":14,\"paragraphTitle\":\"test\",\"paragraphText\":\"test\"}}"},
{"invalid paragraph key", 404, "/paragraphs/13", ""},
{"paragraphs by category tag", 200, "/category-tags/1/paragraphs", "{\"data\":[{\"id\":42,\"paragraphNumber\":42,\"paragraphTitle\":\"test\",\"paragraphText\":\"test\"}]}"},
{"paragraphs by invalid category tag", 404, "/category-tags/13/paragraphs", ""},
{"paragraphs by specific tag", 200, "/specific-tags/1/paragraphs", "{\"data\":[{\"id\":42,\"paragraphNumber\":42,\"paragraphTitle\":\"test\",\"paragraphText\":\"test\"}]}"},
{"paragraphs by invalid specific tag", 404, "/specific-tags/13/paragraphs", ""},
{"specific tags by invalid paragraph", 404, "/paragraphs/13/specifictags", ""},
{"all compliances", 200, "/compliances", "{\"data\":[{\"reportId\":2,\"paragraphId\":3,\"primaryCompliance\":\"In Compliance\",\"operationalCompliance\":\"Not In Compliance\",\"secondaryCompliance\":\"Not In Compliance\",\"pages\":[14,15]}]}"},
{"invalid compliances key", 404, "/compliances/42", ""},
{"compliances by paragraph", 200, "/paragraphs/14/compliances", "{\"data\":[{\"reportId\":2,\"paragraphId\":14,\"primaryCompliance\":\"In Compliance\",\"operationalCompliance\":\"Not In Compliance\",\"secondaryCompliance\":\"Not In Compliance\",\"pages\":[14,15]}]}"},
{"compliances by invalid paragraph", 404, "/paragraphs/13/compliances", ""},
{"compliances by report", 200, "/reports/2/compliances", "{\"data\":[{\"reportId\":2,\"paragraphId\":14,\"primaryCompliance\":\"In Compliance\",\"operationalCompliance\":\"Not In Compliance\",\"secondaryCompliance\":\"Not In Compliance\",\"pages\":[14,15]}]}"},
{"compliances by invalid report", 404, "/reports/1/compliances", ""},
{"all reports", 200, "/reports", "{\"data\":[{\"id\":1,\"reportName\":\"IMR-1\",\"reportTitle\":\"Monitor's First Report\",\"publishDate\":\"2015-11-23\",\"periodBegin\":\"2015-02-01\",\"periodEnd\":\"2015-05-31\"}]}"},
{"report by key", 200, "/reports/1", "{\"data\":{\"id\":1,\"reportName\":\"IMR-1\",\"reportTitle\":\"Monitor's First Report\",\"publishDate\":\"2015-11-23\",\"periodBegin\":\"2015-02-01\",\"periodEnd\":\"2015-05-31\"}}"},
{"invalid report key", 404, "/reports/42", ""},
{"all category tags", 200, "/category-tags", "{\"data\":[{\"id\":1,\"value\":\"I. Use of Force\"}]}"},
{"category tag by key", 200, "/category-tags/1", "{\"data\":{\"id\":1,\"value\":\"I. Use of Force\"}}"},
{"invalid category tag key", 404, "/category-tags/42", ""},
{"category tags by paragraph", 200, "/paragraphs/14/category-tags", "{\"data\":[{\"id\":1,\"value\":\"I. Use of Force\"}]}"},
{"category tags by invalid paragraph", 404, "/paragraphs/13/category-tags", ""},
{"all specific tags", 200, "/specific-tags", "{\"data\":[{\"id\":1,\"value\":\"Use of Force Principles\",\"categoryId\":1}]}"},
{"specific tag by key", 200, "/specific-tags/1", "{\"data\":{\"id\":1,\"value\":\"Use of Force Principles\",\"categoryId\":1}}"},
{"invalid specific tag key", 404, "/specific-tags/42", ""},
{"specific tags by paragraph", 200, "/paragraphs/14/specific-tags", "{\"data\":[{\"id\":1,\"value\":\"Use of Force Principles\",\"categoryId\":1}]}"},
{"specific tags by invalid paragraph", 404, "/paragraphs/13/specific-tags", ""},
}
for _, test := range tests {
rr := httptest.NewRecorder()
req, err := http.NewRequest("GET", test.URL, nil)
if err != nil {
t.Fatal(err)
}
router.ServeHTTP(rr, req)
if test.Code != rr.Code {
t.Errorf("\n%v\nhandler returned wrong status code: got %v want %v",
test.description, rr.Code, test.Code)
}
if test.expected != rr.Body.String() && rr.Code == http.StatusOK {
t.Errorf("\n%v\n...expected = %v\n...obtained = %v",
test.description, test.expected, rr.Body.String())
}
}
}