-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathPowerSyncDatabase.test.ts
More file actions
247 lines (208 loc) · 7.86 KB
/
PowerSyncDatabase.test.ts
File metadata and controls
247 lines (208 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import * as path from 'node:path';
import { Worker } from 'node:worker_threads';
import { LockContext, Schema } from '@powersync/common';
import { randomUUID } from 'node:crypto';
import { expect, test, vi } from 'vitest';
import { CrudEntry, CrudTransaction, PowerSyncDatabase } from '../lib';
import { WorkerOpener } from '../lib/db/options';
import { AppSchema, databaseTest, tempDirectoryTest } from './utils';
test('validates options', async () => {
await expect(async () => {
const database = new PowerSyncDatabase({
schema: AppSchema,
database: {
dbFilename: '/dev/null',
readWorkerCount: 0
}
});
await database.init();
}).rejects.toThrowError('Needs at least one worker for reads');
});
tempDirectoryTest('can customize loading workers', async ({ tmpdir }) => {
const defaultWorker: WorkerOpener = (...args) => new Worker(...args);
const openFunction = vi.fn(defaultWorker); // Wrap in vi.fn to count invocations
const database = new PowerSyncDatabase({
schema: AppSchema,
database: {
dbFilename: 'test.db',
dbLocation: tmpdir,
openWorker: openFunction,
readWorkerCount: 2
}
});
await database.get('SELECT 1;'); // Make sure the database is ready and works
expect(openFunction).toHaveBeenCalledTimes(3); // One writer, two readers
await database.close();
});
tempDirectoryTest('can customize connection initialization', async ({ tmpdir }) => {
const initializeConnection = vi.fn(async (db: LockContext, isWriter: boolean) => {
const row = await db.get('pragma journal_mode');
if (isWriter) {
// This should run before anything else, so the database should not be in WAL mode here.
expect(row).toMatchObject({ journal_mode: 'delete' });
} else {
// Readers are initialized after writers, and initializing the writer will enable WAL mode.
expect(row).toMatchObject({ journal_mode: 'wal' });
}
});
const database = new PowerSyncDatabase({
schema: AppSchema,
database: {
dbFilename: 'test.db',
dbLocation: tmpdir,
initializeConnection,
readWorkerCount: 2
}
});
await database.get('SELECT 1;'); // Make sure the database is ready and works
expect(initializeConnection).toHaveBeenCalledTimes(3); // One writer, two readers
await database.close();
});
databaseTest('links powersync', async ({ database }) => {
await database.get('select powersync_rs_version();');
});
tempDirectoryTest('runs queries on multiple threads', async ({ tmpdir }) => {
const database = new PowerSyncDatabase({
schema: AppSchema,
database: {
dbFilename: 'test.db',
dbLocation: tmpdir
}
});
const threads = new Set<number>();
const collectWorkerThreadId = async () => {
const row = await database.get<{ r: number }>('SELECT node_thread_id() AS r');
threads.add(row.r);
return row.r;
};
const queryTasks: Promise<number>[] = [];
for (let i = 0; i < 10; i++) {
queryTasks.push(collectWorkerThreadId());
}
const res = await Promise.all(queryTasks);
await database.close();
expect(res).toHaveLength(10);
expect([...threads]).toHaveLength(5);
});
databaseTest('can watch tables', async ({ database }) => {
const fn = vi.fn();
const disposeWatch = database.onChangeWithCallback(
{
onChange: () => {
fn();
}
},
{ tables: ['todos'], throttleMs: 0 }
);
await database.execute('INSERT INTO todos (id, content) VALUES (uuid(), ?)', ['first']);
await expect.poll(() => fn).toHaveBeenCalledOnce();
await database.writeTransaction(async (tx) => {
await tx.execute('INSERT INTO todos (id, content) VALUES (uuid(), ?)', ['second']);
});
await expect.poll(() => fn).toHaveBeenCalledTimes(2);
await database.writeTransaction(async (tx) => {
await tx.execute('DELETE FROM todos;');
await tx.rollback();
});
await expect.poll(() => fn).toHaveBeenCalledTimes(2);
disposeWatch();
await database.execute('INSERT INTO todos (id, content) VALUES (uuid(), ?)', ['fourth']);
await expect.poll(() => fn).toHaveBeenCalledTimes(2);
});
tempDirectoryTest('throws error if target directory does not exist', async ({ tmpdir }) => {
const directory = path.join(tmpdir, 'some', 'nested', 'location', 'that', 'does', 'not', 'exist');
await expect(async () => {
const database = new PowerSyncDatabase({
schema: AppSchema,
database: {
dbFilename: 'test.db',
dbLocation: directory,
readWorkerCount: 2
}
});
await database.waitForReady();
}).rejects.toThrowError(/The dbLocation directory at ".+" does not exist/);
});
databaseTest.skip('can watch queries', async ({ database }) => {
const query = await database.watch('SELECT * FROM todos;', [])[Symbol.asyncIterator]();
expect((await query.next()).value.rows).toHaveLength(0);
await database.execute('INSERT INTO todos (id, content) VALUES (uuid(), ?)', ['first']);
// TODO: There is a race condition somewhere, this reports now rows sometimes.
expect((await query.next()).value.rows).toHaveLength(1);
await database.writeTransaction(async (tx) => {
await tx.execute('INSERT INTO todos (id, content) VALUES (uuid(), ?)', ['second']);
await tx.execute('INSERT INTO todos (id, content) VALUES (uuid(), ?)', ['third']);
});
expect((await query.next()).value.rows).toHaveLength(3);
await database.writeTransaction(async (tx) => {
await tx.execute('DELETE FROM todos;');
await tx.rollback();
});
await database.execute('INSERT INTO todos (id, content) VALUES (uuid(), ?)', ['fourth']);
expect((await query.next()).value.rows).toHaveLength(4);
});
databaseTest('getCrudTransactions', async ({ database }) => {
async function createTransaction(amount: number) {
await database.writeTransaction(async (tx) => {
for (let i = 0; i < amount; i++) {
await tx.execute('insert into todos (id) values (uuid())');
}
});
}
let iterator = database.getCrudTransactions()[Symbol.asyncIterator]();
expect(await iterator.next()).toMatchObject({ done: true });
await createTransaction(5);
await createTransaction(10);
await createTransaction(15);
let lastTransaction: CrudTransaction | null = null;
let batch: CrudEntry[] = [];
// Take the first two transactions via the async generator.
for await (const transaction of database.getCrudTransactions()) {
batch.push(...transaction.crud);
lastTransaction = transaction;
if (batch.length > 10) {
break;
}
}
expect(batch).toHaveLength(15);
await lastTransaction!.complete();
const remainingTransaction = await database.getNextCrudTransaction();
expect(remainingTransaction?.crud).toHaveLength(15);
});
// This is not a SemVer check, but is basic enough to skip this test on older versions of Node.js
tempDirectoryTest.skipIf(process.versions.node < '22.5.0')(
'should not present database is locked errors on startup',
async ({ tmpdir }) => {
for (let i = 0; i < 10; i++) {
const database = new PowerSyncDatabase({
schema: AppSchema,
database: {
dbFilename: `${randomUUID()}.sqlite`,
dbLocation: tmpdir,
implementation: {
type: 'node:sqlite'
}
}
});
// This should not throw
await database.waitForReady();
await database.close();
}
}
);
databaseTest('clear raw tables', async ({ database }) => {
await database.init();
const schema = new Schema({});
schema.withRawTables({
users: {
table_name: 'lists',
clear: 'DELETE FROM lists'
}
});
await database.updateSchema(schema);
await database.execute('CREATE TABLE lists (id TEXT NOT NULL PRIMARY KEY, name TEXT)');
await database.execute('INSERT INTO lists (id, name) VALUES (uuid(), ?)', ['list']);
expect(await database.getAll('SELECT * FROM lists')).toHaveLength(1);
await database.disconnectAndClear();
expect(await database.getAll('SELECT * FROM lists')).toHaveLength(0);
});