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

distinct.test.ts

233 lines5.7 KB

1import { describe, expect, test } from "vitest"
2import { D2 } from "../../src/d2.js"
3import { MultiSet } from "../../src/multiset.js"
4import { distinct } from "../../src/operators/distinct.js"
5import { output } from "../../src/operators/output.js"
6import { MessageTracker, assertResults } from "../test-utils.js"
7​
8describe(`Operators`, () => {
9 describe(`Efficient distinct operation`, () => {
10 testDistinct()
11 })
12})
13​
14function testDistinct() {
15 test(`basic distinct operation`, () => {
16 const graph = new D2()
17 const input = graph.newInput<[number, string]>()
18 const messages: Array<MultiSet<[number, string]>> = []
19​
20 input.pipe(
21 distinct(),
22 output((message) => {
23 messages.push(message)
24 })
25 )
26​
27 graph.finalize()
28​
29 input.sendData(
30 new MultiSet([
31 [[1, `a`], 2],
32 [[2, `b`], 1],
33 [[2, `c`], 2],
34 ])
35 )
36 graph.run()
37​
38 const data = messages.map((m) => m.getInner())
39​
40 expect(data).toEqual([
41 [
42 [[1, `a`], 1],
43 [[2, `b`], 1],
44 [[2, `c`], 1],
45 ],
46 ])
47 })
48​
49 test(`distinct by certain property`, () => {
50 const graph = new D2()
51 const input = graph.newInput<[number, { name: string; country: string }]>()
52 const messages: Array<
53 MultiSet<[number, { name: string; country: string }]>
54 > = []
55​
56 input.pipe(
57 distinct(([_, value]) => value.country),
58 output((message) => {
59 messages.push(message)
60 })
61 )
62​
63 graph.finalize()
64​
65 input.sendData(
66 new MultiSet([
67 [[1, { name: `Valter`, country: `Portugal` }], 1],
68 [[2, { name: `Sam`, country: `UK` }], 1],
69 [[2, { name: `Kevin`, country: `Belgium` }], 1],
70 [[3, { name: `Garry`, country: `UK` }], 1],
71 [[4, { name: `Kyle`, country: `USA` }], 1],
72 ])
73 )
74​
75 graph.run()
76​
77 const data = messages.map((m) => m.getInner())[0]
78 const countries = data
79 .map(([[_, value], multiplicity]) => [value.country, multiplicity])
80 .sort()
81​
82 expect(countries).toEqual(
83 [
84 [`Belgium`, 1],
85 [`Portugal`, 1],
86 [`UK`, 1],
87 [`USA`, 1],
88 ].sort()
89 )
90 })
91​
92 test(`distinct with updates`, () => {
93 const graph = new D2()
94 const input = graph.newInput<[number, string]>()
95 const tracker = new MessageTracker<[number, string]>()
96​
97 input.pipe(
98 distinct(),
99 output((message) => {
100 tracker.addMessage(message)
101 })
102 )
103​
104 graph.finalize()
105​
106 // Initial batch
107 input.sendData(
108 new MultiSet([
109 [[1, `a`], 1],
110 [[1, `b`], 1],
111 [[1, `a`], 1], // Duplicate, should only result in 1
112 ])
113 )
114 graph.run()
115​
116 const initialResult = tracker.getResult()
117 assertResults(
118 `distinct with updates - initial`,
119 initialResult,
120 [
121 [1, `a`],
122 [1, `b`],
123 ], // Should have both distinct values
124 4 // Max expected messages
125 )
126​
127 tracker.reset()
128​
129 // Second batch - remove some, add new
130 input.sendData(
131 new MultiSet([
132 [[1, `b`], -1], // Remove 'b'
133 [[1, `c`], 2], // Add 'c' (multiplicity should be capped at 1)
134 [[1, `a`], -1], // Remove 'a'
135 ])
136 )
137 graph.run()
138​
139 const secondResult = tracker.getResult()
140 assertResults(
141 `distinct with updates - second batch`,
142 secondResult,
143 [[1, `c`]], // Should only have 'c' remaining
144 4 // Max expected messages
145 )
146​
147 tracker.reset()
148​
149 // Third batch - remove remaining
150 input.sendData(new MultiSet([[[1, `c`], -2]]))
151 graph.run()
152​
153 const thirdResult = tracker.getResult()
154 assertResults(
155 `distinct with updates - third batch`,
156 thirdResult,
157 [], // Should have no remaining distinct values
158 2 // Max expected messages
159 )
160 })
161​
162 test(`distinct with multiple batches of same key`, () => {
163 const graph = new D2()
164 const input = graph.newInput<[string, number]>()
165 const messages: Array<MultiSet<[string, number]>> = []
166​
167 input.pipe(
168 distinct(),
169 output((message) => {
170 messages.push(message)
171 })
172 )
173​
174 graph.finalize()
175​
176 input.sendData(
177 new MultiSet([
178 [[`key1`, 1], 2],
179 [[`key1`, 2], 3],
180 [[`key2`, 1], 1],
181 ])
182 )
183 graph.run()
184​
185 const data = messages.map((m) => m.getInner())
186​
187 expect(data).toEqual([
188 [
189 [[`key1`, 1], 1],
190 [[`key1`, 2], 1],
191 [[`key2`, 1], 1],
192 ],
193 ])
194 })
195​
196 test(`distinct with multiple batches of same key that cancel out`, () => {
197 const graph = new D2()
198 const input = graph.newInput<[string, number]>()
199 const tracker = new MessageTracker<[string, number]>()
200​
201 input.pipe(
202 distinct(),
203 output((message) => {
204 tracker.addMessage(message)
205 })
206 )
207​
208 graph.finalize()
209​
210 input.sendData(
211 new MultiSet([
212 [[`key1`, 1], 2], // Add ['key1', 1] with multiplicity 2 -> should become 1 (distinct)
213 [[`key1`, 2], 2], // Add ['key1', 2] with multiplicity 2 -> should become 1 (distinct)
214 [[`key1`, 2], 1], // Add more ['key1', 2] with multiplicity 1 -> total 3, still 1 in distinct
215 [[`key2`, 1], 1], // Add ['key2', 1] with multiplicity 1 -> should become 1 (distinct)
216 [[`key1`, 2], -3], // Remove all ['key1', 2] (total was 3) -> should be removed from distinct
217 [[`key2`, 1], 1], // Add more ['key2', 1] -> still 1 in distinct
218 ])
219 )
220 graph.run()
221​
222 const result = tracker.getResult()
223 assertResults(
224 `distinct with multiple batches that cancel out`,
225 result,
226 [
227 [`key1`, 1], // Should remain (multiplicity 2 -> 1 in distinct)
228 [`key2`, 1], // Should remain (multiplicity 2 -> 1 in distinct)
229 ],
230 6 // Max expected messages (generous upper bound)
231 )
232 })