summaryrefslogtreecommitdiff
path: root/tests/js/accretion.mjs
blob: c6ec4b2792a98d4089fa29ec96b44ffd021e0cc8 (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
import assert from "node:assert/strict";
import fs     from "node:fs";

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

import * as runner from "../runner.mjs";
import * as db     from "../../src/db.mjs";
import * as u      from "../../src/utils.mjs";
import {
	MIGRATIONS_DIR,
	runMigrations,
} from "../../src/accretion.mjs";


const test_runMigrations = async t => {
	t.start("runMigrations()");

	await t.test("running twice is a noop", async () => {
		const contents = [];
		const logFn = x => contents.push(x);
		const handle = await db.open(":memory:");
		const migrationsFn = () => handle.all("SELECT filename FROM migrations;");

		assert.rejects(
			migrationsFn,
			{ message: "SQLITE_ERROR: no such table: migrations" },
		);

		await runMigrations(logFn, handle);
		const filled = await migrationsFn();

		await runMigrations(logFn, handle);
		const unchanged = await migrationsFn();

		assert.deepEqual(filled, unchanged);

		const migrationLogs = fs
			.readdirSync(MIGRATIONS_DIR)
			.sort(u.strSortFn)
			.map(filename => ({ log: "exec-migration", filename }));
		assert.deepEqual(contents, migrationLogs);
	});
};


await runner.runTests([
	test_runMigrations,
]);