-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver-tests.ts
More file actions
225 lines (193 loc) · 7.56 KB
/
driver-tests.ts
File metadata and controls
225 lines (193 loc) · 7.56 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
import { beforeEach, describe, test, expect } from './test.js';
import { SqliteDriverConnectionPool } from '@sqlite-js/driver';
export interface DriverFeatures {
getColumns: boolean;
rawResults: boolean;
allowsMissingParameters: boolean;
}
export function describeDriverTests(
name: string,
features: DriverFeatures,
factory: (path: string) => Promise<SqliteDriverConnectionPool>
) {
describe(`${name} - driver tests`, () => {
let dbPath: string;
const open = async () => {
const db = await factory(dbPath);
return db;
};
beforeEach((context) => {
let testNameSanitized = context.fullName.replaceAll(
/[\s\/\\>\.\-\:]+/g,
'_'
);
if (testNameSanitized.length > 10) {
testNameSanitized =
testNameSanitized.substring(testNameSanitized.length - 7) +
String(Math.random()).substring(1, 4);
}
dbPath = `test-db/${testNameSanitized}.db`;
});
test.skipIf(!features.rawResults)('basic select - array', async () => {
await using driver = await open();
await using connection = await driver.reserveConnection();
using s = connection.prepare('select 1 as one');
const rows = await s.allArray();
expect(rows).toEqual([[1]]);
if (features.getColumns) {
const columns = await s.getColumns();
expect(columns).toEqual(['one']);
}
});
test('basic select - object', async () => {
await using driver = await open();
await using connection = await driver.reserveConnection();
using s = connection.prepare('select 1 as one');
const rows = await s.all();
expect(rows).toEqual([{ one: 1 }]);
});
test('big number', async () => {
await using driver = await open();
await using connection = await driver.reserveConnection();
using s = connection.prepare('select 9223372036854775807 as bignumber');
const rows = await s.all();
expect(rows).toEqual([{ bignumber: 9223372036854776000 }]);
using s2 = connection.prepare('select ? as bignumber');
const rows2 = await s2.all([9223372036854775807n]);
expect(rows2).toEqual([{ bignumber: 9223372036854776000 }]);
});
test('bigint', async () => {
await using driver = await open();
await using connection = await driver.reserveConnection();
using s = connection.prepare('select ? as bignumber');
const rows1 = await s.all([9223372036854775807n], { bigint: true });
expect(rows1).toEqual([{ bignumber: 9223372036854775807n }]);
using s2 = connection.prepare(
'select 9223372036854775807 as bignumber',
{}
);
const rows2 = await s2.all(undefined, { bigint: true });
expect(rows2).toEqual([{ bignumber: 9223372036854775807n }]);
});
test('insert returning', async () => {
await using driver = await open();
await using connection = await driver.reserveConnection();
using s1 = connection.prepare(
'create table test_data(id integer primary key, data text)'
);
await s1.run();
using s2 = connection.prepare(
'insert into test_data(data) values(123) returning id'
);
const rows = await s2.all();
expect(rows).toEqual([{ id: 1 }]);
if (features.getColumns) {
const columns = await s2.getColumns();
expect(columns).toEqual(['id']);
}
});
test('bind named args', async () => {
await using driver = await open();
await using connection = await driver.reserveConnection();
using s = connection.prepare('select :one as one, :two as two');
const rows = await s.all({ one: 1, two: 2 });
expect(rows).toEqual([{ one: 1, two: 2 }]);
});
test('bind named args - explicit names', async () => {
await using driver = await open();
await using connection = await driver.reserveConnection();
using s = connection.prepare('select $one as one, $two as two');
const rows = await s.all({ $one: 1, $two: 2 });
expect(rows).toEqual([{ one: 1, two: 2 }]);
});
test.skipIf(!features.allowsMissingParameters)(
'skip named arg',
async () => {
await using driver = await open();
await using connection = await driver.reserveConnection();
using s = connection.prepare('select :one as one, :two as two');
const rows = await s.all({ two: 2 });
expect(rows).toEqual([{ one: null, two: 2 }]);
}
);
test('positional parameters', async () => {
await using driver = await open();
await using connection = await driver.reserveConnection();
using s = connection.prepare('select ? as one, ? as two');
const rows = await s.all([1, 2]);
expect(rows).toEqual([{ one: 1, two: 2 }]);
});
test('positional specific parameters', async () => {
await using driver = await open();
await using connection = await driver.reserveConnection();
using s = connection.prepare('select ?2 as two, ?1 as one');
const rows = await s.all({ '1': 1, '2': 2 });
expect(rows).toEqual([{ two: 2, one: 1 }]);
});
test.skip('named and positional parameters', async () => {
// TODO: Specify the behavior for this
await using driver = await open();
await using connection = await driver.reserveConnection();
using s = connection.prepare(
'select ? as one, @three as three, ? as two'
);
const rows = await s.all({ 1: 1, 2: 2, three: 3 });
expect(rows).toEqual([{ one: 1, three: 3, two: 2 }]);
});
test.skipIf(!features.allowsMissingParameters)(
'reset parameters',
async () => {
await using driver = await open();
await using connection = await driver.reserveConnection();
using s = connection.prepare('select ? as one, ? as two');
const rows1 = await s.all([1, 2]);
expect(rows1).toEqual([{ one: 1, two: 2 }]);
const rows2 = await s.all();
expect(rows2).toEqual([{ one: null, two: null }]);
}
);
test('error handling - prepare', async () => {
await using driver = await open();
await using connection = await driver.reserveConnection();
using s = connection.prepare('select foobar');
expect(await s.getColumns().catch((e) => e)).toMatchObject({
code: 'SQLITE_ERROR',
message: 'no such column: foobar'
});
expect(await s.all().catch((e) => e)).toMatchObject({
code: 'SQLITE_ERROR',
message: 'no such column: foobar'
});
});
test('error handling - query', async () => {
await using driver = await open();
await using connection = await driver.reserveConnection();
using s = connection.prepare(
"select json_each.value from json_each('test')"
);
if (features.getColumns) {
expect(await s.getColumns()).toEqual(['value']);
}
expect(await s.all().catch((e) => e)).toMatchObject({
code: 'SQLITE_ERROR',
message: 'malformed JSON'
});
});
test.skip('onUpdate', async () => {
// Skipped: Not properly implemented yet.
await using driver = await open();
await using connection = await driver.reserveConnection();
// await connection.run(
// "create table test_data(id integer primary key, data text)"
// );
// // TODO: test the results
// connection.onUpdate(({ events }) => {
// console.log("update", events);
// });
// await connection.run(
// "insert into test_data(data) values(123) returning id"
// );
// await connection.run("update test_data set data = data || 'test'");
});
});
}