(autoIndex: `off` | `eager`)
| 223 | } |
| 224 | |
| 225 | function createGroupByTests(autoIndex: `off` | `eager`): void { |
| 226 | describe(`with autoIndex ${autoIndex}`, () => { |
| 227 | describe(`Single Column Grouping`, () => { |
| 228 | let ordersCollection: ReturnType<typeof createOrdersCollection> |
| 229 | |
| 230 | beforeEach(() => { |
| 231 | ordersCollection = createOrdersCollection(autoIndex) |
| 232 | }) |
| 233 | |
| 234 | test(`group by customer_id with aggregates`, () => { |
| 235 | const customerSummary = createLiveQueryCollection({ |
| 236 | startSync: true, |
| 237 | query: (q) => |
| 238 | q |
| 239 | .from({ orders: ordersCollection }) |
| 240 | .groupBy(({ orders }) => orders.customer_id) |
| 241 | .select(({ orders }) => ({ |
| 242 | customer_id: orders.customer_id, |
| 243 | total_amount: sum(orders.amount), |
| 244 | order_count: count(orders.id), |
| 245 | avg_amount: avg(orders.amount), |
| 246 | min_amount: min(orders.amount), |
| 247 | max_amount: max(orders.amount), |
| 248 | min_date: min(orders.date), |
| 249 | max_date: max(orders.date), |
| 250 | })), |
| 251 | }) |
| 252 | |
| 253 | expect(customerSummary.size).toBe(3) // 3 customers |
| 254 | |
| 255 | // Customer 1: orders 1, 2, 7 (amounts: 100, 200, 400) |
| 256 | const customer1 = customerSummary.get(1) |
| 257 | expect(customer1).toBeDefined() |
| 258 | expect(customer1?.customer_id).toBe(1) |
| 259 | expect(customer1?.total_amount).toBe(700) |
| 260 | expect(customer1?.order_count).toBe(3) |
| 261 | expect(customer1?.avg_amount).toBe(233.33333333333334) // (100+200+400)/3 |
| 262 | expect(customer1?.min_amount).toBe(100) |
| 263 | expect(customer1?.max_amount).toBe(400) |
| 264 | expect(customer1?.min_date.toISOString()).toBe( |
| 265 | new Date(`2023-01-01`).toISOString(), |
| 266 | ) |
| 267 | expect(customer1?.max_date.toISOString()).toBe( |
| 268 | new Date(`2023-03-01`).toISOString(), |
| 269 | ) |
| 270 | |
| 271 | // Customer 2: orders 3, 4 (amounts: 150, 300) |
| 272 | const customer2 = customerSummary.get(2) |
| 273 | expect(customer2).toBeDefined() |
| 274 | expect(customer2?.customer_id).toBe(2) |
| 275 | expect(customer2?.total_amount).toBe(450) |
| 276 | expect(customer2?.order_count).toBe(2) |
| 277 | expect(customer2?.avg_amount).toBe(225) // (150+300)/2 |
| 278 | expect(customer2?.min_amount).toBe(150) |
| 279 | expect(customer2?.max_amount).toBe(300) |
| 280 | expect(customer2?.min_date.toISOString()).toBe( |
| 281 | new Date(`2023-01-20`).toISOString(), |
| 282 | ) |
no test coverage detected
searching dependent graphs…