-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.go
More file actions
267 lines (215 loc) · 6.3 KB
/
Copy pathapi_test.go
File metadata and controls
267 lines (215 loc) · 6.3 KB
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
package main
import (
"bytes"
"encoding/json"
"github.com/opinionated/debugServer/debugAPI"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func getHTTPResponse(url string) *httptest.ResponseRecorder {
recorder := httptest.NewRecorder()
req, _ := http.NewRequest("GET", url, nil)
apiHandler().ServeHTTP(recorder, req)
return recorder
}
func postHTTPResponse(url, body string) *httptest.ResponseRecorder {
recorder := httptest.NewRecorder()
req, _ := http.NewRequest("POST", url, strings.NewReader(body))
apiHandler().ServeHTTP(recorder, req)
return recorder
}
// makes a simple test article
// store everything in a json map to make it easier
func makeArticle(title string, body string,
related []map[string]interface{}) map[string]interface{} {
article := make(map[string]interface{})
article["Title"] = title
article["Body"] = body
if related != nil {
article["Related"] = related
}
return article
}
// encodes to json then converts to string
func setToString(set map[string]interface{}) (string, error) {
var buffer bytes.Buffer
encoder := json.NewEncoder(&buffer)
err := encoder.Encode(set)
if err != nil {
return "", err
}
return buffer.String(), nil
}
var testSet = struct {
guns map[string]interface{}
god map[string]interface{}
}{
guns: makeArticle("Guns", "guns r great",
[]map[string]interface{}{
makeArticle("Guns good", "shoot stuff", nil),
makeArticle("Stop guns", "stop em", nil),
}),
god: makeArticle("God", "separate church and state",
[]map[string]interface{}{
makeArticle("Abortions bad", "god said so!", nil),
makeArticle("America christian", "one nation under god", nil),
}),
}
func addTestSet(t *testing.T) {
clearArticles()
// add sets
gunstr, err := setToString(testSet.guns)
assert.Nil(t, err)
resp := postHTTPResponse("/add", gunstr)
assert.Equal(t, 200, resp.Code)
assert.Equal(t, "", resp.Body.String())
godstr, err := setToString(testSet.god)
assert.Nil(t, err)
resp = postHTTPResponse("/add", godstr)
assert.Equal(t, 200, resp.Code)
assert.Equal(t, "", resp.Body.String())
assert.Equal(t, 2, cache.count)
}
func clearArticles() {
cache.clear()
}
func TestAddArticle(t *testing.T) {
clearArticles()
str, err := setToString(testSet.guns)
assert.Nil(t, err)
resp := postHTTPResponse("/add", str)
assert.Equal(t, 200, resp.Code)
assert.Equal(t, "", resp.Body.String())
// now go test the body
assert.Equal(t, 1, cache.count)
assert.NotNil(t, cache.start)
article := cache.start.article
assert.Equal(t, article.Title, "Guns")
assert.Len(t, article.Related, 2)
assert.Equal(t, article.Body, testSet.guns["Body"])
}
func TestAddToPop(t *testing.T) {
clearArticles()
str, err := setToString(testSet.guns)
assert.Nil(t, err)
for i := 0; i < cache.limit+1; i++ {
resp := postHTTPResponse("/add", str)
assert.Equal(t, 200, resp.Code)
assert.Equal(t, "", resp.Body.String())
}
}
func TestGetArticles(t *testing.T) {
addTestSet(t)
resp := getHTTPResponse("/frontpage")
assert.Equal(t, 200, resp.Code)
assert.NotNil(t, resp.Body)
var articles []debugAPI.GenericArticle
err := json.Unmarshal(resp.Body.Bytes(), &articles)
assert.Nil(t, err)
assert.Len(t, articles, 2)
assert.Equal(t, "God", articles[0].Title)
assert.Equal(t, "Guns", articles[1].Title)
assert.Nil(t, articles[0].Related)
assert.Nil(t, articles[1].Related)
}
func TestGetArticle(t *testing.T) {
addTestSet(t)
resp := getHTTPResponse("/article/Guns")
assert.Equal(t, 200, resp.Code)
assert.NotNil(t, resp.Body)
var article debugAPI.GenericArticle
err := json.Unmarshal(resp.Body.Bytes(), &article)
assert.Nil(t, err)
assert.Equal(t, testSet.guns["Body"], article.Body)
assert.Len(t, article.Related, 2)
assert.Equal(t, "Guns good", article.Related[0].Title)
assert.Equal(t, "Stop guns", article.Related[1].Title)
}
func TestGetRelated(t *testing.T) {
addTestSet(t)
resp := getHTTPResponse("/article/Guns good")
assert.Equal(t, 200, resp.Code)
assert.NotNil(t, resp.Body)
if _, ok := cache.titleMap["Guns good"]; !ok {
t.Error("Expected article, but could not find!")
t.Error("list is:", cache.titleMap)
}
var article debugAPI.GenericArticle
err := json.Unmarshal(resp.Body.Bytes(), &article)
if !assert.Nil(t, err) {
t.Error("json body is:", resp.Body.String())
}
assert.Equal(t, "shoot stuff", article.Body)
assert.Len(t, article.Related, 0)
}
func TestAddDebug(t *testing.T) {
guns := testSet.guns
guns["Title"] = "debug"
guns["DebugInfo"] = map[string]interface{}{
"Score": 1.0,
"Taxonomy": map[string]interface{}{
"a": 1,
"b": 2,
},
}
str, err := setToString(guns)
assert.Nil(t, err)
// add our article with debug info up to the server
clearArticles()
resp := postHTTPResponse("/add", str)
assert.NotNil(t, resp.Body)
assert.Equal(t, "", resp.Body.String())
resp = getHTTPResponse("/article/debug")
assert.NotNil(t, resp.Body)
var data map[string]interface{}
err = json.Unmarshal(resp.Body.Bytes(), &data)
assert.Nil(t, err)
// pull the debug info out of the json
rawDebug, ok := data["DebugInfo"]
assert.True(t, ok)
debugInfo, ok := rawDebug.(map[string]interface{})
assert.True(t, ok)
// check that the sub info exists
assert.Equal(t, 1.0, debugInfo["Score"])
taxonomy, ok := debugInfo["Taxonomy"].(map[string]interface{})
assert.True(t, ok)
assert.Equal(t, 1.0, taxonomy["a"])
assert.Equal(t, 2.0, taxonomy["b"])
}
func TestDoubleRemove(t *testing.T) {
// get adding a related article twice, then bumping the first occurance
clearArticles()
cache.push(buildDebug("a", "a", []debugAPI.GenericArticle{
buildDebug("popular", "", nil),
}))
spam := buildDebug("s", "s", []debugAPI.GenericArticle{
buildDebug("other", "", nil),
})
// fill up the cache part of the way
for i := 0; i < 6; i++ {
cache.push(spam)
}
// double add the article "popular"
cache.push(buildDebug("b", "b", []debugAPI.GenericArticle{
buildDebug("popular", "", nil),
}))
// fill the cache the rest of the way
for i := 0; i < 5; i++ {
cache.push(spam)
}
_, ok := cache.articleByTitle("popular")
assert.True(t, ok)
_, ok = cache.articleByTitle("a")
assert.False(t, ok)
}
func TestClear(t *testing.T) {
addTestSet(t)
assert.Equal(t, 2, cache.count)
resp := postHTTPResponse("/clear", "")
assert.Equal(t, 200, resp.Code)
assert.Equal(t, "", resp.Body.String())
assert.Equal(t, 0, cache.count)
}