Open History to see the last commit
1import { ShapeStream } from '@electric-sql/client'
2import { IR } from '@tanstack/db'
3import { afterAll, beforeAll, describe, expect, inject, it } from 'vitest'
4import { makePgClient } from '../../db-collection-e2e/support/global-setup'
5import { compileSQL } from '../src/sql-compiler'
6import type { SubsetParams } from '@electric-sql/client'
7import type { Client } from 'pg'
8
9type TestRow = Record<string, unknown> & { id: number }
10
11describe(`Electric subset SQL acceptance`, () => {
12 let client: Client
13 let baseUrl: string
14 let schema: string
15 let table: string
16 let qualifiedTable: string
17
18 const ref = (column: string) => new IR.PropRef([column])
19 const call = (name: string, ...args: Array<IR.BasicExpression>) =>
20 new IR.Func<boolean>(name, args)
21
22 async function selectedIds(params: SubsetParams): Promise<Array<number>> {
23 const stream = new ShapeStream<TestRow>({
24 url: `${baseUrl}/v1/shape`,
25 params: { table: qualifiedTable },
26 log: `changes_only`,
27 })
28 const snapshot = await stream.fetchSnapshot(params)
29 return snapshot.data.map(({ value }) => value.id)
30 }
31
32 beforeAll(async () => {
33 baseUrl = inject(`baseUrl`)
34 schema = inject(`testSchema`)
35 table = `subset_sql_acceptance_${Date.now().toString(16)}`
36 qualifiedTable = `${schema}.${table}`
37 client = makePgClient()
38 await client.connect()
39 await client.query(
40 `CREATE TABLE "${schema}"."${table}" (
41 id INTEGER PRIMARY KEY,
42 roles TEXT[],
43 integer_roles INTEGER[],
44 required_role VARCHAR(16)
45 )`,
46 )
47 await client.query(
48 `INSERT INTO "${schema}"."${table}" VALUES
49 (1, ARRAY['admin'], ARRAY[1, 2], 'admin'),
50 (2, ARRAY['viewer'], ARRAY[3], 'admin'),
51 (3, NULL, NULL, NULL),
52 (4, ARRAY[NULL]::TEXT[], ARRAY[NULL]::INTEGER[], 'admin')`,
53 )
54 })
55
56 afterAll(async () => {
57 await client.query(`DROP TABLE IF EXISTS "${schema}"."${table}"`)
58 await client.end()
59 })
60
61 it(`accepts generated plain-column predicates`, async () => {
62 const equality = compileSQL({
63 where: call(`eq`, ref(`id`), new IR.Value(1)),
64 })
65 expect(await selectedIds(equality)).toEqual([1])
66
67 const textMembership = compileSQL({
68 where: call(`in`, new IR.Value(`admin`), ref(`roles`)),
69 })
70 expect(await selectedIds(textMembership)).toEqual([1])
71 expect(
72 await selectedIds(
73 compileSQL({
74 where: call(`not`, call(`in`, new IR.Value(`admin`), ref(`roles`))),
75 orderBy: [
76 {
77 expression: ref(`id`),
78 compareOptions: { direction: `asc`, nulls: `last` },
79 },
80 ],
81 }),
82 ),
83 ).toEqual([2, 3, 4])
84
85 const integerMembership = compileSQL({
86 where: call(`in`, new IR.Value(2), ref(`integer_roles`)),
87 })
88 expect(await selectedIds(integerMembership)).toEqual([1])
89 })
90
91 it(`preserves compatible ref-to-array membership`, async () => {
92 const membership = compileSQL({
93 where: call(`in`, ref(`required_role`), ref(`roles`)),
94 })
95 const ids = await selectedIds(membership)
96 expect(membership.where).toBe(`"required_role" = ANY("roles")`)
97 expect(ids).toEqual([1])
98 })
99
100 it(`accepts generated plain-column ordering`, async () => {
101 const ordered = compileSQL({
102 orderBy: [
103 {
104 expression: ref(`id`),
105 compareOptions: { direction: `desc`, nulls: `last` },
106 },
107 ],
108 limit: 2,
109 })
110 expect(await selectedIds(ordered)).toEqual([4, 3])
111 })
112})