40 lines
1.4 KiB
TypeScript
40 lines
1.4 KiB
TypeScript
import Database from "better-sqlite3";
|
|
import path from "node:path";
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
function testDatabasePath() {
|
|
const databaseUrl = process.env.DATABASE_URL;
|
|
if (!databaseUrl?.startsWith("file:./")) {
|
|
throw new Error("Disposable test DATABASE_URL was not provided");
|
|
}
|
|
return path.resolve(process.cwd(), databaseUrl.slice("file:".length));
|
|
}
|
|
|
|
describe("committed migration chain", () => {
|
|
it("creates material groups and all expected group relationships", () => {
|
|
const database = new Database(testDatabasePath(), { readonly: true });
|
|
try {
|
|
const tables = database
|
|
.prepare("SELECT name FROM sqlite_master WHERE type = 'table'")
|
|
.all() as Array<{ name: string }>;
|
|
expect(tables.map((table) => table.name)).toContain("MaterialGroup");
|
|
|
|
for (const table of ["Deck", "QuizSet", "ShareLink"]) {
|
|
const columns = database.pragma(`table_info(${table})`) as Array<{
|
|
name: string;
|
|
}>;
|
|
expect(columns.map((column) => column.name)).toContain("groupId");
|
|
}
|
|
|
|
const groupIndexes = database.pragma("index_list(ShareLink)") as Array<{
|
|
name: string;
|
|
unique: number;
|
|
}>;
|
|
expect(groupIndexes).toContainEqual(
|
|
expect.objectContaining({ name: "ShareLink_groupId_key", unique: 1 })
|
|
);
|
|
} finally {
|
|
database.close();
|
|
}
|
|
});
|
|
});
|