Skip to content

Commit 04e94b0

Browse files
committed
cleanup live queries
1 parent 9fbbb40 commit 04e94b0

File tree

10 files changed

+192
-7
lines changed

10 files changed

+192
-7
lines changed

packages/db-collection-e2e/src/suites/accumulated-data.suite.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export function createAccumulatedDataTestSuite(
2929

3030
// Test structure: Delete a record, verify it's removed
3131
expect(query.size).toBeGreaterThan(0)
32+
33+
await query.cleanup()
3234
})
3335
})
3436

@@ -52,6 +54,8 @@ export function createAccumulatedDataTestSuite(
5254
results.forEach((u) => {
5355
expect(u.deletedAt).toBeNull()
5456
})
57+
58+
await query.cleanup()
5559
})
5660

5761
it(`should include soft-deleted records when not filtered`, async () => {
@@ -71,6 +75,8 @@ export function createAccumulatedDataTestSuite(
7175

7276
expect(hasNotDeleted).toBe(true)
7377
// May or may not have deleted records depending on seed data
78+
79+
await query.cleanup()
7480
})
7581
})
7682

@@ -89,6 +95,8 @@ export function createAccumulatedDataTestSuite(
8995
// Test structure: Update a record in database
9096
// Query should receive update reactively
9197
expect(query.size).toBeGreaterThan(0)
98+
99+
await query.cleanup()
92100
})
93101
})
94102
})

packages/db-collection-e2e/src/suites/collation.suite.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export function createCollationTestSuite(
2929

3030
const results = Array.from(query.state.values())
3131
expect(results.length).toBeGreaterThan(0)
32+
33+
await query.cleanup()
3234
})
3335

3436
it(`should handle case-sensitive comparisons by default`, async () => {
@@ -48,6 +50,8 @@ export function createCollationTestSuite(
4850
const results = Array.from(query.state.values())
4951
// Should match lowercase variant from seed data
5052
expect(results.length).toBeGreaterThanOrEqual(0)
53+
54+
await query.cleanup()
5155
})
5256
})
5357

@@ -66,6 +70,8 @@ export function createCollationTestSuite(
6670
await query.preload()
6771

6872
expect(query.compareOptions.stringSort).toBe(`lexical`)
73+
74+
await query.cleanup()
6975
})
7076

7177
it(`should support locale-based collation`, async () => {
@@ -86,6 +92,8 @@ export function createCollationTestSuite(
8692
if (query.compareOptions.stringSort === `locale`) {
8793
expect(query.compareOptions.locale).toBe(`de-DE`)
8894
}
95+
96+
await query.cleanup()
8997
})
9098
})
9199

@@ -103,6 +111,8 @@ export function createCollationTestSuite(
103111
await query.preload()
104112

105113
expect(query.compareOptions.stringSort).toBe(`lexical`)
114+
115+
await query.cleanup()
106116
})
107117
})
108118

@@ -128,6 +138,8 @@ export function createCollationTestSuite(
128138
// Just verify it's sorted, regardless of collation
129139
expect(results[i - 1]!.name).toBeTruthy()
130140
}
141+
142+
await query.cleanup()
131143
})
132144
})
133145
})

packages/db-collection-e2e/src/suites/deduplication.suite.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export function createDeduplicationTestSuite(
3737
// Both should have same results
3838
expect(query1.size).toBe(query2.size)
3939
expect(query1.size).toBeGreaterThanOrEqual(0)
40+
41+
await Promise.all([query1.cleanup(), query2.cleanup()])
4042
})
4143

4244
it(`should deduplicate multiple identical queries`, async () => {
@@ -61,6 +63,8 @@ export function createDeduplicationTestSuite(
6163
queries.forEach((q) => {
6264
expect(q.size).toBe(firstSize!)
6365
})
66+
67+
await Promise.all(queries.map((q) => q.cleanup()))
6468
})
6569
})
6670

@@ -88,6 +92,8 @@ export function createDeduplicationTestSuite(
8892

8993
// Query 2 results should be subset of Query 1
9094
expect(query2.size).toBeLessThanOrEqual(query1.size)
95+
96+
await Promise.all([query1.cleanup(), query2.cleanup()])
9197
})
9298

9399
it(`should handle non-overlapping predicates correctly`, async () => {
@@ -114,6 +120,8 @@ export function createDeduplicationTestSuite(
114120
// Both should execute independently
115121
expect(query1.size).toBeGreaterThanOrEqual(0)
116122
expect(query2.size).toBeGreaterThanOrEqual(0)
123+
124+
await Promise.all([query1.cleanup(), query2.cleanup()])
117125
})
118126
})
119127

