-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch_test.go
211 lines (200 loc) · 5.6 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
package pagination_test
import (
"errors"
"net/url"
"reflect"
"testing"
"github.com/yohgo/pagination"
)
// newSearchDataProvider provides data for the TestNewQuery function.
var newSearchDataProvider = []struct {
name string
query string
err error
}{
{
name: "Successful search creation",
query: "name__equals=ammar&type__notequals=admin&age__greaterthan=18&rank__lessthan=3&amount__gthanorequals=1000&level__lthanorequals=5&department__startswith=dev&department__endswith=nt&department__contains=mnt&created__after=2016-08-07&created__before=2016-09-07&created__year=2016&created__month=8&created__day=5&searchOperator=AND",
err: nil,
},
{
name: "Successful search creation - Missing search operator",
query: "name__equals=ammar&type__notequals=admin&age__greaterthan=18",
err: errors.New("Search operator is missing"),
},
{
name: "Successful search creation - Unknown search operation",
query: "name__equals=ammar&type__notequals=admin&age__unknownoperation=18&searchOperator=AND",
err: errors.New("Unknown search operation 'unknownoperation'"),
},
{
name: "Successful search creation - No search conditions",
query: "searchOperator=AND",
err: errors.New("Cannot find search conditions"),
},
{
name: "A failed search creation - No search query parameters",
query: "page=3&limit=3&order_by=surname&order=desc",
err: nil,
},
}
// TestNewSearch tests the paginator NewSearch method.
func TestNewSearch(t *testing.T) {
t.Log("NewSearch")
// Check each test case
for _, testcase := range newSearchDataProvider {
t.Log(testcase.name)
query, _ := url.ParseQuery(testcase.query)
_, err := pagination.NewSearch(query)
// Check error
if !reflect.DeepEqual(testcase.err, err) {
t.Errorf("Expected error to be %q but got %q", testcase.err, err)
}
}
}
// getSearchComponentsDataProvider provides data for the TestgetSearchComponents function.
var getSearchComponentsDataProvider = []struct {
name string
field string
operator string
value string
condition string
parameter string
}{
{
name: "An search condition retrieval with the equals operation",
field: "id",
operator: "equals",
value: "3",
condition: "(id = ?)",
parameter: "3",
},
{
name: "An search condition retrieval with the notequals operation",
field: "id",
operator: "notequals",
value: "3",
condition: "(id != ?)",
parameter: "3",
},
{
name: "An search condition retrieval with the greaterthan operation",
field: "id",
operator: "greaterthan",
value: "18",
condition: "(id > ?)",
parameter: "18",
},
{
name: "An search condition retrieval with the lessthan operation",
field: "id",
operator: "lessthan",
value: "18",
condition: "(id < ?)",
parameter: "18",
},
{
name: "An search condition retrieval with the gthanorequals operation",
field: "id",
operator: "gthanorequals",
value: "18",
condition: "(id >= ?)",
parameter: "18",
},
{
name: "An search condition retrieval with the lthanorequals operation",
field: "id",
operator: "lthanorequals",
value: "18",
condition: "(id <= ?)",
parameter: "18",
},
{
name: "An search condition retrieval with the startswith operation",
field: "name",
operator: "startswith",
value: "am",
condition: "(name LIKE ?)",
parameter: "am%",
},
{
name: "An search condition retrieval with the endswith operation",
field: "name",
operator: "endswith",
value: "am",
condition: "(name LIKE ?)",
parameter: "%am",
},
{
name: "An search condition retrieval with the contains operation",
field: "name",
operator: "contains",
value: "mm",
condition: "(name LIKE ?)",
parameter: "%mm%",
},
{
name: "An search condition retrieval with the after operation",
field: "created_at",
operator: "after",
value: "2016-08-07 00:00:00",
condition: "(created_at > ?)",
parameter: "2016-08-07 00:00:00",
},
{
name: "An search condition retrieval with the before operation",
field: "created_at",
operator: "before",
value: "2016-08-07 00:00:00",
condition: "(created_at < ?)",
parameter: "2016-08-07 00:00:00",
},
{
name: "An search condition retrieval with the year operation",
field: "created_at",
operator: "year",
value: "2017",
condition: "(YEAR(created_at) = ?)",
parameter: "2017",
},
{
name: "An search condition retrieval with the month operation",
field: "created_at",
operator: "month",
value: "10",
condition: "(MONTH(created_at) = ?)",
parameter: "10",
},
{
name: "An search condition retrieval with the day operation",
field: "created_at",
operator: "day",
value: "11",
condition: "(DAY(created_at) = ?)",
parameter: "11",
},
{
name: "A failed condition retrieval",
field: "",
operator: "",
value: "",
condition: "",
},
}
// TestGetSearchComponents tests the paginator GetSearchComponents method.
func TestGetSearchComponents(t *testing.T) {
t.Log("GetSearchComponents")
// Check each test case
for _, testcase := range getSearchComponentsDataProvider {
t.Log(testcase.name)
condition, parameter := pagination.GetSearchComponents(testcase.field, testcase.operator, testcase.value)
// Check condition
if !reflect.DeepEqual(testcase.condition, condition) {
t.Errorf("Expected response to be %s but got %s", testcase.condition, condition)
}
// Check parameter
if !reflect.DeepEqual(testcase.parameter, parameter) {
t.Errorf("Expected parameter to be %s but got %s", testcase.parameter, parameter)
}
}
}