-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccountsfilterhandler_test.go
387 lines (320 loc) · 11.1 KB
/
accountsfilterhandler_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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"regexp"
"strconv"
"strings"
"testing"
"time"
"gopkg.in/oleiade/reflections.v1"
"github.com/stretchr/testify/assert"
)
type ResponseAccount Account
type Response struct {
Accounts []ResponseAccount
}
func (account *ResponseAccount) GetField(field string) *string {
actualValue, err := reflections.GetField(account, field)
if err != nil {
panic(err)
}
return actualValue.(*string)
}
func TestLimit(t *testing.T) {
cases := []int{10, 50}
for _, limit := range cases {
t.Run(fmt.Sprint("Limit=", limit), func(t *testing.T) {
teardown, storage := SetupTest(t)
defer teardown()
handler := AccountsFilterHandler{
storage: storage,
}
request, _ := http.NewRequest("GET", fmt.Sprint("accounts/filter?sex_eq=m&limit=", limit), nil)
responseRecorder := httptest.NewRecorder()
handler.ServeHTTP(responseRecorder, request)
assert.Equal(t, http.StatusOK, responseRecorder.Code)
response := Response{}
err := json.Unmarshal(responseRecorder.Body.Bytes(), &response)
assert.Nil(t, err, err)
assert.NotEmpty(t, response)
assert.NotEmpty(t, response.Accounts)
assert.Equal(t, limit, len(response.Accounts))
})
}
}
func TestAreAccountsSorted(t *testing.T) {
teardown, storage := SetupTest(t)
defer teardown()
handler := AccountsFilterHandler{
storage: storage,
}
request, _ := http.NewRequest("GET", "accounts/filter?sex_eq=m&limit=100", nil)
responseRecorder := httptest.NewRecorder()
handler.ServeHTTP(responseRecorder, request)
assert.Equal(t, http.StatusOK, responseRecorder.Code)
response := Response{}
err := json.Unmarshal(responseRecorder.Body.Bytes(), &response)
assert.Nil(t, err, err)
assert.NotEmpty(t, response)
assert.NotEmpty(t, response.Accounts)
prev := response.Accounts[0].Id
for _, account := range response.Accounts {
assert.True(t, *prev >= *account.Id)
prev = account.Id
}
}
func TestFilterFiledsInResponse(t *testing.T) {
requiredFields := []string{"id", "email"}
cases := map[string]([]string){
"sex_eq=f": append(requiredFields, []string{"sex"}...),
}
for query, fields := range cases {
t.Run(query, func(t *testing.T) {
teardown, storage := SetupTest(t)
defer teardown()
handler := AccountsFilterHandler{
storage: storage,
}
request, _ := http.NewRequest("GET", fmt.Sprint("accounts/filter?limit=10&", query), nil)
responseRecorder := httptest.NewRecorder()
handler.ServeHTTP(responseRecorder, request)
assert.Equal(t, http.StatusOK, responseRecorder.Code)
response := map[string]interface{}{}
err := json.Unmarshal(responseRecorder.Body.Bytes(), &response)
assert.Nil(t, err, err)
assert.NotEmpty(t, response)
accounts := response["accounts"].([]interface{})
account := accounts[0].(map[string]interface{})
for k := range account {
assert.Contains(t, fields, k)
}
})
}
}
func TestBadRequests(t *testing.T) {
cases := []string{
"",
"limit=",
"limit=f",
"limit=-10",
"limit=111f",
"fname_bad=aaa",
}
for _, query := range cases {
t.Run(query, func(t *testing.T) {
teardown, storage := SetupTest(t)
defer teardown()
handler := AccountsFilterHandler{
storage: storage,
}
request, _ := http.NewRequest("GET", fmt.Sprint("accounts/filter?", query), nil)
responseRecorder := httptest.NewRecorder()
handler.ServeHTTP(responseRecorder, request)
assert.Equal(t, http.StatusBadRequest, responseRecorder.Code)
})
}
}
func TestFilter(t *testing.T) {
emailDomainAssert := func(t *testing.T, account ResponseAccount, value string) {
ok, err := regexp.MatchString(
fmt.Sprint("^.*@", regexp.QuoteMeta(value), "$"),
*account.Email,
)
assert.NoError(t, err)
assert.True(t, ok)
}
phoneCodeAssert := func(t *testing.T, account ResponseAccount, value string) {
ok, err := regexp.MatchString(
regexp.QuoteMeta(fmt.Sprint("(", value, ")")),
*account.Phone,
)
assert.NoError(t, err)
assert.True(t, ok)
}
birthYearAssert := func(t *testing.T, account ResponseAccount, value string) {
actualTime := time.Unix(int64(*account.Birth), 0)
expectedYear, _ := strconv.Atoi(value)
assert.Equal(t, expectedYear, actualTime.Year())
}
premiumAssert := func(t *testing.T, account ResponseAccount, value string) {
now := time.Now()
start := time.Unix(int64((*account.Premium).Start), 0)
finish := time.Unix(int64((*account.Premium).Finish), 0)
assert.True(t, start.Before(now))
assert.True(t, finish.After(now))
}
// interestsAnyAssert := func(t *testing.T, account ResponseAccount, value string) {
// values := strings.Split(value, ",")
// hasIntersect := false
// loop:
// for _, actualInterest := range *account.Interests {
// for _, value := range values {
// if actualInterest == value {
// hasIntersect = true
// break loop
// }
// }
// }
// assert.True(t, hasIntersect)
// }
// interestsContainsAssert := func(t *testing.T, account ResponseAccount, value string) {
// values := strings.Split(value, ",")
// for _, value := range values {
// found := false
// for _, actualInterest := range *account.Interests {
// if actualInterest == value {
// found = true
// break
// }
// }
// assert.True(t, found)
// }
// }
likesContainsAssert := func(t *testing.T, account ResponseAccount, value string) {
values := strings.Split(value, ",")
likes := make([]int, 0)
for _, value := range values {
i, err := strconv.Atoi(value)
assert.NoError(t, err)
likes = append(likes, int(i))
}
for _, like := range likes {
found := false
for _, actualLike := range *account.Likes {
if actualLike.Id == like {
found = true
break
}
}
assert.True(t, found)
}
}
ltAssert := func(field string) func(t *testing.T, account ResponseAccount, value string) {
return func(t *testing.T, account ResponseAccount, value string) {
assert.True(t, *account.GetField(field) < value, fmt.Sprint(*account.GetField(field), " < ", value))
}
}
gtAssert := func(field string) func(t *testing.T, account ResponseAccount, value string) {
return func(t *testing.T, account ResponseAccount, value string) {
assert.True(t, *account.GetField(field) > value)
}
}
ltAssertInt := func(field string) func(t *testing.T, account ResponseAccount, value string) {
return func(t *testing.T, account ResponseAccount, value string) {
actualValue, err := reflections.GetField(account, field)
assert.NoError(t, err)
valueInt, err := strconv.Atoi(value)
assert.NoError(t, err)
assert.True(t, *(actualValue.(*int)) < int(valueInt))
}
}
gtAssertInt := func(field string) func(t *testing.T, account ResponseAccount, value string) {
return func(t *testing.T, account ResponseAccount, value string) {
actualValue, err := reflections.GetField(account, field)
assert.NoError(t, err)
valueInt, err := strconv.Atoi(value)
assert.NoError(t, err)
assert.True(t, *(actualValue.(*int)) > int(valueInt))
}
}
eqAssert := func(field string) func(t *testing.T, account ResponseAccount, value string) {
return func(t *testing.T, account ResponseAccount, value string) {
assert.Equal(t, &value, account.GetField(field))
}
}
neqAssert := func(field string) func(t *testing.T, account ResponseAccount, value string) {
return func(t *testing.T, account ResponseAccount, value string) {
assert.NotEqual(t, &value, account.GetField(field))
}
}
nullAssert := func(field string) func(t *testing.T, account ResponseAccount, value string) {
return func(t *testing.T, account ResponseAccount, value string) {
actual, err := reflections.GetField(account, field)
assert.NoError(t, err)
if value == "0" {
assert.NotNil(t, actual)
} else {
assert.Nil(t, actual)
}
}
}
anyAssert := func(field string) func(t *testing.T, account ResponseAccount, value string) {
return func(t *testing.T, account ResponseAccount, value string) {
values := strings.Split(value, ",")
assert.Contains(t, values, *account.GetField(field))
}
}
startsAssert := func(field string) func(t *testing.T, account ResponseAccount, value string) {
return func(t *testing.T, account ResponseAccount, value string) {
assert.Equal(t, 0, strings.Index(*account.GetField(field), value))
}
}
cases := []struct {
param string
value string
assert func(t *testing.T, account ResponseAccount, value string)
}{
{"email_domain", "inbox.ru", emailDomainAssert},
{"email_domain", "mail.ru", emailDomainAssert},
{"email_lt", "bbbbbbb", ltAssert("Email")},
{"email_gt", "ttttttt", gtAssert("Email")},
{"sex_eq", "m", eqAssert("Sex")},
{"sex_eq", "f", eqAssert("Sex")},
{"status_eq", "свободны", eqAssert("Status")},
{"status_eq", "заняты", eqAssert("Status")},
{"status_neq", "свободны", neqAssert("Status")},
{"status_neq", "заняты", neqAssert("Status")},
{"fname_eq", "Алёна", eqAssert("FName")},
{"fname_eq", "Павел", eqAssert("FName")},
{"fname_any", "Алёна,Павел", anyAssert("FName")},
{"fname_any", "Алёнааааааа,Павел", anyAssert("FName")},
{"fname_null", "0", nullAssert("FName")},
{"sname_eq", "Терленчан", eqAssert("SName")},
{"sname_null", "0", nullAssert("SName")},
{"sname_starts", "Тер", startsAssert("SName")},
{"phone_code", "983", phoneCodeAssert},
{"phone_code", "967", phoneCodeAssert},
{"phone_null", "1", nullAssert("Phone")},
{"phone_null", "0", nullAssert("Phone")},
{"country_null", "0", nullAssert("Country")},
{"country_null", "1", nullAssert("Country")},
{"country_eq", "Индция", eqAssert("Country")},
{"city_eq", "Росоград", eqAssert("City")},
{"city_any", "Росоград,Пррпрпрп", anyAssert("City")},
{"city_null", "0", nullAssert("City")},
{"city_null", "1", nullAssert("City")},
{"birth_lt", "806700770", ltAssertInt("Birth")},
{"birth_gt", "806700770", gtAssertInt("Birth")},
{"birth_year", "1995", birthYearAssert},
// {"interests_any", "Апельсиновый сок,Матрица,YouTube", interestsAnyAssert},
// {"interests_contains", "Апельсиновый сок,Матрица,YouTube", interestsContainsAssert},
{"likes_contains", "28499,2005", likesContainsAssert},
{"premium_now", "1", premiumAssert},
{"premium_null", "0", nullAssert("Premium")},
{"premium_null", "1", nullAssert("Premium")},
}
for _, testCase := range cases {
t.Run(fmt.Sprint(testCase.param, "=", testCase.value), func(t *testing.T) {
teardown, storage := SetupTest(t)
defer teardown()
handler := AccountsFilterHandler{
storage: storage,
}
request, _ := http.NewRequest("GET", fmt.Sprint("accounts/filter?limit=10&", testCase.param, "=", testCase.value), nil)
responseRecorder := httptest.NewRecorder()
handler.ServeHTTP(responseRecorder, request)
assert.Equal(t, http.StatusOK, responseRecorder.Code)
response := Response{}
err := json.Unmarshal(responseRecorder.Body.Bytes(), &response)
assert.Nil(t, err, err)
assert.NotEmpty(t, response)
assert.NotEmpty(t, response.Accounts)
for _, account := range response.Accounts {
testCase.assert(t, account, testCase.value)
}
})
}
}