-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinner_hits_test.go
560 lines (470 loc) · 14.8 KB
/
inner_hits_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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package es_test
import (
"testing"
ScriptLanguage "github.com/Trendyol/es-query-builder/es/enums/script-language"
Order "github.com/Trendyol/es-query-builder/es/enums/sort/order"
"github.com/Trendyol/es-query-builder/es"
"github.com/Trendyol/es-query-builder/test/assert"
)
//// Inner Hits ////
func Test_InnerHits_should_exist_on_es_package(t *testing.T) {
t.Parallel()
// Given When Then
assert.NotNil(t, es.InnerHits)
}
func Test_InnerHits_method_should_create_innerHitsType(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// Then
assert.NotNil(t, ih)
assert.IsTypeString(t, "es.innerHitsType", ih)
}
func Test_InnerHits_should_have_Explain_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.Explain)
}
func Test_InnerHits_Explain_should_create_json_with_explain_field_inside_inner_hits(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().Explain(true)
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{\"explain\":true}", bodyJSON)
}
func Test_InnerHits_should_have_Fields_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.Fields)
}
func Test_InnerHits_Fields_should_create_json_with_fields_field_inside_inner_hits(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().Fields("f1", "g2", "h3")
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{\"fields\":[\"f1\",\"g2\",\"h3\"]}", bodyJSON)
}
func Test_InnerHits_should_have_Size_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.Size)
}
func Test_InnerHits_Size_should_create_json_with_size_field_inside_inner_hits(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().Size(100)
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{\"size\":100}", bodyJSON)
}
func Test_InnerHits_should_have_From_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.From)
}
func Test_InnerHits_From_should_create_json_with_from_field_inside_inner_hits(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().From(5_000)
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{\"from\":5000}", bodyJSON)
}
func Test_InnerHits_should_have_Name_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.Name)
}
func Test_InnerHits_Name_should_create_json_with_name_field_inside_inner_hits(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().Name("Göksel")
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{\"name\":\"Göksel\"}", bodyJSON)
}
func Test_InnerHits_should_have_SourceFalse_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.SourceFalse)
}
func Test_InnerHits_SourceFalse_should_create_json_with_source_field_inside_inner_hits_with_false(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().SourceFalse()
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{\"_source\":false}", bodyJSON)
}
func Test_InnerHits_should_have_SourceIncludes_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.SourceIncludes)
}
func Test_InnerHits_SourceIncludes_should_create_json_with_source_field_inside_inner_hits(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().SourceIncludes("id", "name", "description")
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{\"_source\":{\"includes\":[\"id\",\"name\",\"description\"]}}", bodyJSON)
}
func Test_InnerHits_SourceIncludes_should_not_create_json_with_source_field_inside_inner_hits_when_empty(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().SourceIncludes()
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{}", bodyJSON)
}
func Test_InnerHits_should_have_SourceExcludes_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.SourceExcludes)
}
func Test_InnerHits_SourceExcludes_should_create_json_with_source_field_inside_inner_hits(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().SourceExcludes("secret", "key", "partition")
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{\"_source\":{\"excludes\":[\"secret\",\"key\",\"partition\"]}}", bodyJSON)
}
func Test_InnerHits_SourceExcludes_should_not_create_json_with_source_field_inside_inner_hits_when_empty(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().SourceExcludes()
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{}", bodyJSON)
}
func Test_InnerHits_should_have_StoredFields_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.StoredFields)
}
func Test_InnerHits_StoredFields_should_create_json_with_stored_fields_field_inside_inner_hits(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().StoredFields("x1", "y2", "z3")
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{\"stored_fields\":[\"x1\",\"y2\",\"z3\"]}", bodyJSON)
}
func Test_InnerHits_should_have_TrackScores_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.TrackScores)
}
func Test_InnerHits_TrackScores_should_create_json_with_track_scores_field_inside_inner_hits(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().TrackScores(false)
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{\"track_scores\":false}", bodyJSON)
}
func Test_InnerHits_should_have_Version_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.Version)
}
func Test_InnerHits_Version_should_create_json_with_version_field_inside_inner_hits(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().Version(true)
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{\"version\":true}", bodyJSON)
}
func Test_InnerHits_should_have_IgnoreUnmapped_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.IgnoreUnmapped)
}
func Test_InnerHits_IgnoreUnmapped_should_create_json_with_ignore_unmapped_field_inside_inner_hits(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().IgnoreUnmapped(true)
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{\"ignore_unmapped\":true}", bodyJSON)
}
func Test_InnerHits_should_have_SeqNoPrimaryTerm_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.SeqNoPrimaryTerm)
}
func Test_InnerHits_SeqNoPrimaryTerm_should_create_json_with_seq_no_primary_term_field_inside_inner_hits(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().SeqNoPrimaryTerm(false)
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{\"seq_no_primary_term\":false}", bodyJSON)
}
func Test_InnerHits_should_have_Sort_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.Sort)
}
func Test_InnerHits_Sort_should_create_json_with_sort_field_inside_inner_hits(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().Sort(es.Sort("indexedAt").Order(Order.Desc))
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{\"sort\":[{\"indexedAt\":{\"order\":\"desc\"}}]}", bodyJSON)
}
func Test_InnerHits_should_have_Collapse_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.Collapse)
}
func Test_InnerHits_Collapse_should_create_json_with_collapse_field_inside_inner_hits(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().Collapse(es.FieldCollapse("yelle"))
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{\"collapse\":{\"field\":\"yelle\"}}", bodyJSON)
}
func Test_InnerHits_should_have_DocvalueFields_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.DocvalueFields)
}
func Test_InnerHits_DocvalueFields_should_create_json_with_docvalue_fields_field_inside_inner_hits(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().DocvalueFields(
es.FieldAndFormat("id"),
es.FieldAndFormat("name"),
es.FieldAndFormat("partition"),
)
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
assert.Equal(t, "{\"docvalue_fields\":[{\"field\":\"id\"},{\"field\":\"name\"},{\"field\":\"partition\"}]}", bodyJSON)
}
func Test_InnerHits_should_have_ScriptField_method(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits()
// When Then
assert.NotNil(t, ih.ScriptField)
}
func Test_InnerHits_ScriptField_should_create_json_with_script_fields_field_inside_inner_hits(t *testing.T) {
t.Parallel()
// Given
ih := es.InnerHits().
ScriptField("first_script",
es.ScriptField(es.ScriptID("key", ScriptLanguage.Painless).
Option("retry", "3").
Option("timeout", "300s").
Parameter("p1", 100).
Parameter("p2", "hello"),
).IgnoreFailure(false),
).
ScriptField("second_script",
es.ScriptField(es.ScriptID("value", ScriptLanguage.Mustache).
Option("retry", "5").
Parameter("p1", "world"),
).IgnoreFailure(true),
)
// When Then
assert.NotNil(t, ih)
bodyJSON := assert.MarshalWithoutError(t, ih)
// nolint:golint,lll
assert.Equal(t, "{\"script_fields\":{\"first_script\":{\"ignore_failure\":false,\"script\":{\"id\":\"key\",\"lang\":\"painless\",\"options\":{\"retry\":\"3\",\"timeout\":\"300s\"},\"params\":{\"p1\":100,\"p2\":\"hello\"}}},\"second_script\":{\"ignore_failure\":true,\"script\":{\"id\":\"value\",\"lang\":\"mustache\",\"options\":{\"retry\":\"5\"},\"params\":{\"p1\":\"world\"}}}}}", bodyJSON)
}
//// Script Field ////
func Test_ScriptField_should_exist_on_es_package(t *testing.T) {
t.Parallel()
// Given When Then
assert.NotNil(t, es.ScriptField)
}
func Test_ScriptField_method_should_create_scriptFieldType(t *testing.T) {
t.Parallel()
// Given
sf := es.ScriptField(es.ScriptID("key", ScriptLanguage.Painless))
// Then
assert.NotNil(t, sf)
assert.IsTypeString(t, "es.scriptFieldType", sf)
}
func Test_ScriptField_should_have_IgnoreFailure_method(t *testing.T) {
t.Parallel()
// Given
sf := es.ScriptField(es.ScriptID("key", ScriptLanguage.Painless))
// When Then
assert.NotNil(t, sf.IgnoreFailure)
}
func Test_ScriptField_IgnoreFailure_should_create_json_with_collapse_field_inside_field_collapse(t *testing.T) {
t.Parallel()
// Given
sf := es.ScriptField(es.ScriptID("key", ScriptLanguage.Painless)).IgnoreFailure(true)
// When Then
assert.NotNil(t, sf)
bodyJSON := assert.MarshalWithoutError(t, sf)
assert.Equal(t, "{\"ignore_failure\":true,\"script\":{\"id\":\"key\",\"lang\":\"painless\"}}", bodyJSON)
}
//// Field Collapse ////
func Test_FieldCollapse_should_exist_on_es_package(t *testing.T) {
t.Parallel()
// Given When Then
assert.NotNil(t, es.FieldCollapse)
}
func Test_FieldCollapse_method_should_create_fieldCollapseType(t *testing.T) {
t.Parallel()
// Given
fc := es.FieldCollapse("name")
// Then
assert.NotNil(t, fc)
assert.IsTypeString(t, "es.fieldCollapseType", fc)
}
func Test_FieldCollapse_should_have_Collapse_method(t *testing.T) {
t.Parallel()
// Given
fc := es.FieldCollapse("id")
// When Then
assert.NotNil(t, fc.Collapse)
}
func Test_FieldCollapse_Collapse_should_create_json_with_collapse_field_inside_field_collapse(t *testing.T) {
t.Parallel()
// Given
fc := es.FieldCollapse("id").Collapse(es.FieldCollapse("index"))
// When Then
assert.NotNil(t, fc)
bodyJSON := assert.MarshalWithoutError(t, fc)
assert.Equal(t, "{\"collapse\":{\"field\":\"index\"},\"field\":\"id\"}", bodyJSON)
}
func Test_FieldCollapse_should_have_InnerHits_method(t *testing.T) {
t.Parallel()
// Given
fc := es.FieldCollapse("id")
// When Then
assert.NotNil(t, fc.InnerHits)
}
func Test_FieldCollapse_InnerHits_should_create_json_with_inner_hits_field_inside_field_collapse(t *testing.T) {
t.Parallel()
// Given
fc := es.FieldCollapse("display").InnerHits(es.InnerHits().Fields("name", "surname"))
// When Then
assert.NotNil(t, fc)
bodyJSON := assert.MarshalWithoutError(t, fc)
assert.Equal(t, "{\"field\":\"display\",\"inner_hits\":[{\"fields\":[\"name\",\"surname\"]}]}", bodyJSON)
}
func Test_FieldCollapse_should_have_MaxConcurrentGroupSearches_method(t *testing.T) {
t.Parallel()
// Given
fc := es.FieldCollapse("id")
// When Then
assert.NotNil(t, fc.MaxConcurrentGroupSearches)
}
// nolint:golint,lll
func Test_FieldCollapse_MaxConcurrentGroupSearches_should_create_json_with_max_concurrent_group_searches_field_inside_field_collapse(t *testing.T) {
t.Parallel()
// Given
fc := es.FieldCollapse("display").MaxConcurrentGroupSearches(67)
// When Then
assert.NotNil(t, fc)
bodyJSON := assert.MarshalWithoutError(t, fc)
assert.Equal(t, "{\"field\":\"display\",\"max_concurrent_group_searches\":67}", bodyJSON)
}
//// Field And Format ////
func Test_FieldAndFormat_should_exist_on_es_package(t *testing.T) {
t.Parallel()
// Given When Then
assert.NotNil(t, es.FieldAndFormat)
}
func Test_FieldAndFormat_method_should_create_fieldAndFormatType(t *testing.T) {
t.Parallel()
// Given
fnf := es.FieldAndFormat("name")
// Then
assert.NotNil(t, fnf)
assert.IsTypeString(t, "es.fieldAndFormatType", fnf)
}
func Test_FieldAndFormat_should_have_Format_method(t *testing.T) {
t.Parallel()
// Given
fnf := es.FieldAndFormat("name")
// When Then
assert.NotNil(t, fnf.Format)
}
func Test_FieldAndFormat_Format_should_create_json_with_format_field_inside_field_and_format(t *testing.T) {
t.Parallel()
// Given
fnf := es.FieldAndFormat("xyz").Format("yyyy-MM-dd")
// When Then
assert.NotNil(t, fnf)
bodyJSON := assert.MarshalWithoutError(t, fnf)
assert.Equal(t, "{\"field\":\"xyz\",\"format\":\"yyyy-MM-dd\"}", bodyJSON)
}
func Test_FieldAndFormat_should_have_IncludeUnmapped_method(t *testing.T) {
t.Parallel()
// Given
fnf := es.FieldAndFormat("name")
// When Then
assert.NotNil(t, fnf.IncludeUnmapped)
}
func Test_FieldAndFormat_IncludeUnmapped_should_create_json_with_include_unmapped_field_inside_field_and_format(t *testing.T) {
t.Parallel()
// Given
fnf := es.FieldAndFormat("hello").IncludeUnmapped(false)
// When Then
assert.NotNil(t, fnf)
bodyJSON := assert.MarshalWithoutError(t, fnf)
assert.Equal(t, "{\"field\":\"hello\",\"include_unmapped\":false}", bodyJSON)
}