-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite.test.ts
More file actions
46 lines (39 loc) · 1.73 KB
/
sqlite.test.ts
File metadata and controls
46 lines (39 loc) · 1.73 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
// Tests for sqlite.photon.ts
const testDbPath = `/tmp/photon-test-${Date.now()}.db`;
const testTable = `test_${Date.now()}`;
export async function beforeAll(photon: any) {
await photon.open({ path: testDbPath });
await photon.execute({
sql: `CREATE TABLE IF NOT EXISTS ${testTable} (id INTEGER PRIMARY KEY, name TEXT, value INTEGER)`,
});
}
export async function afterAll(photon: any) {
try {
if ((photon as any).db) {
(photon as any).db.close();
(photon as any).db = null;
}
const fs = await import('fs/promises');
await fs.unlink(testDbPath).catch(() => {});
} catch {}
}
export async function testOpen(photon: any) {
if (!(photon as any).db) return { skipped: true, reason: 'Database not open' };
}
export async function testTables(photon: any) {
if (!(photon as any).db) return { skipped: true, reason: 'Database not open' };
const result = await photon.tables();
if (!Array.isArray(result)) throw new Error('Tables should be array');
}
export async function testInsertQuery(photon: any) {
if (!(photon as any).db) return { skipped: true, reason: 'Database not open' };
await photon.execute({ sql: `INSERT INTO ${testTable} (name, value) VALUES (?, ?)`, params: ['test', 42] });
const result = await photon.query({ sql: `SELECT * FROM ${testTable} WHERE name = ?`, params: ['test'] });
if (!Array.isArray(result) || result.length === 0) throw new Error('Row not found');
if (result[0].value !== 42) throw new Error('Wrong value');
}
export async function testSchema(photon: any) {
if (!(photon as any).db) return { skipped: true, reason: 'Database not open' };
const result = await photon.schema({ table: testTable });
if (!Array.isArray(result)) throw new Error('Columns should be array');
}