forked from tursodatabase/libsql-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.test.js
341 lines (292 loc) · 8.88 KB
/
sync.test.js
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import test from "ava";
test.beforeEach(async (t) => {
const [db, errorType, provider] = await connect();
db.exec(`
DROP TABLE IF EXISTS users;
CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT)
`);
db.exec(
"INSERT INTO users (id, name, email) VALUES (1, 'Alice', 'alice@example.org')"
);
db.exec(
"INSERT INTO users (id, name, email) VALUES (2, 'Bob', 'bob@example.com')"
);
t.context = {
db,
errorType,
provider
};
});
test.after.always(async (t) => {
if (t.context.db != undefined) {
t.context.db.close();
}
});
test.serial("Open in-memory database", async (t) => {
const [db] = await connect(":memory:");
t.is(db.memory, true);
});
test.serial("Statement.prepare() error", async (t) => {
const db = t.context.db;
t.throws(() => {
return db.prepare("SYNTAX ERROR");
}, {
instanceOf: t.context.errorType,
message: 'near "SYNTAX": syntax error'
});
});
test.serial("Statement.run() [positional]", async (t) => {
const db = t.context.db;
const stmt = db.prepare("INSERT INTO users(name, email) VALUES (?, ?)");
const info = stmt.run(["Carol", "carol@example.net"]);
t.is(info.changes, 1);
t.is(info.lastInsertRowid, 3);
});
test.serial("Statement.get() [positional]", async (t) => {
const db = t.context.db;
var stmt = 0;
stmt = db.prepare("SELECT * FROM users WHERE id = ?");
t.is(stmt.get(0), undefined);
t.is(stmt.get([0]), undefined);
t.is(stmt.get(1).name, "Alice");
t.is(stmt.get(2).name, "Bob");
stmt = db.prepare("SELECT * FROM users WHERE id = ?1");
t.is(stmt.get({1: 0}), undefined);
t.is(stmt.get({1: 1}).name, "Alice");
t.is(stmt.get({1: 2}).name, "Bob");
});
test.serial("Statement.get() [named]", async (t) => {
const db = t.context.db;
var stmt = undefined;
stmt = db.prepare("SELECT :b, :a");
t.deepEqual(stmt.raw().get({ a: 'a', b: 'b' }), ['b', 'a']);
stmt = db.prepare("SELECT * FROM users WHERE id = :id");
t.is(stmt.get({ id: 0 }), undefined);
t.is(stmt.get({ id: 1 }).name, "Alice");
t.is(stmt.get({ id: 2 }).name, "Bob");
stmt = db.prepare("SELECT * FROM users WHERE id = @id");
t.is(stmt.get({ id: 0 }), undefined);
t.is(stmt.get({ id: 1 }).name, "Alice");
t.is(stmt.get({ id: 2 }).name, "Bob");
stmt = db.prepare("SELECT * FROM users WHERE id = $id");
t.is(stmt.get({ id: 0 }), undefined);
t.is(stmt.get({ id: 1 }).name, "Alice");
t.is(stmt.get({ id: 2 }).name, "Bob");
});
test.serial("Statement.get() [raw]", async (t) => {
const db = t.context.db;
const stmt = db.prepare("SELECT * FROM users WHERE id = ?");
t.deepEqual(stmt.raw().get(1), [1, "Alice", "alice@example.org"]);
});
test.serial("Statement.iterate() [empty]", async (t) => {
const db = t.context.db;
const stmt = db.prepare("SELECT * FROM users WHERE id = 0");
t.is(stmt.iterate().next().done, true);
t.is(stmt.iterate([]).next().done, true);
t.is(stmt.iterate({}).next().done, true);
});
test.serial("Statement.iterate()", async (t) => {
const db = t.context.db;
const stmt = db.prepare("SELECT * FROM users");
const expected = [1, 2];
var idx = 0;
for (const row of stmt.iterate()) {
t.is(row.id, expected[idx++]);
}
});
test.serial("Statement.all()", async (t) => {
const db = t.context.db;
const stmt = db.prepare("SELECT * FROM users");
const expected = [
{ id: 1, name: "Alice", email: "alice@example.org" },
{ id: 2, name: "Bob", email: "bob@example.com" },
];
t.deepEqual(stmt.all(), expected);
});
test.serial("Statement.all() [raw]", async (t) => {
const db = t.context.db;
const stmt = db.prepare("SELECT * FROM users");
const expected = [
[1, "Alice", "alice@example.org"],
[2, "Bob", "bob@example.com"],
];
t.deepEqual(stmt.raw().all(), expected);
});
test.serial("Statement.all() [default safe integers]", async (t) => {
const db = t.context.db;
db.defaultSafeIntegers();
const stmt = db.prepare("SELECT * FROM users");
const expected = [
[1n, "Alice", "alice@example.org"],
[2n, "Bob", "bob@example.com"],
];
t.deepEqual(stmt.raw().all(), expected);
});
test.serial("Statement.all() [statement safe integers]", async (t) => {
const db = t.context.db;
const stmt = db.prepare("SELECT * FROM users");
stmt.safeIntegers();
const expected = [
[1n, "Alice", "alice@example.org"],
[2n, "Bob", "bob@example.com"],
];
t.deepEqual(stmt.raw().all(), expected);
});
test.serial("Statement.raw() [failure]", async (t) => {
const db = t.context.db;
const stmt = db.prepare("INSERT INTO users (id, name, email) VALUES (?, ?, ?)");
await t.throws(() => {
stmt.raw()
}, {
message: 'The raw() method is only for statements that return data'
});
});
test.serial("Statement.columns()", async (t) => {
const db = t.context.db;
var stmt = undefined;
stmt = db.prepare("SELECT 1");
t.deepEqual(stmt.columns(), [
{
column: null,
database: null,
name: '1',
table: null,
type: null,
},
]);
stmt = db.prepare("SELECT * FROM users WHERE id = ?");
t.deepEqual(stmt.columns(), [
{
column: "id",
database: "main",
name: "id",
table: "users",
type: "INTEGER",
},
{
column: "name",
database: "main",
name: "name",
table: "users",
type: "TEXT",
},
{
column: "email",
database: "main",
name: "email",
table: "users",
type: "TEXT",
},
]);
});
test.serial("Database.transaction()", async (t) => {
const db = t.context.db;
const insert = db.prepare(
"INSERT INTO users(name, email) VALUES (:name, :email)"
);
const insertMany = db.transaction((users) => {
t.is(db.inTransaction, true);
for (const user of users) insert.run(user);
});
t.is(db.inTransaction, false);
insertMany([
{ name: "Joey", email: "joey@example.org" },
{ name: "Sally", email: "sally@example.org" },
{ name: "Junior", email: "junior@example.org" },
]);
t.is(db.inTransaction, false);
const stmt = db.prepare("SELECT * FROM users WHERE id = ?");
t.is(stmt.get(3).name, "Joey");
t.is(stmt.get(4).name, "Sally");
t.is(stmt.get(5).name, "Junior");
});
test.serial("Database.transaction().immediate()", async (t) => {
const db = t.context.db;
const insert = db.prepare(
"INSERT INTO users(name, email) VALUES (:name, :email)"
);
const insertMany = db.transaction((users) => {
t.is(db.inTransaction, true);
for (const user of users) insert.run(user);
});
t.is(db.inTransaction, false);
insertMany.immediate([
{ name: "Joey", email: "joey@example.org" },
{ name: "Sally", email: "sally@example.org" },
{ name: "Junior", email: "junior@example.org" },
]);
t.is(db.inTransaction, false);
});
test.serial("values", async (t) => {
const db = t.context.db;
const stmt = db.prepare("SELECT ?").raw();
t.deepEqual(stmt.get(1), [1]);
t.deepEqual(stmt.get(Number.MIN_VALUE), [Number.MIN_VALUE]);
t.deepEqual(stmt.get(Number.MAX_VALUE), [Number.MAX_VALUE]);
t.deepEqual(stmt.get(Number.MAX_SAFE_INTEGER), [Number.MAX_SAFE_INTEGER]);
t.deepEqual(stmt.get(9007199254740991n), [9007199254740991]);
});
test.serial("Database.pragma()", async (t) => {
const db = t.context.db;
db.pragma("cache_size = 2000");
t.deepEqual(db.pragma("cache_size"), [{ "cache_size": 2000 }]);
});
test.serial("errors", async (t) => {
const db = t.context.db;
const syntaxError = await t.throws(() => {
db.exec("SYNTAX ERROR");
}, {
instanceOf: t.context.errorType,
message: 'near "SYNTAX": syntax error',
code: 'SQLITE_ERROR'
});
const noTableError = await t.throws(() => {
db.exec("SELECT * FROM missing_table");
}, {
instanceOf: t.context.errorType,
message: "no such table: missing_table",
code: 'SQLITE_ERROR'
});
if (t.context.provider === 'libsql') {
t.is(noTableError.rawCode, 1)
t.is(syntaxError.rawCode, 1)
}
});
test.serial("Database.prepare() after close()", async (t) => {
const db = t.context.db;
db.close();
t.throws(() => {
db.prepare("SELECT 1");
}, {
instanceOf: TypeError,
message: "The database connection is not open"
});
});
test.serial("Database.exec() after close()", async (t) => {
const db = t.context.db;
db.close();
t.throws(() => {
db.exec("SELECT 1");
}, {
instanceOf: TypeError,
message: "The database connection is not open"
});
});
const connect = async (path_opt) => {
const path = path_opt ?? "hello.db";
const provider = process.env.PROVIDER;
if (provider === "libsql") {
const database = process.env.LIBSQL_DATABASE ?? path;
const x = await import("libsql");
const options = {};
const db = new x.default(database, options);
return [db, x.SqliteError, provider];
}
if (provider == "sqlite") {
const x = await import("better-sqlite3");
const options = {};
const db = x.default(path, options);
return [db, x.SqliteError, provider];
}
throw new Error("Unknown provider: " + provider);
};