Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

orderBy.test.ts

527 lines13.6 KB

1import { describe, expect, test } from 'vitest'
2import { D2 } from '../../src/d2.js'
3import { MultiSet } from '../../src/multiset.js'
4import { orderBy, output } from '../../src/operators/index.js'
5import type { KeyValue } from '../../src/types.js'
6​
7describe(`Operators`, () => {
8 describe(`OrderBy operation`, () => {
9 test(`initial results with default comparator`, () => {
10 const graph = new D2()
11 const input = graph.newInput<
12 KeyValue<
13 string,
14 {
15 id: number
16 value: string
17 }
18 >
19 >()
20 let latestMessage: any = null
21​
22 input.pipe(
23 orderBy((item) => item.value),
24 output((message) => {
25 latestMessage = message
26 }),
27 )
28​
29 graph.finalize()
30​
31 input.sendData(
32 new MultiSet([
33 [[`key1`, { id: 1, value: `a` }], 1],
34 [[`key2`, { id: 2, value: `z` }], 1],
35 [[`key3`, { id: 3, value: `b` }], 1],
36 [[`key4`, { id: 4, value: `y` }], 1],
37 [[`key5`, { id: 5, value: `c` }], 1],
38 ]),
39 )
40​
41 graph.run()
42​
43 expect(latestMessage).not.toBeNull()
44​
45 const result = latestMessage.getInner()
46​
47 expect(sortResults(result)).toEqual([
48 [[`key1`, { id: 1, value: `a` }], 1],
49 [[`key2`, { id: 2, value: `z` }], 1],
50 [[`key3`, { id: 3, value: `b` }], 1],
51 [[`key4`, { id: 4, value: `y` }], 1],
52 [[`key5`, { id: 5, value: `c` }], 1],
53 ])
54 })
55​
56 test(`initial results with custom comparator`, () => {
57 const graph = new D2()
58 const input = graph.newInput<
59 KeyValue<
60 string,
61 {
62 id: number
63 value: string
64 }
65 >
66 >()
67 let latestMessage: any = null
68​
69 input.pipe(
70 orderBy((item) => item.value, {
71 comparator: (a, b) => b.localeCompare(a), // reverse order
72 }),
73 output((message) => {
74 latestMessage = message
75 }),
76 )
77​
78 graph.finalize()
79​
80 input.sendData(
81 new MultiSet([
82 [[`key1`, { id: 1, value: `a` }], 1],
83 [[`key2`, { id: 2, value: `z` }], 1],
84 [[`key3`, { id: 3, value: `b` }], 1],
85 [[`key4`, { id: 4, value: `y` }], 1],
86 [[`key5`, { id: 5, value: `c` }], 1],
87 ]),
88 )
89​
90 graph.run()
91​
92 expect(latestMessage).not.toBeNull()
93​
94 const result = latestMessage.getInner()
95​
96 expect(sortResults(result)).toEqual([
97 [[`key1`, { id: 1, value: `a` }], 1],
98 [[`key2`, { id: 2, value: `z` }], 1],
99 [[`key3`, { id: 3, value: `b` }], 1],
100 [[`key4`, { id: 4, value: `y` }], 1],
101 [[`key5`, { id: 5, value: `c` }], 1],
102 ])
103 })
104​
105 test(`initial results with limit`, () => {
106 const graph = new D2()
107 const input = graph.newInput<
108 KeyValue<
109 string,
110 {
111 id: number
112 value: string
113 }
114 >
115 >()
116 let latestMessage: any = null
117​
118 input.pipe(
119 orderBy((item) => item.value, { limit: 3 }),
120 output((message) => {
121 latestMessage = message
122 }),
123 )
124​
125 graph.finalize()
126​
127 input.sendData(
128 new MultiSet([
129 [[`key1`, { id: 1, value: `a` }], 1],
130 [[`key2`, { id: 2, value: `z` }], 1],
131 [[`key3`, { id: 3, value: `b` }], 1],
132 [[`key4`, { id: 4, value: `y` }], 1],
133 [[`key5`, { id: 5, value: `c` }], 1],
134 ]),
135 )
136​
137 graph.run()
138​
139 expect(latestMessage).not.toBeNull()
140​
141 const result = latestMessage.getInner()
142​
143 expect(sortResults(result)).toEqual([
144 [[`key1`, { id: 1, value: `a` }], 1],
145 [[`key3`, { id: 3, value: `b` }], 1],
146 [[`key5`, { id: 5, value: `c` }], 1],
147 ])
148 })
149​
150 test(`initial results with limit and offset`, () => {
151 const graph = new D2()
152 const input = graph.newInput<
153 KeyValue<
154 string,
155 {
156 id: number
157 value: string
158 }
159 >
160 >()
161 let latestMessage: any = null
162​
163 input.pipe(
164 orderBy((item) => item.value, { limit: 2, offset: 2 }),
165 output((message) => {
166 latestMessage = message
167 }),
168 )
169​
170 graph.finalize()
171​
172 input.sendData(
173 new MultiSet([
174 [[`key1`, { id: 1, value: `a` }], 1],
175 [[`key2`, { id: 2, value: `z` }], 1],
176 [[`key3`, { id: 3, value: `b` }], 1],
177 [[`key4`, { id: 4, value: `y` }], 1],
178 [[`key5`, { id: 5, value: `c` }], 1],
179 ]),
180 )
181​
182 graph.run()
183​
184 expect(latestMessage).not.toBeNull()
185​
186 const result = latestMessage.getInner()
187​
188 expect(sortResults(result)).toEqual([
189 [[`key4`, { id: 4, value: `y` }], 1],
190 [[`key5`, { id: 5, value: `c` }], 1],
191 ])
192 })
193​
194 test(`ordering by numeric property`, () => {
195 const graph = new D2()
196 const input = graph.newInput<
197 KeyValue<
198 string,
199 {
200 id: number
201 value: string
202 }
203 >
204 >()
205 let latestMessage: any = null
206​
207 input.pipe(
208 orderBy((item) => item.id),
209 output((message) => {
210 latestMessage = message
211 }),
212 )
213​
214 graph.finalize()
215​
216 input.sendData(
217 new MultiSet([
218 [[`key1`, { id: 5, value: `a` }], 1],
219 [[`key2`, { id: 3, value: `z` }], 1],
220 [[`key3`, { id: 1, value: `b` }], 1],
221 [[`key4`, { id: 4, value: `y` }], 1],
222 [[`key5`, { id: 2, value: `c` }], 1],
223 ]),
224 )
225​
226 graph.run()
227​
228 expect(latestMessage).not.toBeNull()
229​
230 const result = latestMessage.getInner()
231​
232 expect(sortResults(result)).toEqual([
233 [[`key1`, { id: 5, value: `a` }], 1],
234 [[`key2`, { id: 3, value: `z` }], 1],
235 [[`key3`, { id: 1, value: `b` }], 1],
236 [[`key4`, { id: 4, value: `y` }], 1],
237 [[`key5`, { id: 2, value: `c` }], 1],
238 ])
239 })
240​
241 test(`incremental update - adding new rows`, () => {
242 const graph = new D2()
243 const input = graph.newInput<
244 KeyValue<
245 string,
246 {
247 id: number
248 value: string
249 }
250 >
251 >()
252 let latestMessage: any = null
253​
254 input.pipe(
255 orderBy((item) => item.value),
256 output((message) => {
257 latestMessage = message
258 }),
259 )
260​
261 graph.finalize()
262​
263 // Initial data
264 input.sendData(
265 new MultiSet([
266 [[`key1`, { id: 1, value: `c` }], 1],
267 [[`key2`, { id: 2, value: `d` }], 1],
268 [[`key3`, { id: 3, value: `e` }], 1],
269 ]),
270 )
271 graph.run()
272​
273 // Initial result should be all three items in alphabetical order
274 let result = latestMessage.getInner()
275 expect(sortResults(result)).toEqual([
276 [[`key1`, { id: 1, value: `c` }], 1],
277 [[`key2`, { id: 2, value: `d` }], 1],
278 [[`key3`, { id: 3, value: `e` }], 1],
279 ])
280​
281 // Add new rows that should appear in the result
282 input.sendData(
283 new MultiSet([
284 [[`key4`, { id: 4, value: `a` }], 1],
285 [[`key5`, { id: 5, value: `b` }], 1],
286 ]),
287 )
288 graph.run()
289​
290 // Result should now include the new rows in the correct order
291 result = latestMessage.getInner()
292 expect(sortResults(result)).toEqual([
293 [[`key4`, { id: 4, value: `a` }], 1], // New row
294 [[`key5`, { id: 5, value: `b` }], 1], // New row
295 ])
296 })
297​
298 test(`incremental update - removing rows`, () => {
299 const graph = new D2()
300 const input = graph.newInput<
301 KeyValue<
302 string,
303 {
304 id: number
305 value: string
306 }
307 >
308 >()
309 let latestMessage: any = null
310​
311 input.pipe(
312 orderBy((item) => item.value),
313 output((message) => {
314 latestMessage = message
315 }),
316 )
317​
318 graph.finalize()
319​
320 // Initial data
321 input.sendData(
322 new MultiSet([
323 [[`key1`, { id: 1, value: `a` }], 1],
324 [[`key2`, { id: 2, value: `b` }], 1],
325 [[`key3`, { id: 3, value: `c` }], 1],
326 [[`key4`, { id: 4, value: `d` }], 1],
327 ]),
328 )
329 graph.run()
330​
331 // Initial result should be all four items
332 let result = latestMessage.getInner()
333 expect(sortResults(result)).toEqual([
334 [[`key1`, { id: 1, value: `a` }], 1],
335 [[`key2`, { id: 2, value: `b` }], 1],
336 [[`key3`, { id: 3, value: `c` }], 1],
337 [[`key4`, { id: 4, value: `d` }], 1],
338 ])
339​
340 // Remove 'b' from the result set
341 input.sendData(new MultiSet([[[`key2`, { id: 2, value: `b` }], -1]]))
342 graph.run()
343​
344 // Result should show 'b' being removed
345 result = latestMessage.getInner()
346 expect(sortResults(result)).toEqual([
347 [[`key2`, { id: 2, value: `b` }], -1], // Removed row
348 ])
349 })
350​
351 test(`incremental update - with limit`, () => {
352 const graph = new D2()
353 const input = graph.newInput<
354 KeyValue<
355 string,
356 {
357 id: number
358 value: string
359 }
360 >
361 >()
362 let latestMessage: any = null
363​
364 input.pipe(
365 orderBy((item) => item.value, { limit: 3 }),
366 output((message) => {
367 latestMessage = message
368 }),
369 )
370​
371 graph.finalize()
372​
373 // Initial data
374 input.sendData(
375 new MultiSet([
376 [[`key1`, { id: 1, value: `c` }], 1],
377 [[`key2`, { id: 2, value: `d` }], 1],
378 [[`key3`, { id: 3, value: `e` }], 1],
379 ]),
380 )
381 graph.run()
382​
383 // Initial result should be all three items
384 let result = latestMessage.getInner()
385 expect(sortResults(result)).toEqual([
386 [[`key1`, { id: 1, value: `c` }], 1],
387 [[`key2`, { id: 2, value: `d` }], 1],
388 [[`key3`, { id: 3, value: `e` }], 1],
389 ])
390​
391 // Add a new row that should appear in the result (before 'c')
392 input.sendData(new MultiSet([[[`key4`, { id: 4, value: `a` }], 1]]))
393 graph.run()
394​
395 // Result should now include 'a' and drop 'e' due to limit
396 result = latestMessage.getInner()
397 expect(sortResults(result)).toEqual([
398 [[`key3`, { id: 3, value: `e` }], -1], // Moved out of the limit so it's removed
399 [[`key4`, { id: 4, value: `a` }], 1], // New row at the beginning
400 ])
401 })
402​
403 test(`incremental update - with limit and offset`, () => {
404 const graph = new D2()
405 const input = graph.newInput<
406 KeyValue<
407 string,
408 {
409 id: number
410 value: string
411 }
412 >
413 >()
414 let latestMessage: any = null
415​
416 input.pipe(
417 orderBy((item) => item.value, { limit: 2, offset: 1 }),
418 output((message) => {
419 latestMessage = message
420 }),
421 )
422​
423 graph.finalize()
424​
425 // Initial data
426 input.sendData(
427 new MultiSet([
428 [[`key1`, { id: 1, value: `a` }], 1],
429 [[`key2`, { id: 2, value: `b` }], 1],
430 [[`key3`, { id: 3, value: `c` }], 1],
431 [[`key4`, { id: 4, value: `d` }], 1],
432 ]),
433 )
434 graph.run()
435​
436 // Initial result should be items at positions 1 and 2 (b and c)
437 let result = latestMessage.getInner()
438 expect(sortResults(result)).toEqual([
439 [[`key2`, { id: 2, value: `b` }], 1],
440 [[`key3`, { id: 3, value: `c` }], 1],
441 ])
442​
443 // Add a new row that should appear at the beginning
444 input.sendData(new MultiSet([[[`key5`, { id: 5, value: `_` }], 1]]))
445 graph.run()
446​
447 // Result should now shift: a is out, _ is in at offset 1, b is still in, c is out
448 result = latestMessage.getInner()
449 expect(sortResults(result)).toEqual([
450 [[`key1`, { id: 1, value: `a` }], 1], // Now in the window due to offset
451 [[`key3`, { id: 3, value: `c` }], -1], // Moved out due to window shift
452 ])
453 })
454​
455 test(`incremental update - modifying existing rows`, () => {
456 const graph = new D2()
457 const input = graph.newInput<
458 KeyValue<
459 string,
460 {
461 id: number
462 value: string
463 }
464 >
465 >()
466 let latestMessage: any = null
467​
468 input.pipe(
469 orderBy((item) => item.value),
470 output((message) => {
471 latestMessage = message
472 }),
473 )
474​
475 graph.finalize()
476​
477 // Initial data
478 input.sendData(
479 new MultiSet([
480 [[`key1`, { id: 1, value: `a` }], 1],
481 [[`key2`, { id: 2, value: `c` }], 1],
482 [[`key3`, { id: 3, value: `e` }], 1],
483 ]),
484 )
485 graph.run()
486​
487 // Initial result
488 let result = latestMessage.getInner()
489 expect(sortResults(result)).toEqual([
490 [[`key1`, { id: 1, value: `a` }], 1],
491 [[`key2`, { id: 2, value: `c` }], 1],
492 [[`key3`, { id: 3, value: `e` }], 1],
493 ])
494​
495 // Modify an existing row by removing it and adding a new version
496 input.sendData(
497 new MultiSet([
498 [[`key2`, { id: 2, value: `c` }], -1], // Remove old version
499 [[`key2`, { id: 2, value: `z` }], 1], // Add new version with different value
500 ]),
501 )
502 graph.run()
503​
504 // Result should show the modification
505 result = latestMessage.getInner()
506 expect(sortResults(result)).toEqual([
507 [[`key2`, { id: 2, value: `c` }], -1], // Old version removed
508 [[`key2`, { id: 2, value: `z` }], 1], // New version added
509 ])
510 })
511 })
512})
513​
514/**
515 * Sort results by multiplicity and then key
516 */
517function sortResults(results: Array<any>) {
518 return [...results]
519 .sort(
520 ([[_aKey, _aValue], aMultiplicity], [[_bKey, _bValue], bMultiplicity]) =>
521 aMultiplicity - bMultiplicity,
522 )
523 .sort(
524 ([[aKey, _aValue], _aMultiplicity], [[bKey, _bValue], _bMultiplicity]) =>
525 aKey.localeCompare(bKey),
526 )