-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase_query_test.go
355 lines (287 loc) · 7.9 KB
/
base_query_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
package es_test
import (
"reflect"
"testing"
"github.com/Trendyol/es-query-builder/es"
"github.com/Trendyol/es-query-builder/test/assert"
)
//// NewQuery ////
func Test_NewQuery_should_exist_on_es_package(t *testing.T) {
t.Parallel()
// Given When Then
assert.NotNil(t, es.NewQuery)
}
func Test_NewQuery_should_create_a_new_Object(t *testing.T) {
t.Parallel()
// Given When
bodyA := es.NewQuery(nil)
bodyB := es.NewQuery(nil)
// Then
assert.NotNil(t, bodyA)
assert.NotNil(t, bodyB)
assert.Equal(t, bodyA, bodyB)
assert.NotEqualReference(t, bodyA, bodyB)
assert.MarshalWithoutError(t, bodyA)
assert.MarshalWithoutError(t, bodyB)
}
func Test_NewQuery_should_return_type_of_Object(t *testing.T) {
t.Parallel()
// Given
query := es.NewQuery(nil)
// When
bodyType := reflect.TypeOf(query).String()
// Then
assert.NotNil(t, query)
assert.Equal(t, "es.Object", bodyType)
assert.MarshalWithoutError(t, query)
}
func Test_NewQuery_should_add_query_field_into_Object(t *testing.T) {
t.Parallel()
// Given
query := es.NewQuery(nil)
// When
q, exists := query["query"]
// Then
assert.True(t, exists)
assert.NotNil(t, q)
}
func Test_NewQuery_should_create_json_with_query_field(t *testing.T) {
t.Parallel()
// Given
query := es.NewQuery(nil)
// When Then
assert.NotNil(t, query)
bodyJSON := assert.MarshalWithoutError(t, query)
assert.Equal(t, "{\"query\":{}}", bodyJSON)
}
func Test_NewQuery_Bool_should_create_json_with_bool_field_inside_query(t *testing.T) {
t.Parallel()
// Given
query := es.NewQuery(
es.Bool(),
)
// When Then
assert.NotNil(t, query)
bodyJSON := assert.MarshalWithoutError(t, query)
assert.Equal(t, "{\"query\":{\"bool\":{}}}", bodyJSON)
}
//// TrackTotalHits ////
func Test_Object_should_have_TrackTotalHits_method(t *testing.T) {
t.Parallel()
// Given
b := es.NewQuery(nil)
// When Then
assert.NotNil(t, b.TrackTotalHits)
}
func Test_TrackTotalHits_should_add_track_total_hits_field_into_Object(t *testing.T) {
t.Parallel()
// Given
query := es.NewQuery(nil)
// When
_, beforeExists := query["track_total_hits"]
object := query.TrackTotalHits(true)
trackTotalHits, afterExists := query["track_total_hits"]
// Then
assert.NotNil(t, object)
assert.False(t, beforeExists)
assert.True(t, afterExists)
assert.True(t, trackTotalHits.(bool))
}
//// Size ////
func Test_Object_should_have_Size_method(t *testing.T) {
t.Parallel()
// Given
b := es.NewQuery(nil)
// When Then
assert.NotNil(t, b.Size)
}
func Test_Size_should_add_size_field_into_Object(t *testing.T) {
t.Parallel()
// Given
query := es.NewQuery(nil)
// When
_, beforeExists := query["size"]
object := query.Size(123)
size, afterExists := query["size"]
// Then
assert.NotNil(t, object)
assert.False(t, beforeExists)
assert.True(t, afterExists)
assert.Equal(t, 123, size.(int))
}
//// From ////
func Test_Object_should_have_From_method(t *testing.T) {
t.Parallel()
// Given
b := es.NewQuery(nil)
// When Then
assert.NotNil(t, b.From)
}
func Test_From_should_add_from_field_into_Object(t *testing.T) {
t.Parallel()
// Given
query := es.NewQuery(nil)
// When
_, beforeExists := query["from"]
object := query.From(1500)
from, afterExists := query["from"]
// Then
assert.NotNil(t, object)
assert.False(t, beforeExists)
assert.True(t, afterExists)
assert.Equal(t, 1500, from.(int))
}
//// Sort ////
func Test_Object_should_have_Sort_method(t *testing.T) {
t.Parallel()
// Given
b := es.NewQuery(nil)
// When Then
assert.NotNil(t, b.Sort)
}
//// Source ////
func Test_Object_should_have_SourceIncludes_method(t *testing.T) {
t.Parallel()
// Given
b := es.NewQuery(nil)
// When Then
assert.NotNil(t, b.SourceIncludes())
}
func Test_SourceIncludes_should_add_source_field_with_includes_to_Object(t *testing.T) {
t.Parallel()
// Given
query := es.NewQuery(nil)
// When
_, beforeExists := query["_source"]
query.SourceIncludes("test")
source, afterExists := query["_source"]
// Then
assert.False(t, beforeExists)
assert.True(t, afterExists)
assert.NotNil(t, source)
assert.IsTypeString(t, "es.Object", source)
bodyJSON := assert.MarshalWithoutError(t, query)
assert.Equal(t, "{\"_source\":{\"includes\":[\"test\"]},\"query\":{}}", bodyJSON)
}
func Test_SourceIncludes_should_apped_includes_when_it_already_exists_in_the_source(t *testing.T) {
t.Parallel()
// Given
query := es.NewQuery(nil).SourceIncludes("first", "second")
// When
_, beforeExists := query["_source"]
query.
SourceIncludes("third").
SourceIncludes("fourth", "fifth")
source, afterExists := query["_source"]
// Then
assert.True(t, beforeExists)
assert.True(t, afterExists)
assert.NotNil(t, source)
assert.IsTypeString(t, "es.Object", source)
bodyJSON := assert.MarshalWithoutError(t, query)
// nolint:golint,lll
assert.Equal(t, "{\"_source\":{\"includes\":[\"first\",\"second\",\"third\",\"fourth\",\"fifth\"]},\"query\":{}}", bodyJSON)
}
func Test_SourceIncludes_should_not_add_includes_to_Object_when_fields_are_empty(t *testing.T) {
t.Parallel()
// Given
query := es.NewQuery(nil)
// When
_, beforeExists := query["_source"]
query.SourceIncludes() // empty
source, afterExists := query["_source"]
// Then
assert.False(t, beforeExists)
assert.False(t, afterExists)
assert.Nil(t, source)
bodyJSON := assert.MarshalWithoutError(t, query)
assert.Equal(t, "{\"query\":{}}", bodyJSON)
}
func Test_Object_should_have_SourceExcludes_method(t *testing.T) {
t.Parallel()
// Given
b := es.NewQuery(nil)
// When Then
assert.NotNil(t, b.SourceExcludes())
}
func Test_SourceExcludes_should_add_source_field_with_excludes_to_Object(t *testing.T) {
t.Parallel()
// Given
query := es.NewQuery(nil)
// When
_, beforeExists := query["_source"]
query.SourceExcludes("test")
source, afterExists := query["_source"]
// Then
assert.False(t, beforeExists)
assert.True(t, afterExists)
assert.NotNil(t, source)
assert.IsTypeString(t, "es.Object", source)
bodyJSON := assert.MarshalWithoutError(t, query)
assert.Equal(t, "{\"_source\":{\"excludes\":[\"test\"]},\"query\":{}}", bodyJSON)
}
func Test_SourceExcludes_should_apped_excludes_when_it_already_exists_in_the_source(t *testing.T) {
t.Parallel()
// Given
query := es.NewQuery(nil).SourceExcludes("first", "second")
// When
_, beforeExists := query["_source"]
query.
SourceExcludes("third").
SourceExcludes("fourth", "fifth")
source, afterExists := query["_source"]
// Then
assert.True(t, beforeExists)
assert.True(t, afterExists)
assert.NotNil(t, source)
assert.IsTypeString(t, "es.Object", source)
bodyJSON := assert.MarshalWithoutError(t, query)
// nolint:golint,lll
assert.Equal(t, "{\"_source\":{\"excludes\":[\"first\",\"second\",\"third\",\"fourth\",\"fifth\"]},\"query\":{}}", bodyJSON)
}
func Test_SourceExcludes_should_not_add_excludes_to_Object_when_fields_are_empty(t *testing.T) {
t.Parallel()
// Given
query := es.NewQuery(nil)
// When
_, beforeExists := query["_source"]
query.SourceExcludes() // empty
source, afterExists := query["_source"]
// Then
assert.False(t, beforeExists)
assert.False(t, afterExists)
assert.Nil(t, source)
bodyJSON := assert.MarshalWithoutError(t, query)
assert.Equal(t, "{\"query\":{}}", bodyJSON)
}
func Test_Object_should_have_SourceFalse_method(t *testing.T) {
t.Parallel()
// Given
b := es.NewQuery(nil)
// When Then
assert.NotNil(t, b.SourceFalse)
}
func Test_SourceFalse_should_set_source_field_as_false(t *testing.T) {
t.Parallel()
// Given
query := es.NewQuery(nil)
// When
_, beforeExists := query["_source"]
query.SourceFalse()
source, afterExists := query["_source"]
// Then
assert.NotNil(t, source)
assert.False(t, beforeExists)
assert.True(t, afterExists)
assert.False(t, source.(bool))
bodyJSON := assert.MarshalWithoutError(t, query)
assert.Equal(t, "{\"_source\":false,\"query\":{}}", bodyJSON)
}
//// Aggs ////
func Test_Object_should_have_Aggs_method(t *testing.T) {
t.Parallel()
// Given
b := es.NewQuery(nil)
// When Then
assert.NotNil(t, b.Aggs)
}