@@ -6,10 +6,12 @@ import (
6
6
"encoding/json"
7
7
"fmt"
8
8
"github.com/RediSearch/redisearch-go/redisearch"
9
+ "github.com/garyburd/redigo/redis"
9
10
"github.com/stretchr/testify/assert"
10
11
"log"
11
12
"math/rand"
12
13
"os"
14
+ "reflect"
13
15
"strconv"
14
16
"strings"
15
17
"testing"
@@ -215,3 +217,309 @@ func BenchmarkProcessAggResponseSS_100x4Elements(b *testing.B) {
215
217
b .ResetTimer ()
216
218
benchmarkProcessAggResponseSS (res , 100 , b )
217
219
}
220
+
221
+ func TestProjection_Serialize (t * testing.T ) {
222
+ type fields struct {
223
+ Expression string
224
+ Alias string
225
+ }
226
+ tests := []struct {
227
+ name string
228
+ fields fields
229
+ want redis.Args
230
+ }{
231
+ {"Test_Serialize_1" , fields {"sqrt(log(foo) * floor(@bar/baz)) + (3^@qaz % 6)" , "sqrt" }, redis.Args {"APPLY" , "sqrt(log(foo) * floor(@bar/baz)) + (3^@qaz % 6)" , "AS" , "sqrt" }},
232
+ }
233
+ for _ , tt := range tests {
234
+ t .Run (tt .name , func (t * testing.T ) {
235
+ p := redisearch.Projection {
236
+ Expression : tt .fields .Expression ,
237
+ Alias : tt .fields .Alias ,
238
+ }
239
+ if got := p .Serialize (); ! reflect .DeepEqual (got , tt .want ) {
240
+ t .Errorf ("Serialize() = %v, want %v" , got , tt .want )
241
+ }
242
+ })
243
+ }
244
+ }
245
+
246
+ func TestCursor_Serialize (t * testing.T ) {
247
+ type fields struct {
248
+ Id int
249
+ Count int
250
+ MaxIdle int
251
+ }
252
+ tests := []struct {
253
+ name string
254
+ fields fields
255
+ want redis.Args
256
+ }{
257
+ {"TestCursor_Serialize_1" , fields {1 , 0 , 0 ,}, redis.Args {"WITHCURSOR" }},
258
+ {"TestCursor_Serialize_2_MAXIDLE" , fields {1 , 0 , 30000 ,}, redis.Args {"WITHCURSOR" , "MAXIDLE" , 30000 }},
259
+ {"TestCursor_Serialize_3_COUNT_MAXIDLE" , fields {1 , 10 , 30000 ,}, redis.Args {"WITHCURSOR" , "COUNT" , 10 , "MAXIDLE" , 30000 }},
260
+ }
261
+ for _ , tt := range tests {
262
+ t .Run (tt .name , func (t * testing.T ) {
263
+ c := redisearch.Cursor {
264
+ Id : tt .fields .Id ,
265
+ Count : tt .fields .Count ,
266
+ MaxIdle : tt .fields .MaxIdle ,
267
+ }
268
+ if got := c .Serialize (); ! reflect .DeepEqual (got , tt .want ) {
269
+ t .Errorf ("Serialize() = %v, want %v" , got , tt .want )
270
+ }
271
+ })
272
+ }
273
+ }
274
+
275
+ func TestGroupBy_AddFields (t * testing.T ) {
276
+ type fields struct {
277
+ Fields []string
278
+ Reducers []redisearch.Reducer
279
+ Paging * redisearch.Paging
280
+ }
281
+ type args struct {
282
+ fields interface {}
283
+ }
284
+ tests := []struct {
285
+ name string
286
+ fields fields
287
+ args args
288
+ want * redisearch.GroupBy
289
+ }{
290
+ {"TestGroupBy_AddFields_1" ,
291
+ fields {[]string {}, nil , nil },
292
+ args {"a" ,},
293
+ & redisearch.GroupBy {[]string {"a" }, nil , nil },
294
+ },
295
+ }
296
+ for _ , tt := range tests {
297
+ t .Run (tt .name , func (t * testing.T ) {
298
+ g := & redisearch.GroupBy {
299
+ Fields : tt .fields .Fields ,
300
+ Reducers : tt .fields .Reducers ,
301
+ Paging : tt .fields .Paging ,
302
+ }
303
+ if got := g .AddFields (tt .args .fields ); ! reflect .DeepEqual (got , tt .want ) {
304
+ t .Errorf ("AddFields() = %v, want %v" , got , tt .want )
305
+ }
306
+ })
307
+ }
308
+ }
309
+
310
+ func TestGroupBy_Limit (t * testing.T ) {
311
+ type fields struct {
312
+ Fields []string
313
+ Reducers []redisearch.Reducer
314
+ Paging * redisearch.Paging
315
+ }
316
+ type args struct {
317
+ offset int
318
+ num int
319
+ }
320
+ tests := []struct {
321
+ name string
322
+ fields fields
323
+ args args
324
+ want * redisearch.GroupBy
325
+ }{
326
+ {"TestGroupBy_Limit_1" ,
327
+ fields {[]string {}, nil , nil },
328
+ args {10 , 20 },
329
+ & redisearch.GroupBy {[]string {}, nil , & redisearch.Paging {10 , 20 }},
330
+ },
331
+ }
332
+ for _ , tt := range tests {
333
+ t .Run (tt .name , func (t * testing.T ) {
334
+ g := & redisearch.GroupBy {
335
+ Fields : tt .fields .Fields ,
336
+ Reducers : tt .fields .Reducers ,
337
+ Paging : tt .fields .Paging ,
338
+ }
339
+ if got := g .Limit (tt .args .offset , tt .args .num ); (got .Paging .Num != tt .want .Paging .Num ) || (got .Paging .Offset != tt .want .Paging .Offset ) {
340
+ t .Errorf ("Limit() = %v, want %v, %v, want %v" ,
341
+ got .Paging .Num , tt .want .Paging .Num ,
342
+ got .Paging .Offset , tt .want .Paging .Offset )
343
+ }
344
+ })
345
+ }
346
+ }
347
+
348
+ func TestAggregateQuery_SetMax (t * testing.T ) {
349
+ type fields struct {
350
+ Query * redisearch.Query
351
+ AggregatePlan redis.Args
352
+ Paging * redisearch.Paging
353
+ Max int
354
+ WithSchema bool
355
+ Verbatim bool
356
+ WithCursor bool
357
+ Cursor * redisearch.Cursor
358
+ }
359
+ type args struct {
360
+ value int
361
+ }
362
+ tests := []struct {
363
+ name string
364
+ fields fields
365
+ args args
366
+ want * redisearch.AggregateQuery
367
+ }{
368
+ {"TestAggregateQuery_SetMax_1" ,
369
+ fields {nil , redis.Args {}, nil , 0 , false , false , false , nil },
370
+ args {10 },
371
+ & redisearch.AggregateQuery {nil , redis.Args {}, nil , 10 , false , false , false , nil },
372
+ },
373
+ }
374
+ for _ , tt := range tests {
375
+ t .Run (tt .name , func (t * testing.T ) {
376
+ a := & redisearch.AggregateQuery {
377
+ Query : tt .fields .Query ,
378
+ AggregatePlan : tt .fields .AggregatePlan ,
379
+ Paging : tt .fields .Paging ,
380
+ Max : tt .fields .Max ,
381
+ WithSchema : tt .fields .WithSchema ,
382
+ Verbatim : tt .fields .Verbatim ,
383
+ WithCursor : tt .fields .WithCursor ,
384
+ Cursor : tt .fields .Cursor ,
385
+ }
386
+ if got := a .SetMax (tt .args .value ); ! reflect .DeepEqual (got , tt .want ) {
387
+ t .Errorf ("SetMax() = %v, want %v" , got , tt .want )
388
+ }
389
+ })
390
+ }
391
+ }
392
+
393
+ func TestAggregateQuery_SetVerbatim (t * testing.T ) {
394
+ type fields struct {
395
+ Query * redisearch.Query
396
+ AggregatePlan redis.Args
397
+ Paging * redisearch.Paging
398
+ Max int
399
+ WithSchema bool
400
+ Verbatim bool
401
+ WithCursor bool
402
+ Cursor * redisearch.Cursor
403
+ }
404
+ type args struct {
405
+ value bool
406
+ }
407
+ tests := []struct {
408
+ name string
409
+ fields fields
410
+ args args
411
+ want * redisearch.AggregateQuery
412
+ }{
413
+ {"TestAggregateQuery_SetVerbatim_1" ,
414
+ fields {nil , redis.Args {}, nil , 0 , false , false , false , nil },
415
+ args {true },
416
+ & redisearch.AggregateQuery {nil , redis.Args {}, nil , 0 , false , true , false , nil },
417
+ },
418
+ }
419
+ for _ , tt := range tests {
420
+ t .Run (tt .name , func (t * testing.T ) {
421
+ a := & redisearch.AggregateQuery {
422
+ Query : tt .fields .Query ,
423
+ AggregatePlan : tt .fields .AggregatePlan ,
424
+ Paging : tt .fields .Paging ,
425
+ Max : tt .fields .Max ,
426
+ WithSchema : tt .fields .WithSchema ,
427
+ Verbatim : tt .fields .Verbatim ,
428
+ WithCursor : tt .fields .WithCursor ,
429
+ Cursor : tt .fields .Cursor ,
430
+ }
431
+ if got := a .SetVerbatim (tt .args .value ); ! reflect .DeepEqual (got , tt .want ) {
432
+ t .Errorf ("SetVerbatim() = %v, want %v" , got , tt .want )
433
+ }
434
+ })
435
+ }
436
+ }
437
+
438
+ func TestAggregateQuery_SetWithSchema (t * testing.T ) {
439
+ type fields struct {
440
+ Query * redisearch.Query
441
+ AggregatePlan redis.Args
442
+ Paging * redisearch.Paging
443
+ Max int
444
+ WithSchema bool
445
+ Verbatim bool
446
+ WithCursor bool
447
+ Cursor * redisearch.Cursor
448
+ }
449
+ type args struct {
450
+ value bool
451
+ }
452
+ tests := []struct {
453
+ name string
454
+ fields fields
455
+ args args
456
+ want * redisearch.AggregateQuery
457
+ }{
458
+ {"TestAggregateQuery_SetWithSchema_1" ,
459
+ fields {nil , redis.Args {}, nil , 0 , false , false , false , nil },
460
+ args {true },
461
+ & redisearch.AggregateQuery {nil , redis.Args {}, nil , 0 , true , false , false , nil },
462
+ },
463
+ }
464
+ for _ , tt := range tests {
465
+ t .Run (tt .name , func (t * testing.T ) {
466
+ a := & redisearch.AggregateQuery {
467
+ Query : tt .fields .Query ,
468
+ AggregatePlan : tt .fields .AggregatePlan ,
469
+ Paging : tt .fields .Paging ,
470
+ Max : tt .fields .Max ,
471
+ WithSchema : tt .fields .WithSchema ,
472
+ Verbatim : tt .fields .Verbatim ,
473
+ WithCursor : tt .fields .WithCursor ,
474
+ Cursor : tt .fields .Cursor ,
475
+ }
476
+ if got := a .SetWithSchema (tt .args .value ); ! reflect .DeepEqual (got , tt .want ) {
477
+ t .Errorf ("SetWithSchema() = %v, want %v" , got , tt .want )
478
+ }
479
+ })
480
+ }
481
+ }
482
+
483
+ func TestAggregateQuery_CursorHasResults (t * testing.T ) {
484
+ type fields struct {
485
+ Query * redisearch.Query
486
+ AggregatePlan redis.Args
487
+ Paging * redisearch.Paging
488
+ Max int
489
+ WithSchema bool
490
+ Verbatim bool
491
+ WithCursor bool
492
+ Cursor * redisearch.Cursor
493
+ }
494
+ tests := []struct {
495
+ name string
496
+ fields fields
497
+ wantRes bool
498
+ }{
499
+ {"TestAggregateQuery_CursorHasResults_1_false" ,
500
+ fields {nil , redis.Args {}, nil , 0 , false , false , false , nil },
501
+ false ,
502
+ },
503
+ {"TestAggregateQuery_CursorHasResults_1_true" ,
504
+ fields {nil , redis.Args {}, nil , 0 , false , false , false , redisearch .NewCursor ().SetId (10 )},
505
+ true ,
506
+ },
507
+ }
508
+ for _ , tt := range tests {
509
+ t .Run (tt .name , func (t * testing.T ) {
510
+ a := & redisearch.AggregateQuery {
511
+ Query : tt .fields .Query ,
512
+ AggregatePlan : tt .fields .AggregatePlan ,
513
+ Paging : tt .fields .Paging ,
514
+ Max : tt .fields .Max ,
515
+ WithSchema : tt .fields .WithSchema ,
516
+ Verbatim : tt .fields .Verbatim ,
517
+ WithCursor : tt .fields .WithCursor ,
518
+ Cursor : tt .fields .Cursor ,
519
+ }
520
+ if gotRes := a .CursorHasResults (); gotRes != tt .wantRes {
521
+ t .Errorf ("CursorHasResults() = %v, want %v" , gotRes , tt .wantRes )
522
+ }
523
+ })
524
+ }
525
+ }
0 commit comments