leistungsbilanz-ts/tests/batched-insert.persistence.test.ts

20 lines
1 KiB
TypeScript

import assert from "node:assert/strict";
import path from "node:path";
import { describe, it } from "node:test";
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
import { createDatabaseContext } from "../src/db/database-context.js";
import { insertManyInBatches } from "../src/db/repositories/batched-insert.persistence.js";
import { projects } from "../src/db/schema/projects.js";
describe("batched SQLite inserts", () => {
it("keeps every batch inside the caller transaction", () => {
const context = createDatabaseContext(":memory:");
try {
migrate(context.db, { migrationsFolder: path.resolve("src", "db", "migrations") });
const values = Array.from({ length: 401 }, (_, index) => ({ id: `project-${index}`, name: `Projekt ${index}` }));
values[400] = { id: "project-0", name: "Duplikat" };
assert.throws(() => context.db.transaction((tx) => insertManyInBatches(tx, projects, values)));
assert.equal(context.db.select().from(projects).all().length, 0);
} finally { context.close(); }
});
});