@gjsify/sqlite 0.4.0 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,267 +0,0 @@
1
- // Ported from refs/node-test/parallel/test-sqlite-database-sync.js
2
- // Original: MIT license, Node.js contributors
3
-
4
- import { describe, it, expect } from '@gjsify/unit';
5
- import { join } from 'node:path';
6
- import { tmpdir } from 'node:os';
7
- import { existsSync, mkdirSync, rmSync } from 'node:fs';
8
- import { DatabaseSync } from 'node:sqlite';
9
-
10
- let cnt = 0;
11
- const testDir = join(tmpdir(), 'gjsify-sqlite-test-' + Date.now());
12
-
13
- function setup() {
14
- try { mkdirSync(testDir, { recursive: true }); } catch {}
15
- }
16
-
17
- function cleanup() {
18
- try { rmSync(testDir, { recursive: true, force: true }); } catch {}
19
- }
20
-
21
- function nextDb(): string {
22
- return join(testDir, `database-${cnt++}.db`);
23
- }
24
-
25
- export default async () => {
26
- setup();
27
-
28
- await describe('DatabaseSync() constructor', async () => {
29
- await it('throws if database path is not a string, Uint8Array, or URL', async () => {
30
- expect(() => {
31
- new (DatabaseSync as any)();
32
- }).toThrow();
33
- });
34
-
35
- await it('throws if the database location as string contains null bytes', async () => {
36
- expect(() => {
37
- new DatabaseSync('l\0cation');
38
- }).toThrow();
39
- });
40
-
41
- await it('throws if options is provided but is not an object', async () => {
42
- expect(() => {
43
- new (DatabaseSync as any)('foo', null);
44
- }).toThrow();
45
- });
46
-
47
- await it('throws if options.open is provided but is not a boolean', async () => {
48
- expect(() => {
49
- new (DatabaseSync as any)('foo', { open: 5 });
50
- }).toThrow();
51
- });
52
-
53
- await it('throws if options.readOnly is provided but is not a boolean', async () => {
54
- expect(() => {
55
- new (DatabaseSync as any)('foo', { readOnly: 5 });
56
- }).toThrow();
57
- });
58
-
59
- await it('throws if options.timeout is provided but is not an integer', async () => {
60
- expect(() => {
61
- new (DatabaseSync as any)('foo', { timeout: .99 });
62
- }).toThrow();
63
- });
64
-
65
- await it('throws if options.enableForeignKeyConstraints is not a boolean', async () => {
66
- expect(() => {
67
- new (DatabaseSync as any)('foo', { enableForeignKeyConstraints: 5 });
68
- }).toThrow();
69
- });
70
-
71
- await it('throws if options.enableDoubleQuotedStringLiterals is not a boolean', async () => {
72
- expect(() => {
73
- new (DatabaseSync as any)('foo', { enableDoubleQuotedStringLiterals: 5 });
74
- }).toThrow();
75
- });
76
-
77
- await it('throws if options.readBigInts is not a boolean', async () => {
78
- expect(() => {
79
- new (DatabaseSync as any)('foo', { readBigInts: 42 });
80
- }).toThrow();
81
- });
82
-
83
- await it('throws if options.returnArrays is not a boolean', async () => {
84
- expect(() => {
85
- new (DatabaseSync as any)('foo', { returnArrays: 42 });
86
- }).toThrow();
87
- });
88
-
89
- await it('throws if the URL does not have the file: scheme', async () => {
90
- expect(() => {
91
- new DatabaseSync(new URL('http://example.com') as any);
92
- }).toThrow();
93
- });
94
- });
95
-
96
- await describe('DatabaseSync.prototype.open()', async () => {
97
- await it('opens a database connection', async () => {
98
- const dbPath = nextDb();
99
- const db = new DatabaseSync(dbPath, { open: false });
100
- expect(db.isOpen).toBe(false);
101
- db.open();
102
- expect(db.isOpen).toBe(true);
103
- db.close();
104
- });
105
-
106
- await it('throws if database is already open', async () => {
107
- const dbPath = nextDb();
108
- const db = new DatabaseSync(dbPath);
109
- expect(db.isOpen).toBe(true);
110
- expect(() => { db.open(); }).toThrow();
111
- db.close();
112
- });
113
- });
114
-
115
- await describe('DatabaseSync.prototype.close()', async () => {
116
- await it('closes an open database connection', async () => {
117
- const db = new DatabaseSync(nextDb());
118
- expect(db.isOpen).toBe(true);
119
- db.close();
120
- expect(db.isOpen).toBe(false);
121
- });
122
-
123
- await it('throws if database is not open', async () => {
124
- const db = new DatabaseSync(nextDb(), { open: false });
125
- expect(db.isOpen).toBe(false);
126
- expect(() => { db.close(); }).toThrow();
127
- });
128
- });
129
-
130
- await describe('DatabaseSync.prototype.exec()', async () => {
131
- await it('runs SQL and returns undefined', async () => {
132
- const db = new DatabaseSync(nextDb());
133
- const result = db.exec(`
134
- CREATE TABLE data(key INTEGER PRIMARY KEY, val INTEGER) STRICT;
135
- INSERT INTO data (key, val) VALUES (1, 2);
136
- INSERT INTO data (key, val) VALUES (8, 9);
137
- `);
138
- expect(result).toBe(undefined);
139
- const stmt = db.prepare('SELECT * FROM data ORDER BY key');
140
- const rows = stmt.all();
141
- expect(rows.length).toBe(2);
142
- db.close();
143
- });
144
-
145
- await it('reports errors from SQLite', async () => {
146
- const db = new DatabaseSync(nextDb());
147
- expect(() => {
148
- db.exec('CREATE TABLEEEE');
149
- }).toThrow();
150
- db.close();
151
- });
152
-
153
- await it('throws if database is not open', async () => {
154
- const db = new DatabaseSync(nextDb(), { open: false });
155
- expect(() => { db.exec('SELECT 1'); }).toThrow();
156
- });
157
-
158
- await it('throws if sql is not a string', async () => {
159
- const db = new DatabaseSync(nextDb());
160
- expect(() => { (db as any).exec(); }).toThrow();
161
- db.close();
162
- });
163
- });
164
-
165
- await describe('DatabaseSync.prototype.prepare()', async () => {
166
- await it('returns a prepared statement', async () => {
167
- const db = new DatabaseSync(nextDb());
168
- const stmt = db.prepare('CREATE TABLE webstorage(key TEXT)');
169
- expect(stmt).toBeDefined();
170
- db.close();
171
- });
172
-
173
- await it('throws if database is not open', async () => {
174
- const db = new DatabaseSync(nextDb(), { open: false });
175
- expect(() => { (db as any).prepare(); }).toThrow();
176
- });
177
-
178
- await it('throws if sql is not a string', async () => {
179
- const db = new DatabaseSync(nextDb());
180
- expect(() => { (db as any).prepare(); }).toThrow();
181
- db.close();
182
- });
183
- });
184
-
185
- await describe('DatabaseSync :memory: database', async () => {
186
- await it('works with :memory: path', async () => {
187
- const db = new DatabaseSync(':memory:');
188
- expect(db.isOpen).toBe(true);
189
- db.exec('CREATE TABLE t(id INTEGER PRIMARY KEY, val TEXT)');
190
- db.exec("INSERT INTO t (id, val) VALUES (1, 'hello')");
191
- const row = db.prepare('SELECT * FROM t').get();
192
- expect(row).toBeDefined();
193
- db.close();
194
- });
195
- });
196
-
197
- await describe('DatabaseSync.prototype.isTransaction', async () => {
198
- await it('correctly detects a committed transaction', async () => {
199
- const db = new DatabaseSync(':memory:');
200
- expect(db.isTransaction).toBe(false);
201
- db.exec('BEGIN');
202
- expect(db.isTransaction).toBe(true);
203
- db.exec('CREATE TABLE foo (id INTEGER PRIMARY KEY)');
204
- expect(db.isTransaction).toBe(true);
205
- db.exec('COMMIT');
206
- expect(db.isTransaction).toBe(false);
207
- db.close();
208
- });
209
-
210
- await it('correctly detects a rolled back transaction', async () => {
211
- const db = new DatabaseSync(':memory:');
212
- expect(db.isTransaction).toBe(false);
213
- db.exec('BEGIN');
214
- expect(db.isTransaction).toBe(true);
215
- db.exec('ROLLBACK');
216
- expect(db.isTransaction).toBe(false);
217
- db.close();
218
- });
219
- });
220
-
221
- await describe('DatabaseSync.prototype.location()', async () => {
222
- await it('throws if database is not open', async () => {
223
- const db = new DatabaseSync(nextDb(), { open: false });
224
- expect(() => { db.location(); }).toThrow();
225
- });
226
-
227
- await it('returns null for in-memory database', async () => {
228
- const db = new DatabaseSync(':memory:');
229
- expect(db.location()).toBeNull();
230
- db.close();
231
- });
232
-
233
- await it('returns db path for persistent database', async () => {
234
- const dbPath = nextDb();
235
- const db = new DatabaseSync(dbPath);
236
- expect(db.location()).toBe(dbPath);
237
- db.close();
238
- });
239
- });
240
-
241
- await describe('DatabaseSync foreign key constraints', async () => {
242
- await it('enables foreign key constraints by default', async () => {
243
- const db = new DatabaseSync(nextDb());
244
- db.exec(`
245
- CREATE TABLE foo (id INTEGER PRIMARY KEY);
246
- CREATE TABLE bar (foo_id INTEGER REFERENCES foo(id));
247
- `);
248
- expect(() => {
249
- db.exec('INSERT INTO bar (foo_id) VALUES (1)');
250
- }).toThrow();
251
- db.close();
252
- });
253
-
254
- await it('allows disabling foreign key constraints', async () => {
255
- const db = new DatabaseSync(nextDb(), { enableForeignKeyConstraints: false });
256
- db.exec(`
257
- CREATE TABLE foo (id INTEGER PRIMARY KEY);
258
- CREATE TABLE bar (foo_id INTEGER REFERENCES foo(id));
259
- `);
260
- // Should not throw
261
- db.exec('INSERT INTO bar (foo_id) VALUES (1)');
262
- db.close();
263
- });
264
- });
265
-
266
- cleanup();
267
- };