Make initial circuit creation atomic
This commit is contained in:
@@ -251,14 +251,16 @@ describe("circuit write service rules", () => {
|
||||
},
|
||||
} as never,
|
||||
deviceRowRepository: {
|
||||
createCircuitWithDeviceRowsTransactional(input: typeof transactionalCreate) {
|
||||
transactionalCreate = input;
|
||||
return { circuitId: "c-new", rowIds: ["r1", "r2"] };
|
||||
},
|
||||
async findById(rowId: string) {
|
||||
return { id: rowId, circuitId: "c-new" } as never;
|
||||
},
|
||||
} as never,
|
||||
deviceRowTransactionStore: {
|
||||
createCircuitWithDeviceRows(input: typeof transactionalCreate) {
|
||||
transactionalCreate = input;
|
||||
return { circuitId: "c-new", rowIds: ["r1", "r2"] };
|
||||
},
|
||||
} as never,
|
||||
});
|
||||
|
||||
const created = await service.createCircuitWithDeviceRows("p1", "l1", {
|
||||
@@ -925,76 +927,6 @@ describe("circuit write service rules", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("new circuit and initial device rows share one synchronous transaction", () => {
|
||||
const repository = new CircuitDeviceRowRepository();
|
||||
const originalTransaction = (db as unknown as { transaction: unknown }).transaction;
|
||||
|
||||
let callbackReturnedPromise = false;
|
||||
const insertedValues: Array<Record<string, unknown>> = [];
|
||||
(db as unknown as { transaction: (cb: (tx: unknown) => unknown) => void }).transaction = (cb) => {
|
||||
const fakeTx = {
|
||||
insert() {
|
||||
return {
|
||||
values(values: Record<string, unknown>) {
|
||||
insertedValues.push(values);
|
||||
return {
|
||||
run() {
|
||||
return { changes: 1 };
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
||||
const callbackResult = cb(fakeTx);
|
||||
callbackReturnedPromise = Boolean(
|
||||
callbackResult && typeof (callbackResult as Promise<unknown>).then === "function"
|
||||
);
|
||||
};
|
||||
|
||||
try {
|
||||
const created = repository.createCircuitWithDeviceRowsTransactional({
|
||||
circuit: {
|
||||
circuitListId: "l1",
|
||||
sectionId: "s1",
|
||||
equipmentIdentifier: "-2F1",
|
||||
displayName: "Steckdosen",
|
||||
sortOrder: 10,
|
||||
},
|
||||
deviceRows: [
|
||||
{
|
||||
name: "Socket",
|
||||
displayName: "Steckdose",
|
||||
quantity: 1,
|
||||
powerPerUnit: 0.2,
|
||||
simultaneityFactor: 1,
|
||||
sortOrder: 15,
|
||||
},
|
||||
{
|
||||
name: "Manual",
|
||||
displayName: "Manuell",
|
||||
quantity: 2,
|
||||
powerPerUnit: 0.1,
|
||||
simultaneityFactor: 0.8,
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
assert.equal(callbackReturnedPromise, false);
|
||||
assert.equal(insertedValues.length, 3);
|
||||
assert.equal(insertedValues[0].id, created.circuitId);
|
||||
assert.equal(insertedValues[0].isReserve, 0);
|
||||
assert.equal(insertedValues[1].id, created.rowIds[0]);
|
||||
assert.equal(insertedValues[1].circuitId, created.circuitId);
|
||||
assert.equal(insertedValues[1].sortOrder, 15);
|
||||
assert.equal(insertedValues[2].id, created.rowIds[1]);
|
||||
assert.equal(insertedValues[2].circuitId, created.circuitId);
|
||||
assert.equal(insertedValues[2].sortOrder, 20);
|
||||
} finally {
|
||||
(db as unknown as { transaction: unknown }).transaction = originalTransaction;
|
||||
}
|
||||
});
|
||||
|
||||
it("tracks local edits on linked device rows as overridden fields", async () => {
|
||||
let updatePayload: Record<string, unknown> = {};
|
||||
const current = {
|
||||
|
||||
Reference in New Issue
Block a user