summaryrefslogtreecommitdiff
path: root/tests/js/db.mjs
blob: 372f64a1505916c24a2275dc7c8b3e3a523a1d0d (plain) (blame)
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
import assert from "node:assert/strict";

import sqlite from "../../src/sqlite.cjs";

import { runTests } from "../runner.mjs";
import { promisify, promisifyDb, open, db, init } from "../../src/db.mjs";


const test_promisify = t => {
	t.start("promisify()");

	t.test("we wrap the callbacky function", async () => {
		const okFn1  = (a, b, cb) =>
			setTimeout(() => cb(null, { a, b, ok: true }));
		const errFn1 = (a, b, c, cb) =>
			setTimeout(() => cb({ err: true, a }, "ignored"));

		const okFn2  = promisify(okFn1);
		const errFn2 = promisify(errFn1);

		assert.deepEqual(
			await okFn2("a-value", "b-value"),
			{
				a: "a-value",
				b: "b-value",
				ok: true,
			},
		);

		assert.rejects(
			async () => await errFn2("aa", "bb", "cc"),
			{
				err: true,
				a: "aa",
			},
		);
	});
};
const test_promisifyDb = t => {
	t.start("promisifyDb()");

	const createTable = "CREATE TABLE table_ (column INTEGER NOT NULL PRIMARY KEY);";

	t.test("we can access the underlying database and statement ref", async () => {
		const db = await open(":memory:");
		assert.ok(db.ref instanceof sqlite.Database);

		const stmt = await db.prepare("SELECT 1;");
		assert.ok(stmt.ref instanceof sqlite.Statement);
	});

	t.test("we can run the wrapped fns", async () => {
		const db = await open(":memory:");

		await db.exec(createTable);

		const stmt = await db.prepare("INSERT INTO table_ (column) VALUES ($column)");
		await stmt.run({ $column: 3 });
		await stmt.run({ $column: 1 });
		await stmt.finalize();
		assert.rejects(
			async () => await stmt.run({ $column: 2 }),
			{
				message: "SQLITE_MISUSE: Statement is already finalized",
				code: "SQLITE_MISUSE",
			},
		);

		const selectAll = "SELECT column FROM table_ ORDER BY column;";

		const all = await db.all(selectAll);
		assert.deepEqual(all, [{ column: 1 }, { column: 3 }]);

		const contents = [];
		const cb = row => contents.push(row);
		await db.each(selectAll, cb);
		assert.deepEqual(contents, [{ column: 1 }, { column: 3 }]);
	});
};

const test_open = t => {
	t.start("open()");

	t.test("we must provide a name", () => {
		assert.rejects(
			async () => await open(),
			assert.AssertionError,
		);

		assert.rejects(
			async () => await open(undefined),
			assert.AssertionError,
		);

		assert.rejects(
			async () => await open(null),
			assert.AssertionError,
		);

		assert.rejects(
			async () => await open(""),
			assert.AssertionError,
		);
	});

	t.test("failure to open causes a promise rejection", () => {
		assert.rejects(
			async () => await open("tests/non/existing/directory/and/file"),
			{
				message: "SQLITE_CANTOPEN: unable to open database file",
				code: "SQLITE_CANTOPEN",
			},
		);
	});
};

const test_init = t => {
	t.start("init()");
	t.test("we only know how to deal with 1 database", async () => {
		await init();
		const ref1 = db;
		await init();
		const ref2 = db;

		assert.notDeepEqual(ref1, ref2);
	});
};


await runTests([
	test_promisify,
	test_promisifyDb,
	test_open,
	test_init,
]);