-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_test.go
235 lines (208 loc) · 6.18 KB
/
search_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
package main
import (
"context"
"fmt"
"net/http"
"net/url"
"reflect"
"strings"
"testing"
)
type pageTestReulst struct {
printLastPage int
printMax int
}
func TestSearch_Page(t *testing.T) {
assert := func(result interface{}, want interface{}) {
if !reflect.DeepEqual(result, want) {
t.Errorf("Returned %+v, want %+v", result, want)
}
}
// pageTest: max, total, perPage
// Result: lastPage, max
// normal test
assert(pageTest(1000, 1000, 100), &pageTestReulst{10, 1000})
// total test
assert(pageTest(1000, 100, 100), &pageTestReulst{1, 100})
assert(pageTest(1000, 101, 100), &pageTestReulst{2, 101})
assert(pageTest(1000, 500, 100), &pageTestReulst{5, 500})
assert(pageTest(1000, 1, 100), &pageTestReulst{1, 1})
assert(pageTest(2000, 1000, 100), &pageTestReulst{10, 1000})
// max test
assert(pageTest(100, 1000, 100), &pageTestReulst{1, 100})
assert(pageTest(101, 1000, 100), &pageTestReulst{2, 101})
assert(pageTest(500, 1000, 100), &pageTestReulst{5, 500})
assert(pageTest(1, 1000, 100), &pageTestReulst{1, 1})
assert(pageTest(2000, 1000, 100), &pageTestReulst{10, 1000})
// perPage test
assert(pageTest(1000, 1000, 1), &pageTestReulst{1000, 1000})
assert(pageTest(1000, 1000, 2), &pageTestReulst{500, 1000})
assert(pageTest(1000, 1000, 10), &pageTestReulst{100, 1000})
assert(pageTest(1000, 1000, 50), &pageTestReulst{20, 1000})
assert(pageTest(1000, 1000, 1000), &pageTestReulst{1, 1000})
}
func pageTest(max int, total int, perPage int) *pageTestReulst {
Setup()
defer Teardown()
c := context.Background()
// create input
lastPage := ((total - 1) / perPage) + 1
url, _ := url.Parse(server.URL)
repo = NewRepo(NewSearch(c, option(max, perPage, url, "")))
// create output
mux.HandleFunc("/search/repositories", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Link", HeaderLink(perPage, lastPage))
fmt.Fprintf(w, `{"total_count": %d, "items": [{"id":1}]}`, total)
})
// test
_, printLastPage, printMax, _ := repo.search.First(c)
return &pageTestReulst{
printLastPage: printLastPage,
printMax: printMax,
}
}
func HeaderLink(perPage int, lastPage int) string {
link := func(per int, page int, rel string) string {
url := "https://api.github.com/search/repositories"
query := fmt.Sprintf("?q=ghs&per_page=%d&page=%d", per, page)
link := "<" + url + query + ">"
return link + "; " + fmt.Sprintf(`rel="%s"`, rel)
}
next := link(perPage, 2, "next")
last := link(perPage, lastPage, "last")
return "Link: " + next + ", " + last
}
func option(max int, perPage int, url *url.URL, token string) *SearchOpt {
return &SearchOpt{
sort: "best match",
order: "desc",
query: "ghs",
perPage: perPage,
max: max,
baseURL: url,
token: token,
}
}
type requestTestReulst struct {
repolen int
printMax int
}
func TestSearch_Request(t *testing.T) {
assert := func(result interface{}, want interface{}) {
if !reflect.DeepEqual(result, want) {
t.Errorf("Returned %+v, want %+v", result, want)
}
}
var handler func(w http.ResponseWriter, r *http.Request)
// Normal response
handler = func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"q": "ghs",
"sort": "best match",
"order": "desc",
"page": "1",
"per_page": "100",
})
var items []string
for i := 1; i < 100+1; i++ {
items = append(items, fmt.Sprintf(`{"id":%d}`, i))
}
fmt.Fprintf(w, `{"total_count": 102, "items": [%s]}`, strings.Join(items, ","))
}
ret, _ := firstRequestTest(t, handler)
assert(ret, &requestTestReulst{100, 102})
// Repository not found
handler = func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"q": "ghs",
"sort": "best match",
"order": "desc",
"page": "1",
"per_page": "100",
})
fmt.Fprintf(w, `{"total_count": 0, "items": []}`)
}
ret, _ = firstRequestTest(t, handler)
assert(ret, &requestTestReulst{0, 0})
// Invalid response
handler = func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"q": "ghs",
"sort": "best match",
"order": "desc",
"page": "1",
"per_page": "100",
})
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusNotFound)
}
ret, err := firstRequestTest(t, handler)
assert(strings.Contains(err.Error(), "404"), true)
assert(ret, &requestTestReulst{0, 0})
// Normal response
handler = func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"q": "ghs",
"sort": "best match",
"order": "desc",
"page": "2",
"per_page": "100",
})
fmt.Fprint(w, `{"total_count": 102, "items": [{"id":1},{"id":2}]}`)
}
repoNum, _ := secondRequestTest(t, handler)
assert(repoNum, 2)
// Invalid response
handler = func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testFormValues(t, r, values{
"q": "ghs",
"sort": "best match",
"order": "desc",
"page": "2",
"per_page": "100",
})
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusNotFound)
}
repoNum, err = secondRequestTest(t, handler)
assert(strings.Contains(err.Error(), "404"), true)
assert(repoNum, 0)
}
func firstRequestTest(t *testing.T, handler func(http.ResponseWriter, *http.Request)) (*requestTestReulst, error) {
Setup()
defer Teardown()
c := context.Background()
// create input
max := 1000
perPage := 100
url, _ := url.Parse(server.URL)
repo = NewRepo(NewSearch(c, option(max, perPage, url, "")))
// create output
mux.HandleFunc("/search/repositories", handler)
// test
repos, _, printMax, err := repo.search.First(c)
return &requestTestReulst{
repolen: len(repos),
printMax: printMax,
}, err
}
func secondRequestTest(t *testing.T, handler func(http.ResponseWriter, *http.Request)) (int, error) {
Setup()
defer Teardown()
c := context.Background()
// create input
max := 1000
perPage := 100
url, _ := url.Parse(server.URL)
repo = NewRepo(NewSearch(c, option(max, perPage, url, "abcdefg")))
// create output
mux.HandleFunc("/search/repositories", handler)
// test
repos, err := repo.search.Exec(c, 2)
return len(repos), err
}