Isolate device row transactions
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
import path from "node:path";
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||
import {
|
||||
createDatabaseContext,
|
||||
type DatabaseContext,
|
||||
} from "../src/db/database-context.js";
|
||||
import { CircuitDeviceRowTransactionRepository } from "../src/db/repositories/circuit-device-row-transaction.repository.js";
|
||||
import { DistributionBoardRepository } from "../src/db/repositories/distribution-board.repository.js";
|
||||
import { circuitDeviceRows } from "../src/db/schema/circuit-device-rows.js";
|
||||
import { circuitSections } from "../src/db/schema/circuit-sections.js";
|
||||
import { circuits } from "../src/db/schema/circuits.js";
|
||||
import { projects } from "../src/db/schema/projects.js";
|
||||
|
||||
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();
|
||||
const board = new DistributionBoardRepository(
|
||||
context.db
|
||||
).createWithCircuitListAndDefaultSections(
|
||||
"project-1",
|
||||
"UV-01"
|
||||
);
|
||||
const [section] = context.db
|
||||
.select()
|
||||
.from(circuitSections)
|
||||
.where(eq(circuitSections.circuitListId, board.id))
|
||||
.limit(1)
|
||||
.all();
|
||||
context.db
|
||||
.insert(circuits)
|
||||
.values({
|
||||
id: "circuit-1",
|
||||
circuitListId: board.id,
|
||||
sectionId: section.id,
|
||||
equipmentIdentifier: "-1F1",
|
||||
displayName: "Reserve",
|
||||
sortOrder: 10,
|
||||
isReserve: 1,
|
||||
})
|
||||
.run();
|
||||
return context;
|
||||
}
|
||||
|
||||
function insertDeviceRow(context: DatabaseContext) {
|
||||
context.db
|
||||
.insert(circuitDeviceRows)
|
||||
.values({
|
||||
id: "row-1",
|
||||
circuitId: "circuit-1",
|
||||
sortOrder: 10,
|
||||
name: "Leuchte",
|
||||
displayName: "Leuchte",
|
||||
quantity: 1,
|
||||
powerPerUnit: 0.1,
|
||||
simultaneityFactor: 1,
|
||||
})
|
||||
.run();
|
||||
context.db
|
||||
.update(circuits)
|
||||
.set({ isReserve: 0 })
|
||||
.where(eq(circuits.id, "circuit-1"))
|
||||
.run();
|
||||
}
|
||||
|
||||
describe("circuit device-row transaction repository", () => {
|
||||
it("commits a new device row and clears the circuit reserve status together", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
const repository = new CircuitDeviceRowTransactionRepository(context.db);
|
||||
const rowId = repository.createInCircuit({
|
||||
circuitId: "circuit-1",
|
||||
name: "Steckdose",
|
||||
displayName: "Steckdose",
|
||||
quantity: 1,
|
||||
powerPerUnit: 0.2,
|
||||
simultaneityFactor: 1,
|
||||
});
|
||||
|
||||
const [row] = context.db
|
||||
.select()
|
||||
.from(circuitDeviceRows)
|
||||
.where(eq(circuitDeviceRows.id, rowId))
|
||||
.all();
|
||||
const [circuit] = context.db
|
||||
.select()
|
||||
.from(circuits)
|
||||
.where(eq(circuits.id, "circuit-1"))
|
||||
.all();
|
||||
|
||||
assert.equal(row.circuitId, "circuit-1");
|
||||
assert.equal(row.sortOrder, 10);
|
||||
assert.equal(circuit.isReserve, 0);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rolls back the row insert when clearing the reserve status fails", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
context.sqlite.exec(`
|
||||
CREATE TRIGGER fail_reserve_clear
|
||||
BEFORE UPDATE OF is_reserve ON circuits
|
||||
WHEN NEW.is_reserve = 0
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'forced reserve clear failure');
|
||||
END;
|
||||
`);
|
||||
const repository = new CircuitDeviceRowTransactionRepository(context.db);
|
||||
|
||||
assert.throws(
|
||||
() =>
|
||||
repository.createInCircuit({
|
||||
circuitId: "circuit-1",
|
||||
name: "Steckdose",
|
||||
displayName: "Steckdose",
|
||||
quantity: 1,
|
||||
powerPerUnit: 0.2,
|
||||
simultaneityFactor: 1,
|
||||
}),
|
||||
/forced reserve clear failure/
|
||||
);
|
||||
|
||||
assert.equal(context.db.select().from(circuitDeviceRows).all().length, 0);
|
||||
assert.equal(context.db.select().from(circuits).all()[0].isReserve, 1);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("commits the last-row deletion and activates the circuit reserve status together", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
insertDeviceRow(context);
|
||||
const repository = new CircuitDeviceRowTransactionRepository(context.db);
|
||||
|
||||
repository.deleteFromCircuit("row-1", "circuit-1");
|
||||
|
||||
assert.equal(context.db.select().from(circuitDeviceRows).all().length, 0);
|
||||
assert.equal(context.db.select().from(circuits).all()[0].isReserve, 1);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("rolls back the row deletion when activating the reserve status fails", () => {
|
||||
const context = createTestDatabase();
|
||||
try {
|
||||
insertDeviceRow(context);
|
||||
context.sqlite.exec(`
|
||||
CREATE TRIGGER fail_reserve_activation
|
||||
BEFORE UPDATE OF is_reserve ON circuits
|
||||
WHEN NEW.is_reserve = 1
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'forced reserve activation failure');
|
||||
END;
|
||||
`);
|
||||
const repository = new CircuitDeviceRowTransactionRepository(context.db);
|
||||
|
||||
assert.throws(
|
||||
() => repository.deleteFromCircuit("row-1", "circuit-1"),
|
||||
/forced reserve activation failure/
|
||||
);
|
||||
|
||||
assert.equal(context.db.select().from(circuitDeviceRows).all().length, 1);
|
||||
assert.equal(context.db.select().from(circuits).all()[0].isReserve, 0);
|
||||
} finally {
|
||||
context.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -125,7 +125,9 @@ describe("circuit write service rules", () => {
|
||||
async findById() {
|
||||
return { id: "r1", circuitId: "c1" } as never;
|
||||
},
|
||||
deleteFromCircuitTransactional(rowId: string, circuitId: string) {
|
||||
} as never,
|
||||
deviceRowTransactionStore: {
|
||||
deleteFromCircuit(rowId: string, circuitId: string) {
|
||||
transactionalDelete = { rowId, circuitId };
|
||||
},
|
||||
} as never,
|
||||
@@ -170,14 +172,16 @@ describe("circuit write service rules", () => {
|
||||
},
|
||||
} as never,
|
||||
deviceRowRepository: {
|
||||
createInCircuitTransactional(input: typeof transactionalCreate) {
|
||||
transactionalCreate = input;
|
||||
return "row1";
|
||||
},
|
||||
async findById() {
|
||||
return { id: "row1" } as never;
|
||||
},
|
||||
} as never,
|
||||
deviceRowTransactionStore: {
|
||||
createInCircuit(input: typeof transactionalCreate) {
|
||||
transactionalCreate = input;
|
||||
return "row1";
|
||||
},
|
||||
} as never,
|
||||
circuitListRepository: {} as never,
|
||||
circuitSectionRepository: {} as never,
|
||||
projectDeviceRepository: {
|
||||
@@ -921,163 +925,6 @@ describe("circuit write service rules", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("device row creation and reserve activation share one synchronous transaction", () => {
|
||||
const repository = new CircuitDeviceRowRepository();
|
||||
const originalTransaction = (db as unknown as { transaction: unknown }).transaction;
|
||||
|
||||
let callbackReturnedPromise = false;
|
||||
let insertedValues: { id?: string; circuitId?: string; sortOrder?: number } | undefined;
|
||||
let reserveValue: number | undefined;
|
||||
let selectCall = 0;
|
||||
|
||||
(db as unknown as { transaction: (cb: (tx: unknown) => unknown) => void }).transaction = (cb) => {
|
||||
const fakeTx = {
|
||||
select() {
|
||||
const rows = selectCall++ === 0 ? [{ id: "c1" }] : [{ sortOrder: 20 }];
|
||||
const query = {
|
||||
from() {
|
||||
return query;
|
||||
},
|
||||
where() {
|
||||
return query;
|
||||
},
|
||||
limit() {
|
||||
return query;
|
||||
},
|
||||
all() {
|
||||
return rows;
|
||||
},
|
||||
};
|
||||
return query;
|
||||
},
|
||||
insert() {
|
||||
return {
|
||||
values(values: typeof insertedValues) {
|
||||
insertedValues = values;
|
||||
return {
|
||||
run() {
|
||||
return { changes: 1 };
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
update() {
|
||||
return {
|
||||
set(values: { isReserve: number }) {
|
||||
reserveValue = values.isReserve;
|
||||
return {
|
||||
where() {
|
||||
return {
|
||||
run() {
|
||||
return { changes: 1 };
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
const callbackResult = cb(fakeTx);
|
||||
callbackReturnedPromise = Boolean(
|
||||
callbackResult && typeof (callbackResult as Promise<unknown>).then === "function"
|
||||
);
|
||||
};
|
||||
|
||||
try {
|
||||
const rowId = repository.createInCircuitTransactional({
|
||||
circuitId: "c1",
|
||||
name: "Load",
|
||||
displayName: "Load",
|
||||
quantity: 1,
|
||||
powerPerUnit: 1,
|
||||
simultaneityFactor: 1,
|
||||
});
|
||||
assert.equal(callbackReturnedPromise, false);
|
||||
assert.equal(insertedValues?.id, rowId);
|
||||
assert.equal(insertedValues?.circuitId, "c1");
|
||||
assert.equal(insertedValues?.sortOrder, 30);
|
||||
assert.equal(reserveValue, 0);
|
||||
} finally {
|
||||
(db as unknown as { transaction: unknown }).transaction = originalTransaction;
|
||||
}
|
||||
});
|
||||
|
||||
it("device row deletion and reserve update share one synchronous transaction", () => {
|
||||
const repository = new CircuitDeviceRowRepository();
|
||||
const originalTransaction = (db as unknown as { transaction: unknown }).transaction;
|
||||
|
||||
let callbackReturnedPromise = false;
|
||||
let deleteCount = 0;
|
||||
let reserveValue: number | undefined;
|
||||
let selectCall = 0;
|
||||
|
||||
(db as unknown as { transaction: (cb: (tx: unknown) => unknown) => void }).transaction = (cb) => {
|
||||
const fakeTx = {
|
||||
select() {
|
||||
const rows = selectCall++ === 0 ? [{ id: "r1", circuitId: "c1" }] : [];
|
||||
const query = {
|
||||
from() {
|
||||
return query;
|
||||
},
|
||||
where() {
|
||||
return query;
|
||||
},
|
||||
limit() {
|
||||
return query;
|
||||
},
|
||||
all() {
|
||||
return rows;
|
||||
},
|
||||
};
|
||||
return query;
|
||||
},
|
||||
delete() {
|
||||
return {
|
||||
where() {
|
||||
return {
|
||||
run() {
|
||||
deleteCount += 1;
|
||||
return { changes: 1 };
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
update() {
|
||||
return {
|
||||
set(values: { isReserve: number }) {
|
||||
reserveValue = values.isReserve;
|
||||
return {
|
||||
where() {
|
||||
return {
|
||||
run() {
|
||||
return { changes: 1 };
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
const callbackResult = cb(fakeTx);
|
||||
callbackReturnedPromise = Boolean(
|
||||
callbackResult && typeof (callbackResult as Promise<unknown>).then === "function"
|
||||
);
|
||||
};
|
||||
|
||||
try {
|
||||
repository.deleteFromCircuitTransactional("r1", "c1");
|
||||
assert.equal(callbackReturnedPromise, false);
|
||||
assert.equal(deleteCount, 1);
|
||||
assert.equal(reserveValue, 1);
|
||||
} finally {
|
||||
(db as unknown as { transaction: unknown }).transaction = originalTransaction;
|
||||
}
|
||||
});
|
||||
|
||||
it("new circuit and initial device rows share one synchronous transaction", () => {
|
||||
const repository = new CircuitDeviceRowRepository();
|
||||
const originalTransaction = (db as unknown as { transaction: unknown }).transaction;
|
||||
|
||||
Reference in New Issue
Block a user