Add real SQLite transaction tests
This commit is contained in:
@@ -1,62 +1,60 @@
|
||||
import path from "node:path";
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import { db } from "../src/db/client.js";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||
import {
|
||||
createDatabaseContext,
|
||||
type DatabaseContext,
|
||||
} from "../src/db/database-context.js";
|
||||
import { DistributionBoardRepository } from "../src/db/repositories/distribution-board.repository.js";
|
||||
import { circuitLists } from "../src/db/schema/circuit-lists.js";
|
||||
import { circuitSections } from "../src/db/schema/circuit-sections.js";
|
||||
import { distributionBoards } from "../src/db/schema/distribution-boards.js";
|
||||
import { projects } from "../src/db/schema/projects.js";
|
||||
|
||||
describe("distribution board repository", () => {
|
||||
it("creates board, circuit list and default sections in one synchronous transaction", () => {
|
||||
const originalTransaction = db.transaction;
|
||||
const insertedValues: Array<{ table: unknown; values: unknown }> = [];
|
||||
let transactionCalls = 0;
|
||||
|
||||
(db as unknown as { transaction: (callback: (tx: unknown) => unknown) => unknown }).transaction =
|
||||
(callback) => {
|
||||
transactionCalls += 1;
|
||||
return callback({
|
||||
insert(table: unknown) {
|
||||
return {
|
||||
values(values: unknown) {
|
||||
insertedValues.push({ table, values });
|
||||
return {
|
||||
run() {
|
||||
return { changes: Array.isArray(values) ? values.length : 1 };
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
};
|
||||
function createTestDatabase(): DatabaseContext {
|
||||
const context = createDatabaseContext(":memory:");
|
||||
migrate(context.db, {
|
||||
migrationsFolder: path.resolve("src", "db", "migrations"),
|
||||
});
|
||||
context.db.insert(projects).values({ id: "project-1", name: "Test project" }).run();
|
||||
return context;
|
||||
}
|
||||
|
||||
describe("distribution board repository integration", () => {
|
||||
it("commits board, circuit list and default sections together", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const repository = new DistributionBoardRepository();
|
||||
const board = repository.createWithCircuitListAndDefaultSections("p1", "UV-01");
|
||||
const repository = new DistributionBoardRepository(context.db);
|
||||
const board = repository.createWithCircuitListAndDefaultSections(
|
||||
"project-1",
|
||||
"UV-01"
|
||||
);
|
||||
|
||||
assert.equal(transactionCalls, 1);
|
||||
assert.equal(insertedValues.length, 3);
|
||||
assert.equal(insertedValues[0].table, distributionBoards);
|
||||
assert.deepEqual(insertedValues[0].values, board);
|
||||
assert.equal(insertedValues[1].table, circuitLists);
|
||||
assert.deepEqual(insertedValues[1].values, {
|
||||
id: board.id,
|
||||
projectId: "p1",
|
||||
distributionBoardId: board.id,
|
||||
name: "UV-01 Stromkreisliste",
|
||||
});
|
||||
assert.equal(insertedValues[2].table, circuitSections);
|
||||
const persistedBoards = context.db
|
||||
.select()
|
||||
.from(distributionBoards)
|
||||
.where(eq(distributionBoards.id, board.id))
|
||||
.all();
|
||||
const persistedLists = context.db
|
||||
.select()
|
||||
.from(circuitLists)
|
||||
.where(eq(circuitLists.distributionBoardId, board.id))
|
||||
.all();
|
||||
const persistedSections = context.db
|
||||
.select()
|
||||
.from(circuitSections)
|
||||
.where(eq(circuitSections.circuitListId, board.id))
|
||||
.all();
|
||||
|
||||
const sections = insertedValues[2].values as Array<{
|
||||
circuitListId: string;
|
||||
key: string;
|
||||
prefix: string;
|
||||
}>;
|
||||
assert.equal(sections.length, 4);
|
||||
assert.ok(sections.every((section) => section.circuitListId === board.id));
|
||||
assert.deepEqual(persistedBoards, [board]);
|
||||
assert.equal(persistedLists.length, 1);
|
||||
assert.equal(persistedLists[0].name, "UV-01 Stromkreisliste");
|
||||
assert.deepEqual(
|
||||
sections.map((section) => [section.key, section.prefix]),
|
||||
persistedSections
|
||||
.sort((left, right) => left.sortOrder - right.sortOrder)
|
||||
.map((section) => [section.key, section.prefix]),
|
||||
[
|
||||
["lighting", "-1F"],
|
||||
["single_phase", "-2F"],
|
||||
@@ -65,7 +63,36 @@ describe("distribution board repository", () => {
|
||||
]
|
||||
);
|
||||
} finally {
|
||||
(db as unknown as { transaction: unknown }).transaction = originalTransaction;
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rolls back board and circuit list when default section creation fails", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
context.sqlite.exec(`
|
||||
CREATE TRIGGER fail_default_section_insert
|
||||
BEFORE INSERT ON circuit_sections
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'forced section failure');
|
||||
END;
|
||||
`);
|
||||
const repository = new DistributionBoardRepository(context.db);
|
||||
|
||||
assert.throws(
|
||||
() =>
|
||||
repository.createWithCircuitListAndDefaultSections(
|
||||
"project-1",
|
||||
"UV rollback"
|
||||
),
|
||||
/forced section failure/
|
||||
);
|
||||
|
||||
assert.equal(context.db.select().from(distributionBoards).all().length, 0);
|
||||
assert.equal(context.db.select().from(circuitLists).all().length, 0);
|
||||
assert.equal(context.db.select().from(circuitSections).all().length, 0);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user