Skip to content

Commit 5369c52

Browse files
committed
More tests
1 parent f650cd6 commit 5369c52

File tree

2 files changed

+134
-4
lines changed

2 files changed

+134
-4
lines changed

packages/common/tests/db/schema/Schema.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ describe('Schema', () => {
8080
content: column.text
8181
})
8282
});
83+
schema.withRawTables({
84+
lists: {
85+
put: {
86+
sql: 'SELECT 1',
87+
params: [{ Column: 'foo' }]
88+
},
89+
delete: {
90+
sql: 'SELECT 2',
91+
params: ['Id']
92+
}
93+
}
94+
});
8395

8496
const json = schema.toJSON();
8597

@@ -115,6 +127,19 @@ describe('Schema', () => {
115127
],
116128
indexes: []
117129
}
130+
],
131+
raw_tables: [
132+
{
133+
name: 'lists',
134+
delete: {
135+
sql: 'SELECT 2',
136+
params: ['Id']
137+
},
138+
put: {
139+
sql: 'SELECT 1',
140+
params: [{ Column: 'foo' }]
141+
}
142+
}
118143
]
119144
});
120145
});

packages/web/tests/stream.test.ts

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ import {
33
createBaseLogger,
44
DataStream,
55
PowerSyncConnectionOptions,
6+
Schema,
7+
SyncClientImplementation,
68
SyncStreamConnectionMethod,
79
WASQLiteOpenFactory,
8-
WASQLiteVFS
10+
WASQLiteVFS,
11+
WebPowerSyncOpenFactoryOptions
912
} from '@powersync/web';
1013
import { describe, expect, it, onTestFinished, vi } from 'vitest';
1114
import { TestConnector } from './utils/MockStreamOpenFactory';
1215
import { ConnectedDatabaseUtils, generateConnectedDatabase } from './utils/generateConnectedDatabase';
16+
import { v4 } from 'uuid';
1317

1418
const UPLOAD_TIMEOUT_MS = 3000;
1519

@@ -22,10 +26,11 @@ describe('Streaming', { sequential: true }, () => {
2226
{
2327
sequential: true
2428
},
25-
describeStreamingTests(() =>
29+
describeStreamingTests((options) =>
2630
generateConnectedDatabase({
2731
powerSyncOptions: {
28-
logger
32+
logger,
33+
...options
2934
}
3035
})
3136
)
@@ -160,9 +165,109 @@ describe('Streaming', { sequential: true }, () => {
160165
await expectUserRows(2);
161166
});
162167
});
168+
169+
// There are more tests for raw tables in the node package and in the core extension itself. We just want to make
170+
// sure the schema options are properly forwarded.
171+
it('raw tables smoke test', async () => {
172+
const customSchema = new Schema({});
173+
customSchema.withRawTables({
174+
lists: {
175+
put: {
176+
sql: 'INSERT OR REPLACE INTO lists (id, name) VALUES (?, ?)',
177+
params: ['Id', { Column: 'name' }]
178+
},
179+
delete: {
180+
sql: 'DELETE FROM lists WHERE id = ?',
181+
params: ['Id']
182+
}
183+
}
184+
});
185+
186+
function bucket(name: string, count: number): BucketChecksum {
187+
return {
188+
bucket: name,
189+
count,
190+
checksum: 0,
191+
priority: 3
192+
};
193+
}
194+
195+
const { powersync, waitForStream, remote } = await generateConnectedDatabase({
196+
powerSyncOptions: { schema: customSchema, flags: { enableMultiTabs: true } }
197+
});
198+
await powersync.execute('CREATE TABLE lists (id TEXT NOT NULL PRIMARY KEY, name TEXT);');
199+
onTestFinished(async () => {
200+
await powersync.execute('DROP TABLE lists');
201+
});
202+
203+
const query = powersync.watchWithAsyncGenerator('SELECT * FROM lists')[Symbol.asyncIterator]();
204+
expect((await query.next()).value.rows._array).toStrictEqual([]);
205+
206+
powersync.connect(new TestConnector(), {
207+
connectionMethod: SyncStreamConnectionMethod.HTTP,
208+
clientImplementation: SyncClientImplementation.RUST
209+
});
210+
await waitForStream();
211+
212+
remote.enqueueLine({
213+
checkpoint: {
214+
last_op_id: '1',
215+
buckets: [bucket('a', 1)]
216+
}
217+
});
218+
remote.enqueueLine({
219+
data: {
220+
bucket: 'a',
221+
data: [
222+
{
223+
checksum: 0,
224+
op_id: '1',
225+
op: 'PUT',
226+
object_id: 'my_list',
227+
object_type: 'lists',
228+
data: '{"name": "custom list"}'
229+
}
230+
]
231+
}
232+
});
233+
remote.enqueueLine({ checkpoint_complete: { last_op_id: '1' } });
234+
await powersync.waitForFirstSync();
235+
236+
console.log('has first sync, should update list');
237+
expect((await query.next()).value.rows._array).toStrictEqual([{ id: 'my_list', name: 'custom list' }]);
238+
239+
remote.enqueueLine({
240+
checkpoint: {
241+
last_op_id: '2',
242+
buckets: [bucket('a', 2)]
243+
}
244+
});
245+
await vi.waitFor(() => powersync.currentStatus.dataFlowStatus.downloading == true);
246+
remote.enqueueLine({
247+
data: {
248+
bucket: 'a',
249+
data: [
250+
{
251+
checksum: 0,
252+
op_id: '2',
253+
op: 'REMOVE',
254+
object_id: 'my_list',
255+
object_type: 'lists'
256+
}
257+
]
258+
}
259+
});
260+
remote.enqueueLine({ checkpoint_complete: { last_op_id: '2' } });
261+
await vi.waitFor(() => powersync.currentStatus.dataFlowStatus.downloading == false);
262+
263+
console.log('has second sync, should update list');
264+
expect((await query.next()).value.rows._array).toStrictEqual([]);
265+
});
163266
});
164267

165-
function describeStreamingTests(createConnectedDatabase: () => Promise<ConnectedDatabaseUtils>) {
268+
function describeStreamingTests(
269+
createConnectedDatabase: (options?: Partial<WebPowerSyncOpenFactoryOptions>) => Promise<ConnectedDatabaseUtils>
270+
) {
166271
return () => {
167272
it('PowerSync reconnect on closed stream', async () => {
168273
const { powersync, waitForStream, remote } = await createConnectedDatabase();

0 commit comments

Comments
 (0)