@@ -123,21 +131,23 @@ export function createDeduplicationTestSuite(
123131
const usersCollection = config.collections.onDemand.users
124132

125133
// Start first query
126-
const query1Promise = createLiveQueryCollection((q) =>
134+
const query1 = createLiveQueryCollection((q) =>
127135
q
128136
.from({ user: usersCollection })
129137
.where(({ user }) => eq(user.isActive, true))
130-
).preload()
138+
)
131139

132140
// Immediately start identical second query
133-
const query2Promise = createLiveQueryCollection((q) =>
141+
const query2 = createLiveQueryCollection((q) =>
134142
q
135143
.from({ user: usersCollection })
136144
.where(({ user }) => eq(user.isActive, true))
137-
).preload()
145+
)
138146

139147
// Both should complete successfully
140-
await Promise.all([query1Promise, query2Promise])
148+
await Promise.all([query1.preload(), query2.preload()])
149+
150+
await Promise.all([query1.cleanup(), query2.cleanup()])
141151
})
142152

143153
it(`should handle query arriving mid-flight with different predicate`, async () => {
@@ -160,6 +170,8 @@ export function createDeduplicationTestSuite(
160170

161171
// Both execute correctly
162172
expect(query1.size).toBeGreaterThanOrEqual(query2.size)
173+
174+
await Promise.all([query1.cleanup(), query2.cleanup()])
163175
})
164176
})
165177

@@ -190,6 +202,8 @@ export function createDeduplicationTestSuite(
190202

191203
expect(query1.size).toBe(query2.size)
192204
expect(query1.size).toBe(10)
205+
206+
await Promise.all([query1.cleanup(), query2.cleanup()])
193207
})
194208

195209
it(`should handle queries with different limits`, async () => {
@@ -216,6 +230,8 @@ export function createDeduplicationTestSuite(
216230

217231
expect(query1.size).toBe(10)
218232
expect(query2.size).toBe(20)
233+
234+
await Promise.all([query1.cleanup(), query2.cleanup()])
219235
})
220236

221237
it(`should handle queries with different offsets`, async () => {
@@ -249,6 +265,8 @@ export function createDeduplicationTestSuite(
249265
const ids1 = Array.from(query1.state.values()).map((u) => u.id)
250266
const ids2 = Array.from(query2.state.values()).map((u) => u.id)
251267
expect(ids1).not.toEqual(ids2)
268+
269+
await Promise.all([query1.cleanup(), query2.cleanup()])
252270
})
253271
})
254272

@@ -275,6 +293,8 @@ export function createDeduplicationTestSuite(
275293
queries.forEach((q) => {
276294
expect(q.size).toBe(firstSize!)
277295
})
296+
297+
await Promise.all(queries.map((q) => q.cleanup()))
278298
})
279299

280300
it(`should not corrupt data with concurrent queries`, async () => {
@@ -311,6 +331,8 @@ export function createDeduplicationTestSuite(
311331
queries.forEach((q) => {
312332
expect(q.size).toBeGreaterThanOrEqual(0)
313333
})
334+
335+
await Promise.all(queries.map((q) => q.cleanup()))
314336
})
315337
})
316338
})

packages/db-collection-e2e/src/suites/joins.suite.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export function createJoinsTestSuite(getConfig: () => Promise<E2ETestConfig>) {
3737
expect(results.length).toBeGreaterThan(0)
3838
expect(results[0]!).toHaveProperty(`userName`)
3939
expect(results[0]!).toHaveProperty(`postTitle`)
40+
41+
await query.cleanup()
4042
})
4143

4244
it(`should join with predicates on both collections`, async () => {
@@ -67,6 +69,8 @@ export function createJoinsTestSuite(getConfig: () => Promise<E2ETestConfig>) {
6769
results.forEach((r) => {
6870
expect(r.viewCount).toBeGreaterThan(10)
6971
})
72+
73+
await query.cleanup()
7074
})
7175

7276
it(`should join with one eager, one on-demand`, async () => {
@@ -94,6 +98,8 @@ export function createJoinsTestSuite(getConfig: () => Promise<E2ETestConfig>) {
9498

9599
const results = Array.from(query.state.values())
96100
expect(results.length).toBeGreaterThan(0)
101+
102+
await query.cleanup()
97103
})
98104

99105
it(`should join with ordering across collections`, async () => {
@@ -141,6 +147,8 @@ export function createJoinsTestSuite(getConfig: () => Promise<E2ETestConfig>) {
141147
const currCount = results[i]!.viewCount
142148
expect(prevCount).toBeGreaterThanOrEqual(currCount)
143149
}
150+
151+
await query.cleanup()
144152
})
145153

146154
it(`should join with pagination`, async () => {
@@ -167,6 +175,8 @@ export function createJoinsTestSuite(getConfig: () => Promise<E2ETestConfig>) {
167175
await query.preload()
168176

169177
expect(query.size).toBeLessThanOrEqual(10)
178+
179+
await query.cleanup()
170180
})
171181
})
172182

@@ -198,6 +208,8 @@ export function createJoinsTestSuite(getConfig: () => Promise<E2ETestConfig>) {
198208
expect(results[0]!).toHaveProperty(`userName`)
199209
expect(results[0]!).toHaveProperty(`postTitle`)
200210
expect(results[0]!).toHaveProperty(`commentText`)
211+
212+
await query.cleanup()
201213
})
202214

