|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { createCollection } from '../../src/collection/index.js' |
| 3 | +import { |
| 4 | + and, |
| 5 | + createLiveQueryCollection, |
| 6 | + eq, |
| 7 | + gte, |
| 8 | +} from '../../src/query/index.js' |
| 9 | +import { PropRef, Value } from '../../src/query/ir.js' |
| 10 | +import type { Collection } from '../../src/collection/index.js' |
| 11 | +import type { |
| 12 | + LoadSubsetOptions, |
| 13 | + NonSingleResult, |
| 14 | + UtilsRecord, |
| 15 | +} from '../../src/types.js' |
| 16 | +import type { OrderBy } from '../../src/query/ir.js' |
| 17 | + |
| 18 | +// Sample types for testing |
| 19 | +type Order = { |
| 20 | + id: number |
| 21 | + scheduled_at: string |
| 22 | + status: string |
| 23 | + address_id: number |
| 24 | +} |
| 25 | + |
| 26 | +type Charge = { |
| 27 | + id: number |
| 28 | + address_id: number |
| 29 | + amount: number |
| 30 | +} |
| 31 | + |
| 32 | +// Sample data |
| 33 | +const sampleOrders: Array<Order> = [ |
| 34 | + { |
| 35 | + id: 1, |
| 36 | + scheduled_at: `2024-01-15`, |
| 37 | + status: `queued`, |
| 38 | + address_id: 1, |
| 39 | + }, |
| 40 | + { |
| 41 | + id: 2, |
| 42 | + scheduled_at: `2024-01-10`, |
| 43 | + status: `queued`, |
| 44 | + address_id: 2, |
| 45 | + }, |
| 46 | + { |
| 47 | + id: 3, |
| 48 | + scheduled_at: `2024-01-20`, |
| 49 | + status: `completed`, |
| 50 | + address_id: 1, |
| 51 | + }, |
| 52 | +] |
| 53 | + |
| 54 | +const sampleCharges: Array<Charge> = [ |
| 55 | + { id: 1, address_id: 1, amount: 100 }, |
| 56 | + { id: 2, address_id: 2, amount: 200 }, |
| 57 | +] |
| 58 | + |
| 59 | +type ChargersCollection = Collection< |
| 60 | + Charge, |
| 61 | + string | number, |
| 62 | + UtilsRecord, |
| 63 | + never, |
| 64 | + Charge |
| 65 | +> & |
| 66 | + NonSingleResult |
| 67 | + |
| 68 | +type OrdersCollection = Collection< |
| 69 | + Order, |
| 70 | + string | number, |
| 71 | + UtilsRecord, |
| 72 | + never, |
| 73 | + Order |
| 74 | +> & |
| 75 | + NonSingleResult |
| 76 | + |
| 77 | +describe(`loadSubset with subqueries`, () => { |
| 78 | + let chargesCollection: ChargersCollection |
| 79 | + |
| 80 | + beforeEach(() => { |
| 81 | + // Create charges collection |
| 82 | + chargesCollection = createCollection<Charge>({ |
| 83 | + id: `charges`, |
| 84 | + getKey: (charge) => charge.id, |
| 85 | + sync: { |
| 86 | + sync: ({ begin, write, commit, markReady }) => { |
| 87 | + begin() |
| 88 | + for (const charge of sampleCharges) { |
| 89 | + write({ type: `insert`, value: charge }) |
| 90 | + } |
| 91 | + commit() |
| 92 | + markReady() |
| 93 | + }, |
| 94 | + }, |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + function createOrdersCollectionWithTracking(): { |
| 99 | + collection: OrdersCollection |
| 100 | + loadSubsetCalls: Array<LoadSubsetOptions> |
| 101 | + } { |
| 102 | + const loadSubsetCalls: Array<LoadSubsetOptions> = [] |
| 103 | + |
| 104 | + const collection = createCollection<Order>({ |
| 105 | + id: `orders`, |
| 106 | + getKey: (order) => order.id, |
| 107 | + syncMode: `on-demand`, |
| 108 | + sync: { |
| 109 | + sync: ({ begin, write, commit, markReady }) => { |
| 110 | + begin() |
| 111 | + for (const order of sampleOrders) { |
| 112 | + write({ type: `insert`, value: order }) |
| 113 | + } |
| 114 | + commit() |
| 115 | + markReady() |
| 116 | + return { |
| 117 | + loadSubset: vi.fn((options: LoadSubsetOptions) => { |
| 118 | + loadSubsetCalls.push(options) |
| 119 | + return Promise.resolve() |
| 120 | + }), |
| 121 | + } |
| 122 | + }, |
| 123 | + }, |
| 124 | + }) |
| 125 | + |
| 126 | + return { collection, loadSubsetCalls } |
| 127 | + } |
| 128 | + |
| 129 | + it(`should call loadSubset with where clause for direct query`, async () => { |
| 130 | + const today = `2024-01-12` |
| 131 | + const { collection: ordersCollection, loadSubsetCalls } = |
| 132 | + createOrdersCollectionWithTracking() |
| 133 | + |
| 134 | + const directQuery = createLiveQueryCollection((q) => |
| 135 | + q |
| 136 | + .from({ order: ordersCollection }) |
| 137 | + .where(({ order }) => gte(order.scheduled_at, today)) |
| 138 | + .where(({ order }) => eq(order.status, `queued`)), |
| 139 | + ) |
| 140 | + |
| 141 | + await directQuery.preload() |
| 142 | + |
| 143 | + // Verify loadSubset was called |
| 144 | + expect(loadSubsetCalls.length).toBeGreaterThan(0) |
| 145 | + |
| 146 | + // Verify the last call (or any call) has the where clause |
| 147 | + const lastCall = loadSubsetCalls[loadSubsetCalls.length - 1] |
| 148 | + expect(lastCall).toBeDefined() |
| 149 | + expect(lastCall!.where).toBeDefined() |
| 150 | + |
| 151 | + const expectedWhereClause = and( |
| 152 | + gte(new PropRef([`scheduled_at`]), new Value(today)), |
| 153 | + eq(new PropRef([`status`]), new Value(`queued`)), |
| 154 | + ) |
| 155 | + |
| 156 | + expect(lastCall!.where).toEqual(expectedWhereClause) |
| 157 | + }) |
| 158 | + |
| 159 | + it(`should call loadSubset with where clause for subquery`, async () => { |
| 160 | + const today = `2024-01-12` |
| 161 | + const { collection: ordersCollection, loadSubsetCalls } = |
| 162 | + createOrdersCollectionWithTracking() |
| 163 | + |
| 164 | + const subqueryQuery = createLiveQueryCollection((q) => { |
| 165 | + // Build subquery with filters |
| 166 | + const prepaidOrderQ = q |
| 167 | + .from({ prepaidOrder: ordersCollection }) |
| 168 | + .where(({ prepaidOrder }) => gte(prepaidOrder.scheduled_at, today)) |
| 169 | + .where(({ prepaidOrder }) => eq(prepaidOrder.status, `queued`)) |
| 170 | + |
| 171 | + // Use subquery in main query |
| 172 | + return q |
| 173 | + .from({ charge: chargesCollection }) |
| 174 | + .fullJoin({ prepaidOrder: prepaidOrderQ }, ({ charge, prepaidOrder }) => |
| 175 | + eq(charge.address_id, prepaidOrder.address_id), |
| 176 | + ) |
| 177 | + }) |
| 178 | + |
| 179 | + await subqueryQuery.preload() |
| 180 | + |
| 181 | + // Verify loadSubset was called for the orders collection |
| 182 | + expect(loadSubsetCalls.length).toBeGreaterThan(0) |
| 183 | + |
| 184 | + // Verify the last call (or any call) has the where clause |
| 185 | + const lastCall = loadSubsetCalls[loadSubsetCalls.length - 1] |
| 186 | + expect(lastCall).toBeDefined() |
| 187 | + expect(lastCall!.where).toBeDefined() |
| 188 | + |
| 189 | + const expectedWhereClause = and( |
| 190 | + gte(new PropRef([`scheduled_at`]), new Value(today)), |
| 191 | + eq(new PropRef([`status`]), new Value(`queued`)), |
| 192 | + ) |
| 193 | + |
| 194 | + expect(lastCall!.where).toEqual(expectedWhereClause) |
| 195 | + }) |
| 196 | + |
| 197 | + it(`should call loadSubset with orderBy clause for direct query`, async () => { |
| 198 | + const { collection: ordersCollection, loadSubsetCalls } = |
| 199 | + createOrdersCollectionWithTracking() |
| 200 | + |
| 201 | + const directQuery = createLiveQueryCollection((q) => |
| 202 | + q |
| 203 | + .from({ order: ordersCollection }) |
| 204 | + .orderBy(({ order }) => order.scheduled_at, `desc`) |
| 205 | + .limit(2), |
| 206 | + ) |
| 207 | + |
| 208 | + await directQuery.preload() |
| 209 | + |
| 210 | + // Verify loadSubset was called |
| 211 | + expect(loadSubsetCalls.length).toBeGreaterThan(0) |
| 212 | + |
| 213 | + // Verify the last call has the orderBy clause and limit |
| 214 | + const lastCall = loadSubsetCalls[loadSubsetCalls.length - 1] |
| 215 | + expect(lastCall).toBeDefined() |
| 216 | + expect(lastCall!.orderBy).toBeDefined() |
| 217 | + expect(lastCall!.limit).toBe(2) |
| 218 | + |
| 219 | + const expectedOrderBy: OrderBy = [ |
| 220 | + { |
| 221 | + expression: new PropRef([`scheduled_at`]), |
| 222 | + compareOptions: { direction: `desc`, nulls: `first` }, |
| 223 | + }, |
| 224 | + ] |
| 225 | + |
| 226 | + expect(lastCall!.orderBy).toEqual(expectedOrderBy) |
| 227 | + }) |
| 228 | + |
| 229 | + it(`should call loadSubset with orderBy clause for subquery`, async () => { |
| 230 | + const { collection: ordersCollection, loadSubsetCalls } = |
| 231 | + createOrdersCollectionWithTracking() |
| 232 | + |
| 233 | + const subqueryQuery = createLiveQueryCollection((q) => { |
| 234 | + // Build subquery with orderBy and limit |
| 235 | + const prepaidOrderQ = q |
| 236 | + .from({ prepaidOrder: ordersCollection }) |
| 237 | + .orderBy(({ prepaidOrder }) => prepaidOrder.scheduled_at, `desc`) |
| 238 | + .limit(2) |
| 239 | + |
| 240 | + // Use subquery in main query |
| 241 | + return q |
| 242 | + .from({ charge: chargesCollection }) |
| 243 | + .fullJoin({ prepaidOrder: prepaidOrderQ }, ({ charge, prepaidOrder }) => |
| 244 | + eq(charge.address_id, prepaidOrder.address_id), |
| 245 | + ) |
| 246 | + }) |
| 247 | + |
| 248 | + await subqueryQuery.preload() |
| 249 | + |
| 250 | + // Verify loadSubset was called for the orders collection |
| 251 | + expect(loadSubsetCalls.length).toBeGreaterThan(0) |
| 252 | + |
| 253 | + // Verify the last call has the orderBy clause and limit |
| 254 | + const lastCall = loadSubsetCalls[loadSubsetCalls.length - 1] |
| 255 | + expect(lastCall).toBeDefined() |
| 256 | + expect(lastCall!.orderBy).toBeDefined() |
| 257 | + expect(lastCall!.limit).toBe(2) |
| 258 | + |
| 259 | + const expectedOrderBy: OrderBy = [ |
| 260 | + { |
| 261 | + expression: new PropRef([`scheduled_at`]), |
| 262 | + compareOptions: { direction: `desc`, nulls: `first` }, |
| 263 | + }, |
| 264 | + ] |
| 265 | + |
| 266 | + expect(lastCall!.orderBy).toEqual(expectedOrderBy) |
| 267 | + }) |
| 268 | +}) |
0 commit comments