-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathPowerSyncDatabase.test.ts
More file actions
169 lines (137 loc) · 5.37 KB
/
Copy pathPowerSyncDatabase.test.ts
File metadata and controls
169 lines (137 loc) · 5.37 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
import * as path from 'node:path';
import { Worker } from 'node:worker_threads';
import { vi, expect, test } from 'vitest';
import { AppSchema, databaseTest, tempDirectoryTest } from './utils';
import { CrudEntry, CrudTransaction, PowerSyncDatabase } from '../lib';
import { WorkerOpener } from '../lib/db/options';
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();
});
databaseTest('links powersync', async ({ database }) => {
await database.get('select powersync_rs_version();');
});
databaseTest('runs queries on multiple threads', async ({ database }) => {
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);
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);
});