mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 05:26:47 +00:00
d8052c49b4
The demo seed parity case, the transaction yield probe and the test UnitOfWork check read their row counts and rows back with raw statements; they now use the ORM readers. The statements these files are about stay raw and are marked: the dirty read and write the transaction yield probe measures on the shared handle, and the reset-admin.js case, whose database file has no ORM bound to it.
45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
|
|
import { createSnapshotTestDb } from '../../helpers/db-mock';
|
|
import { resetTestDb } from '../../helpers/test-db';
|
|
import { createTestUnitOfWork, sharedTestOrm } from '../../helpers/test-uow';
|
|
import { countRows } from '../../helpers/factories/rows';
|
|
import { Users } from '../../../src/db/entities/Users.entity';
|
|
import { createUser } from '../../helpers/factories';
|
|
import type { UnitOfWork } from '../../../src/nest/database/unit-of-work';
|
|
|
|
const testDb = createSnapshotTestDb();
|
|
let uow: UnitOfWork;
|
|
|
|
beforeAll(async () => {
|
|
uow = await createTestUnitOfWork(testDb);
|
|
});
|
|
beforeEach(() => resetTestDb(testDb));
|
|
afterAll(() => testDb.close());
|
|
|
|
const countUsers = async () => countRows(await sharedTestOrm(testDb), Users);
|
|
|
|
describe('createTestUnitOfWork', () => {
|
|
it('TUOW-001: raw better-sqlite3 statements inside transactional() commit together', async () => {
|
|
await uow.transactional(async () => {
|
|
createUser(testDb);
|
|
createUser(testDb);
|
|
});
|
|
expect(await countUsers()).toBe(2);
|
|
});
|
|
|
|
it('TUOW-002: a throw rolls raw statements back', async () => {
|
|
await expect(
|
|
uow.transactional(async () => {
|
|
createUser(testDb);
|
|
throw new Error('boom');
|
|
}),
|
|
).rejects.toThrow('boom');
|
|
expect(await countUsers()).toBe(0);
|
|
});
|
|
|
|
it('TUOW-003: the helper is memoised per handle', async () => {
|
|
const again = await createTestUnitOfWork(testDb);
|
|
expect(again).toBe(uow);
|
|
});
|
|
});
|