203215
it(`should handle predicates on all three collections`, async () => {
@@ -227,6 +239,8 @@ export function createJoinsTestSuite(getConfig: () => Promise<E2ETestConfig>) {
227239
const results = Array.from(query.state.values())
228240
// All results should match all predicates
229241
expect(results.length).toBeGreaterThanOrEqual(0)
242+
243+
await query.cleanup()
230244
})
231245

232246
it(`should handle mixed syncModes in 3-way join`, async () => {
@@ -259,6 +273,8 @@ export function createJoinsTestSuite(getConfig: () => Promise<E2ETestConfig>) {
259273

260274
const results = Array.from(query.state.values())
261275
expect(results.length).toBeGreaterThan(0)
276+
277+
await query.cleanup()
262278
})
263279
})
264280

@@ -287,6 +303,8 @@ export function createJoinsTestSuite(getConfig: () => Promise<E2ETestConfig>) {
287303
results.forEach((r) => {
288304
expect(r.viewCount).toBeGreaterThan(50)
289305
})
306+
307+
await query.cleanup()
290308
})
291309

292310
it(`should not over-fetch in joined collections`, async () => {
@@ -313,6 +331,8 @@ export function createJoinsTestSuite(getConfig: () => Promise<E2ETestConfig>) {
313331
results.forEach((r) => {
314332
expect(r.userAge).toBeGreaterThan(30)
315333
})
334+
335+
await query.cleanup()
316336
})
317337
})
318338

@@ -339,6 +359,8 @@ export function createJoinsTestSuite(getConfig: () => Promise<E2ETestConfig>) {
339359

340360
const results = Array.from(query.state.values())
341361
expect(results.length).toBeGreaterThan(0)
362+
363+
await query.cleanup()
342364
})
343365
})
344366
})

packages/db-collection-e2e/src/suites/live-updates.suite.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export function createLiveUpdatesTestSuite(
2828

2929
// Test structure: After backend insert/update, query should update reactively
3030
expect(initialSize).toBeGreaterThanOrEqual(0)
31+
32+
await query.cleanup()
3133
})
3234

3335
it(`should add new records that match query predicate`, async () => {
@@ -45,6 +47,8 @@ export function createLiveUpdatesTestSuite(
4547
// Test structure: Insert record with age=35 in database
4648
// Should appear in query results reactively
4749
expect(query.size).toBeGreaterThanOrEqual(0)
50+
51+
await query.cleanup()
4852
})
4953

5054
it(`should remove records that no longer match predicate`, async () => {
@@ -62,6 +66,8 @@ export function createLiveUpdatesTestSuite(
6266
// Test structure: Update backend record from age=35 to age=25
6367
// Should be removed from query results
6468
expect(query.size).toBeGreaterThanOrEqual(0)
69+
70+
await query.cleanup()
6571
})
6672
})
6773

@@ -85,6 +91,8 @@ export function createLiveUpdatesTestSuite(
8591
expect(changeCount).toBeGreaterThanOrEqual(0)
8692

8793
subscription.unsubscribe()
94+
95+
await query.cleanup()
8896
})
8997
})
9098

@@ -110,6 +118,8 @@ export function createLiveUpdatesTestSuite(
110118
// Both queries should work independently
111119
expect(query1.size).toBeGreaterThanOrEqual(0)
112120
expect(query2.size).toBeGreaterThanOrEqual(0)
121+
122+
await Promise.all([query1.cleanup(), query2.cleanup()])
113123
})
114124
})
115125
})

packages/db-collection-e2e/src/suites/mutations.suite.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export function createMutationsTestSuite(
2828
// Note: Actual insert would use collection.insert()
2929
// This test structure is ready for when mutation APIs are available
3030
expect(initialSize).toBeGreaterThanOrEqual(0)
31+
32+
await query.cleanup()
3133
})
3234

3335
it(`should handle insert appearing in matching queries`, async () => {
@@ -44,6 +46,8 @@ export function createMutationsTestSuite(
4446

4547
// Test structure ready for insert testing
4648
expect(query.size).toBeGreaterThanOrEqual(0)
49+
50+
await query.cleanup()
4751
})
4852
})
4953

@@ -64,6 +68,8 @@ export function createMutationsTestSuite(
6468
// Test structure: Update a user from age=25 to age=35
6569
// Should appear in query results
6670
expect(initialSize).toBeGreaterThanOrEqual(0)
71+
72+
await query.cleanup()
6773
})
6874

6975
it(`should handle update that makes record unmatch predicate`, async () => {
@@ -81,6 +87,8 @@ export function createMutationsTestSuite(
8187
// Test structure: Update a user from age=35 to age=25
8288
// Should be removed from query results
8389
expect(query.size).toBeGreaterThanOrEqual(0)
90+
91+
await query.cleanup()
8492
})
8593
})
8694

@@ -101,6 +109,8 @@ export function createMutationsTestSuite(
101109

102110
// Test structure: Mutations should maintain pagination state
103111
expect(query.size).toBeLessThanOrEqual(10)
112+
113+
await query.cleanup()
104114
})
105115
})
106116
})

0 commit comments

Comments
 (0)