Debian Codegraph @

packages/search/lib/commands/AGGREGATE.spec.ts

raw
1 import { strict as assert } from 'assert';
2 import testUtils, { GLOBAL } from '../test-utils';
3 import { AggregateGroupByReducers, AggregateSteps, transformArguments } from './AGGREGATE';
4 import { SchemaFieldTypes } from '.';
5
6 describe('AGGREGATE', () => {
7 describe('transformArguments', () => {
8 it('without options', () => {
9 assert.deepEqual(
10 transformArguments('index', '*'),
11 ['FT.AGGREGATE', 'index', '*']
12 );
13 });
14
15 it('with VERBATIM', () => {
16 assert.deepEqual(
17 transformArguments('index', '*', { VERBATIM: true }),
18 ['FT.AGGREGATE', 'index', '*', 'VERBATIM']
19 );
20 });
21
22 describe('with LOAD', () => {
23 describe('single', () => {
24 describe('without alias', () => {
25 it('string', () => {
26 assert.deepEqual(
27 transformArguments('index', '*', { LOAD: '@property' }),
28 ['FT.AGGREGATE', 'index', '*', 'LOAD', '1', '@property']
29 );
30 });
31
32 it('{ identifier: string }', () => {
33 assert.deepEqual(
34 transformArguments('index', '*', {
35 LOAD: {
36 identifier: '@property'
37 }
38 }),
39 ['FT.AGGREGATE', 'index', '*', 'LOAD', '1', '@property']
40 );
41 });
42 });
43
44 it('with alias', () => {
45 assert.deepEqual(
46 transformArguments('index', '*', {
47 LOAD: {
48 identifier: '@property',
49 AS: 'alias'
50 }
51 }),
52 ['FT.AGGREGATE', 'index', '*', 'LOAD', '3', '@property', 'AS', 'alias']
53 );
54 });
55 });
56
57 it('multiple', () => {
58 assert.deepEqual(
59 transformArguments('index', '*', { LOAD: ['@1', '@2'] }),
60 ['FT.AGGREGATE', 'index', '*', 'LOAD', '2', '@1', '@2']
61 );
62 });
63 });
64
65 describe('with STEPS', () => {
66 describe('GROUPBY', () => {
67 describe('COUNT', () => {
68 describe('without properties', () => {
69 it('without alias', () => {
70 assert.deepEqual(
71 transformArguments('index', '*', {
72 STEPS: [{
73 type: AggregateSteps.GROUPBY,
74 REDUCE: {
75 type: AggregateGroupByReducers.COUNT
76 }
77 }]
78 }),
79 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'COUNT', '0']
80 );
81 });
82
83 it('with alias', () => {
84 assert.deepEqual(
85 transformArguments('index', '*', {
86 STEPS: [{
87 type: AggregateSteps.GROUPBY,
88 REDUCE: {
89 type: AggregateGroupByReducers.COUNT,
90 AS: 'count'
91 }
92 }]
93 }),
94 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'COUNT', '0', 'AS', 'count']
95 );
96 });
97 });
98
99 describe('with properties', () => {
100 it('single', () => {
101 assert.deepEqual(
102 transformArguments('index', '*', {
103 STEPS: [{
104 type: AggregateSteps.GROUPBY,
105 properties: '@property',
106 REDUCE: {
107 type: AggregateGroupByReducers.COUNT
108 }
109 }]
110 }),
111 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '1', '@property', 'REDUCE', 'COUNT', '0']
112 );
113 });
114
115 it('multiple', () => {
116 assert.deepEqual(
117 transformArguments('index', '*', {
118 STEPS: [{
119 type: AggregateSteps.GROUPBY,
120 properties: ['@1', '@2'],
121 REDUCE: {
122 type: AggregateGroupByReducers.COUNT
123 }
124 }]
125 }),
126 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '2', '@1', '@2', 'REDUCE', 'COUNT', '0']
127 );
128 });
129 });
130 });
131
132 it('COUNT_DISTINCT', () => {
133 assert.deepEqual(
134 transformArguments('index', '*', {
135 STEPS: [{
136 type: AggregateSteps.GROUPBY,
137 REDUCE: {
138 type: AggregateGroupByReducers.COUNT_DISTINCT,
139 property: '@property'
140 }
141 }]
142 }),
143 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'COUNT_DISTINCT', '1', '@property']
144 );
145 });
146
147 it('COUNT_DISTINCTISH', () => {
148 assert.deepEqual(
149 transformArguments('index', '*', {
150 STEPS: [{
151 type: AggregateSteps.GROUPBY,
152 REDUCE: {
153 type: AggregateGroupByReducers.COUNT_DISTINCTISH,
154 property: '@property'
155 }
156 }]
157 }),
158 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'COUNT_DISTINCTISH', '1', '@property']
159 );
160 });
161
162 it('SUM', () => {
163 assert.deepEqual(
164 transformArguments('index', '*', {
165 STEPS: [{
166 type: AggregateSteps.GROUPBY,
167 REDUCE: {
168 type: AggregateGroupByReducers.SUM,
169 property: '@property'
170 }
171 }]
172 }),
173 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'SUM', '1', '@property']
174 );
175 });
176
177 it('MIN', () => {
178 assert.deepEqual(
179 transformArguments('index', '*', {
180 STEPS: [{
181 type: AggregateSteps.GROUPBY,
182 REDUCE: {
183 type: AggregateGroupByReducers.MIN,
184 property: '@property'
185 }
186 }]
187 }),
188 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'MIN', '1', '@property']
189 );
190 });
191
192 it('MAX', () => {
193 assert.deepEqual(
194 transformArguments('index', '*', {
195 STEPS: [{
196 type: AggregateSteps.GROUPBY,
197 REDUCE: {
198 type: AggregateGroupByReducers.MAX,
199 property: '@property'
200 }
201 }]
202 }),
203 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'MAX', '1', '@property']
204 );
205 });
206
207 it('AVG', () => {
208 assert.deepEqual(
209 transformArguments('index', '*', {
210 STEPS: [{
211 type: AggregateSteps.GROUPBY,
212 REDUCE: {
213 type: AggregateGroupByReducers.AVG,
214 property: '@property'
215 }
216 }]
217 }),
218 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'AVG', '1', '@property']
219 );
220 });
221
222 it('STDDEV', () => {
223 assert.deepEqual(
224 transformArguments('index', '*', {
225 STEPS: [{
226 type: AggregateSteps.GROUPBY,
227 REDUCE: {
228 type: AggregateGroupByReducers.STDDEV,
229 property: '@property'
230 }
231 }]
232 }),
233 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'STDDEV', '1', '@property']
234 );
235 });
236
237 it('QUANTILE', () => {
238 assert.deepEqual(
239 transformArguments('index', '*', {
240 STEPS: [{
241 type: AggregateSteps.GROUPBY,
242 REDUCE: {
243 type: AggregateGroupByReducers.QUANTILE,
244 property: '@property',
245 quantile: 0.5
246 }
247 }]
248 }),
249 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'QUANTILE', '2', '@property', '0.5']
250 );
251 });
252
253 it('TO_LIST', () => {
254 assert.deepEqual(
255 transformArguments('index', '*', {
256 STEPS: [{
257 type: AggregateSteps.GROUPBY,
258 REDUCE: {
259 type: AggregateGroupByReducers.TO_LIST,
260 property: '@property'
261 }
262 }]
263 }),
264 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'TOLIST', '1', '@property']
265 );
266 });
267
268 describe('FIRST_VALUE', () => {
269 it('simple', () => {
270 assert.deepEqual(
271 transformArguments('index', '*', {
272 STEPS: [{
273 type: AggregateSteps.GROUPBY,
274 REDUCE: {
275 type: AggregateGroupByReducers.FIRST_VALUE,
276 property: '@property'
277 }
278 }]
279 }),
280 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'FIRST_VALUE', '1', '@property']
281 );
282 });
283
284 describe('with BY', () => {
285 describe('without direction', () => {
286 it('string', () => {
287 assert.deepEqual(
288 transformArguments('index', '*', {
289 STEPS: [{
290 type: AggregateSteps.GROUPBY,
291 REDUCE: {
292 type: AggregateGroupByReducers.FIRST_VALUE,
293 property: '@property',
294 BY: '@by'
295 }
296 }]
297 }),
298 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'FIRST_VALUE', '3', '@property', 'BY', '@by']
299 );
300 });
301
302
303 it('{ property: string }', () => {
304 assert.deepEqual(
305 transformArguments('index', '*', {
306 STEPS: [{
307 type: AggregateSteps.GROUPBY,
308 REDUCE: {
309 type: AggregateGroupByReducers.FIRST_VALUE,
310 property: '@property',
311 BY: {
312 property: '@by'
313 }
314 }
315 }]
316 }),
317 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'FIRST_VALUE', '3', '@property', 'BY', '@by']
318 );
319 });
320 });
321
322 it('with direction', () => {
323 assert.deepEqual(
324 transformArguments('index', '*', {
325 STEPS: [{
326 type: AggregateSteps.GROUPBY,
327 REDUCE: {
328 type: AggregateGroupByReducers.FIRST_VALUE,
329 property: '@property',
330 BY: {
331 property: '@by',
332 direction: 'ASC'
333 }
334 }
335 }]
336 }),
337 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'FIRST_VALUE', '4', '@property', 'BY', '@by', 'ASC']
338 );
339 });
340 });
341 });
342
343 it('RANDOM_SAMPLE', () => {
344 assert.deepEqual(
345 transformArguments('index', '*', {
346 STEPS: [{
347 type: AggregateSteps.GROUPBY,
348 REDUCE: {
349 type: AggregateGroupByReducers.RANDOM_SAMPLE,
350 property: '@property',
351 sampleSize: 1
352 }
353 }]
354 }),
355 ['FT.AGGREGATE', 'index', '*', 'GROUPBY', '0', 'REDUCE', 'RANDOM_SAMPLE', '2', '@property', '1']
356 );
357 });
358 });
359
360 describe('SORTBY', () => {
361 it('string', () => {
362 assert.deepEqual(
363 transformArguments('index', '*', {
364 STEPS: [{
365 type: AggregateSteps.SORTBY,
366 BY: '@by'
367 }]
368 }),
369 ['FT.AGGREGATE', 'index', '*', 'SORTBY', '1', '@by']
370 );
371 });
372
373 it('Array', () => {
374 assert.deepEqual(
375 transformArguments('index', '*', {
376 STEPS: [{
377 type: AggregateSteps.SORTBY,
378 BY: ['@1', '@2']
379 }]
380 }),
381 ['FT.AGGREGATE', 'index', '*', 'SORTBY', '2', '@1', '@2']
382 );
383 });
384
385 it('with MAX', () => {
386 assert.deepEqual(
387 transformArguments('index', '*', {
388 STEPS: [{
389 type: AggregateSteps.SORTBY,
390 BY: '@by',
391 MAX: 1
392 }]
393 }),
394 ['FT.AGGREGATE', 'index', '*', 'SORTBY', '1', '@by', 'MAX', '1']
395 );
396 });
397 });
398
399 describe('APPLY', () => {
400 assert.deepEqual(
401 transformArguments('index', '*', {
402 STEPS: [{
403 type: AggregateSteps.APPLY,
404 expression: '@field + 1',
405 AS: 'as'
406 }]
407 }),
408 ['FT.AGGREGATE', 'index', '*', 'APPLY', '@field + 1', 'AS', 'as']
409 );
410 });
411
412 describe('LIMIT', () => {
413 assert.deepEqual(
414 transformArguments('index', '*', {
415 STEPS: [{
416 type: AggregateSteps.LIMIT,
417 from: 0,
418 size: 1
419 }]
420 }),
421 ['FT.AGGREGATE', 'index', '*', 'LIMIT', '0', '1']
422 );
423 });
424
425 describe('FILTER', () => {
426 assert.deepEqual(
427 transformArguments('index', '*', {
428 STEPS: [{
429 type: AggregateSteps.FILTER,
430 expression: '@field != ""'
431 }]
432 }),
433 ['FT.AGGREGATE', 'index', '*', 'FILTER', '@field != ""']
434 );
435 });
436 });
437
438 it('with PARAMS', () => {
439 assert.deepEqual(
440 transformArguments('index', '*', {
441 PARAMS: {
442 param: 'value'
443 }
444 }),
445 ['FT.AGGREGATE', 'index', '*', 'PARAMS', '2', 'param', 'value']
446 );
447 });
448
449 it('with DIALECT', () => {
450 assert.deepEqual(
451 transformArguments('index', '*', {
452 DIALECT: 1
453 }),
454 ['FT.AGGREGATE', 'index', '*', 'DIALECT', '1']
455 );
456 });
457
458 it('with TIMEOUT', () => {
459 assert.deepEqual(
460 transformArguments('index', '*', { TIMEOUT: 10 }),
461 ['FT.AGGREGATE', 'index', '*', 'TIMEOUT', '10']
462 );
463 });
464 });
465
466 testUtils.testWithClient('client.ft.aggregate', async client => {
467 await Promise.all([
468 client.ft.create('index', {
469 field: SchemaFieldTypes.NUMERIC
470 }),
471 client.hSet('1', 'field', '1'),
472 client.hSet('2', 'field', '2')
473 ]);
474
475 assert.deepEqual(
476 await client.ft.aggregate('index', '*', {
477 STEPS: [{
478 type: AggregateSteps.GROUPBY,
479 REDUCE: [{
480 type: AggregateGroupByReducers.SUM,
481 property: '@field',
482 AS: 'sum'
483 }, {
484 type: AggregateGroupByReducers.AVG,
485 property: '@field',
486 AS: 'avg'
487 }]
488 }]
489 }),
490 {
491 total: 1,
492 results: [
493 Object.create(null, {
494 sum: {
495 value: '3',
496 configurable: true,
497 enumerable: true
498 },
499 avg: {
500 value: '1.5',
501 configurable: true,
502 enumerable: true
503 }
504 })
505 ]
506 }
507 );
508 }, GLOBAL.SERVERS.OPEN);
509 });