Debian Codegraph @

tests/js/transformExecutors.test.ts

raw
1 import { SortExecutor, FilterExecutor } from '../../js/core/transformExecutors';
2 import { DataGenerator } from '../js/testUtils';
3 import { ReadonlyJSONValue } from '@lumino/coreutils';
4 import { Transform } from '../../js/core/transform';
5 import { DataGridModel } from '../../js';
6 import { DataSource } from '../../js/datasource';
7
8 const INVALID_DATE = new Date('INVALID');
9
10 const sortTestCases: Private.SortTestCase[] = [
11 // Number
12 { desc: true, dType: 'number', data: [2, 1, 3], expected: [3, 2, 1] },
13 { desc: false, dType: 'number', data: [2, 1, 3], expected: [1, 2, 3] },
14 { desc: false, dType: 'number', data: [2, null, 3], expected: [2, 3, null] },
15 {
16 desc: false,
17 dType: 'number',
18 data: [2, Number.NaN, 3],
19 expected: [2, 3, Number.NaN],
20 },
21 // String
22 {
23 desc: true,
24 dType: 'string',
25 data: ['A', 'C', 'B'],
26 expected: ['C', 'B', 'A'],
27 },
28 {
29 desc: false,
30 dType: 'string',
31 data: ['A', 'C', 'B'],
32 expected: ['A', 'B', 'C'],
33 },
34 {
35 desc: false,
36 dType: 'string',
37 data: ['A', null, 'B'],
38 expected: ['A', 'B', null],
39 },
40 // Boolean
41 {
42 desc: true,
43 dType: 'boolean',
44 data: [true, false, true],
45 expected: [true, true, false],
46 },
47 {
48 desc: false,
49 dType: 'boolean',
50 data: [true, false, true],
51 expected: [false, true, true],
52 },
53 {
54 desc: false,
55 dType: 'boolean',
56 data: [true, null, false],
57 expected: [false, true, null],
58 },
59 // Datetime
60 {
61 desc: true,
62 dType: 'datetime',
63 data: [
64 '2019-09-12T18:38:47.431Z',
65 '2019-09-07T18:38:47.431Z',
66 '2019-09-10T18:38:47.431Z',
67 ],
68 expected: [
69 '2019-09-12T18:38:47.431Z',
70 '2019-09-10T18:38:47.431Z',
71 '2019-09-07T18:38:47.431Z',
72 ],
73 },
74 {
75 desc: false,
76 dType: 'datetime',
77 data: [
78 '2019-09-12T18:38:47.431Z',
79 '2019-09-07T18:38:47.431Z',
80 '2019-09-10T18:38:47.431Z',
81 ],
82 expected: [
83 '2019-09-07T18:38:47.431Z',
84 '2019-09-10T18:38:47.431Z',
85 '2019-09-12T18:38:47.431Z',
86 ],
87 },
88 {
89 desc: false,
90 dType: 'datetime',
91 data: ['2019-09-12T18:38:47.431Z', null, '2019-09-10T18:38:47.431Z'],
92 expected: ['2019-09-10T18:38:47.431Z', '2019-09-12T18:38:47.431Z', null],
93 },
94 {
95 desc: false,
96 dType: 'datetime',
97 data: [
98 '2019-09-12T18:38:47.431Z',
99 INVALID_DATE,
100 '2019-09-10T18:38:47.431Z',
101 ],
102 expected: [
103 '2019-09-10T18:38:47.431Z',
104 '2019-09-12T18:38:47.431Z',
105 INVALID_DATE,
106 ],
107 },
108 // Mixed types (treated as strings)
109 {
110 desc: false,
111 dType: 'string',
112 data: [
113 1,
114 Number.NaN,
115 'B',
116 '2019-09-10T18:38:47.431Z',
117 101.22,
118 1.1,
119 Number.NaN,
120 1.21,
121 1.31,
122 Number.NaN,
123 Number.NaN,
124 1.11,
125 1.21,
126 1.91,
127 'A',
128 9.76,
129 ],
130 expected: [
131 1,
132 1.1,
133 1.11,
134 1.21,
135 1.21,
136 1.31,
137 1.91,
138 101.22,
139 '2019-09-10T18:38:47.431Z',
140 9.76,
141 'A',
142 'B',
143 Number.NaN,
144 Number.NaN,
145 Number.NaN,
146 Number.NaN,
147 ],
148 },
149 ];
150
151 // Run tests
152 describe('Sort Executors', () => {
153 for (let testCase of sortTestCases) {
154 test(`sort-${testCase.dType}-${testCase.desc ? 'desc' : 'asc'}`, () => {
155 const testData = DataGenerator.singleCol({
156 name: 'test',
157 type: testCase.dType,
158 data: testCase.data,
159 });
160 const result = Private.executeSort({
161 field: 'test',
162 desc: testCase.desc,
163 dType: testCase.dType,
164 data: testData,
165 });
166 expect(result).toEqual(testCase.expected);
167 });
168 }
169 });
170
171 const filterTestCases: Private.FilterTestCase[] = [
172 // Number
173 { op: '<', value: 10, dType: 'number', data: [5, 10, 15], expected: [5] },
174 { op: '>', value: 10, dType: 'number', data: [5, 10, 15], expected: [15] },
175 {
176 op: '<=',
177 value: 10,
178 dType: 'number',
179 data: [5, 10, 15],
180 expected: [5, 10],
181 },
182 {
183 op: '>=',
184 value: 10,
185 dType: 'number',
186 data: [5, 10, 15],
187 expected: [10, 15],
188 },
189 {
190 op: 'empty',
191 value: null,
192 dType: 'number',
193 data: [5, null, 15],
194 expected: [null],
195 },
196 {
197 op: 'notempty',
198 value: null,
199 dType: 'number',
200 data: [5, null, 15],
201 expected: [5, 15],
202 },
203 {
204 op: 'in',
205 value: [5, 10],
206 dType: 'number',
207 data: [5, 10, 15],
208 expected: [5, 10],
209 },
210 {
211 op: 'between',
212 value: [7, 12],
213 dType: 'number',
214 data: [5, 10, 15],
215 expected: [10],
216 },
217 // String
218 {
219 op: '<',
220 value: 'b',
221 dType: 'string',
222 data: ['a', 'b', 'c'],
223 expected: ['a'],
224 },
225 {
226 op: '>',
227 value: 'b',
228 dType: 'string',
229 data: ['a', 'b', 'c'],
230 expected: ['c'],
231 },
232 {
233 op: 'empty',
234 value: null,
235 dType: 'string',
236 data: ['a', null, 'c'],
237 expected: [null],
238 },
239 {
240 op: 'notempty',
241 value: null,
242 dType: 'string',
243 data: ['a', null, 'c'],
244 expected: ['a', 'c'],
245 },
246 {
247 op: 'in',
248 value: ['a', 'b'],
249 dType: 'string',
250 data: ['a', 'b', 'c'],
251 expected: ['a', 'b'],
252 },
253 {
254 op: 'between',
255 value: ['a', 'e'],
256 dType: 'string',
257 data: ['a', 'b', 'c', 'd', 'e'],
258 expected: ['b', 'c', 'd'],
259 },
260 {
261 op: 'startswith',
262 value: 'a',
263 dType: 'string',
264 data: ['ab', 'ba', 'ca'],
265 expected: ['ab'],
266 },
267 {
268 op: 'endswith',
269 value: 'a',
270 dType: 'string',
271 data: ['ab', 'ba', 'ca'],
272 expected: ['ba', 'ca'],
273 },
274 {
275 op: 'contains',
276 value: 'rr',
277 dType: 'string',
278 data: ['apple', 'berry', 'cherry'],
279 expected: ['berry', 'cherry'],
280 },
281 {
282 op: '!contains',
283 value: 'rr',
284 dType: 'string',
285 data: ['apple', 'berry', 'cherry'],
286 expected: ['apple'],
287 },
288 // Boolean
289 {
290 op: 'empty',
291 value: null,
292 dType: 'boolean',
293 data: [true, null, false],
294 expected: [null],
295 },
296 {
297 op: 'notempty',
298 value: null,
299 dType: 'boolean',
300 data: [true, null, false],
301 expected: [true, false],
302 },
303 // Datetime
304 {
305 op: '<',
306 value: '2019-09-11',
307 dType: 'datetime',
308 data: [
309 '2019-09-10T18:38:47.431Z',
310 '2019-09-11T18:38:47.431Z',
311 '2019-09-12T18:38:47.431Z',
312 ],
313 expected: ['2019-09-10T18:38:47.431Z'],
314 },
315 {
316 op: '>',
317 value: '2019-09-11',
318 dType: 'datetime',
319 data: [
320 '2019-09-10T18:38:47.431Z',
321 '2019-09-11T18:38:47.431Z',
322 '2019-09-12T18:38:47.431Z',
323 ],
324 expected: ['2019-09-12T18:38:47.431Z'],
325 },
326 {
327 op: '<=',
328 value: '2019-09-11',
329 dType: 'datetime',
330 data: [
331 '2019-09-10T18:38:47.431Z',
332 '2019-09-11T18:38:47.431Z',
333 '2019-09-12T18:38:47.431Z',
334 ],
335 expected: ['2019-09-10T18:38:47.431Z', '2019-09-11T18:38:47.431Z'],
336 },
337 {
338 op: '>=',
339 value: '2019-09-11',
340 dType: 'datetime',
341 data: [
342 '2019-09-10T18:38:47.431Z',
343 '2019-09-11T18:38:47.431Z',
344 '2019-09-12T18:38:47.431Z',
345 ],
346 expected: ['2019-09-11T18:38:47.431Z', '2019-09-12T18:38:47.431Z'],
347 },
348 {
349 op: 'empty',
350 value: null,
351 dType: 'datetime',
352 data: ['2019-09-10T18:38:47.431Z', null, '2019-09-12T18:38:47.431Z'],
353 expected: [null],
354 },
355 {
356 op: 'notempty',
357 value: null,
358 dType: 'datetime',
359 data: ['2019-09-10T18:38:47.431Z', null, '2019-09-12T18:38:47.431Z'],
360 expected: ['2019-09-10T18:38:47.431Z', '2019-09-12T18:38:47.431Z'],
361 },
362 {
363 op: 'in',
364 value: ['2019-09-10T18:38:47.431Z', '2019-09-12T18:38:47.431Z'],
365 dType: 'datetime',
366 data: [
367 '2019-09-10T18:38:47.431Z',
368 '2019-09-11T18:38:47.431Z',
369 '2019-09-12T18:38:47.431Z',
370 ],
371 expected: ['2019-09-10T18:38:47.431Z', '2019-09-12T18:38:47.431Z'],
372 },
373 {
374 op: 'between',
375 value: ['2019-09-02T18:38:47.431Z', '2019-09-04T18:38:47.431Z'],
376 dType: 'datetime',
377 data: [
378 '2019-09-01T18:38:47.431Z',
379 '2019-09-03T18:38:47.431Z',
380 '2019-09-06T18:38:47.431Z',
381 ],
382 expected: ['2019-09-03T18:38:47.431Z'],
383 },
384 {
385 op: '=',
386 value: '2019-09-10T18:38:47.431Z',
387 dType: 'datetime',
388 data: [
389 '2019-09-10T11:38:47.431Z',
390 '2019-09-10T18:38:47.431Z',
391 '2019-09-10T12:38:47.431Z',
392 ],
393 expected: ['2019-09-10T18:38:47.431Z'],
394 },
395 {
396 op: 'isOnSameDay',
397 value: '2019-09-10',
398 dType: 'datetime',
399 data: [
400 '2019-09-10T11:38:47.431Z',
401 '2019-09-10T18:38:47.431Z',
402 '2019-09-12T12:38:47.431Z',
403 ],
404 expected: ['2019-09-10T11:38:47.431Z', '2019-09-10T18:38:47.431Z'],
405 },
406 ];
407
408 // Run tests
409 describe('Filter Executors', () => {
410 for (let testCase of filterTestCases) {
411 test(`filter-${testCase.dType}-${testCase.op}`, () => {
412 const testData = DataGenerator.singleCol({
413 name: 'test',
414 type: testCase.dType,
415 data: testCase.data,
416 });
417 const result = Private.executeFilter({
418 field: 'test',
419 dType: testCase.dType,
420 data: testData,
421 operator: testCase.op,
422 value: testCase.value,
423 });
424 expect(result).toEqual(testCase.expected);
425 });
426 }
427 });
428
429 /**
430 * The namespace for the module implementation details.
431 */
432 namespace Private {
433 /**
434 * Returns an array containing the data from a single column of the provided
435 * table.
436 *
437 * @param options - Options for calling this function.
438 */
439 export function getDataByField(
440 options: IGetDataByFieldOptions,
441 ): ReadonlyJSONValue[] {
442 return options.data.data[options.field];
443 }
444
445 /**
446 * Creates a `SortExecutor` and executes it with the provided options.
447 *
448 * @param options - Options for calling this function.
449 */
450 export function executeSort(
451 options: IExecuteSortOptions,
452 ): ReadonlyJSONValue[] {
453 const testObject = new SortExecutor({
454 field: options.field,
455 dType: options.dType,
456 desc: options.desc,
457 });
458 const result = testObject.apply(options.data.data);
459 return Private.getDataByField({ data: result, field: options.field });
460 }
461
462 /**
463 * Creates a `FilterExecutor` and executes it with the provided options.
464 *
465 * @param options - Options for calling this function.
466 */
467 export function executeFilter(
468 options: IExecuteFilterOptions,
469 ): ReadonlyJSONValue[] {
470 const testObject = new FilterExecutor({
471 field: options.field,
472 dType: options.dType,
473 operator: options.operator,
474 value: options.value,
475 });
476 const result = testObject.apply(options.data.data);
477 return Private.getDataByField({ data: result, field: options.field });
478 }
479
480 /**
481 * An options object for returning an array of data from a table.
482 */
483 export interface IGetDataByFieldOptions {
484 /**
485 * The table to operate on.
486 */
487 data: Readonly<DataSource>;
488
489 /**
490 * The name of the field to operate on.
491 */
492 field: string;
493 }
494
495 /**
496 * An options object for executing a sort operation.
497 */
498 export interface IExecuteSortOptions {
499 /**
500 * The table to operate on.
501 */
502 data: DataGridModel.IData;
503
504 /**
505 * The name of the field to operate on.
506 */
507 field: string;
508
509 /**
510 * Indicates if the sort should be in descending or ascending order
511 */
512 desc: boolean;
513
514 /**
515 * The dtype of the column being operated on.
516 */
517 dType: string;
518 }
519
520 /**
521 * An options object for executing a filter operation.
522 */
523 export interface IExecuteFilterOptions {
524 /**
525 * The table to operate on.
526 */
527 data: DataGridModel.IData;
528
529 /**
530 * The name of the field to operate on.
531 */
532 field: string;
533
534 /**
535 * The operator to use for this transform.
536 */
537 operator: Transform.FilterOperator;
538
539 /**
540 * The dtype of the column being operated on.
541 */
542 dType: string;
543
544 /**
545 * The value to filter by.
546 */
547 value: any;
548 }
549
550 /**
551 * An interface that defines the data needed for a filter test case.
552 */
553 export interface FilterTestCase {
554 /**
555 * The operator to use for this transform.
556 */
557 op: Transform.FilterOperator;
558
559 /**
560 * The value to filter by.
561 */
562 value: any;
563
564 /**
565 * The dtype of the column being operated on.
566 */
567 dType: string;
568
569 /**
570 * The data used to create the table for testing.
571 */
572 data: any[];
573
574 /**
575 * The expected data to be returned after the test.
576 */
577 expected: any[];
578 }
579
580 /**
581 * An interface that defines the data needed for a sort test case.
582 */
583 export interface SortTestCase {
584 /**
585 * Indicates if the sort should be in descending or ascending order
586 */
587 desc: boolean;
588
589 /**
590 * The dtype of the column being operated on.
591 */
592 dType: string;
593
594 /**
595 * The data used to create the table for testing.
596 */
597 data: any[];
598
599 /**
600 * The expected data to be returned after the test.
601 */
602 expected: any[];
603 }
604 }