mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 08:30:05 +00:00
test(db): createTestOrm binds an ORM to a suite's own SQLite handle
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
|
||||
import { createTestDb, resetTestDb } from '../../helpers/test-db';
|
||||
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
|
||||
import { createUser } from '../../helpers/factories';
|
||||
import { Users } from '../../../src/db/entities/Users.entity';
|
||||
|
||||
const testDb = createTestDb();
|
||||
let t: TestOrm;
|
||||
|
||||
beforeAll(async () => { t = await createTestOrm(testDb); });
|
||||
beforeEach(() => { resetTestDb(testDb); t.clear(); });
|
||||
afterAll(async () => { await t.close(); testDb.close(); });
|
||||
|
||||
describe('createTestOrm', () => {
|
||||
it('ORM-001: reads through the very handle it was given', async () => {
|
||||
const { user } = createUser(testDb, { username: 'alice' });
|
||||
const found = await t.repo(Users).findOne({ id: user.id });
|
||||
expect(found?.username).toBe('alice');
|
||||
});
|
||||
|
||||
it('ORM-002: clear() drops the identity map so a raw update is visible', async () => {
|
||||
const { user } = createUser(testDb, { username: 'bob' });
|
||||
await t.repo(Users).findOne({ id: user.id });
|
||||
testDb.prepare('UPDATE users SET username = ? WHERE id = ?').run('robert', user.id);
|
||||
t.clear();
|
||||
const again = await t.repo(Users).findOne({ id: user.id });
|
||||
expect(again?.username).toBe('robert');
|
||||
});
|
||||
|
||||
it('ORM-003: closing the ORM leaves the handle usable', async () => {
|
||||
const local = await createTestOrm(testDb);
|
||||
// Force a real connection first — MikroORM connects lazily, so closing an
|
||||
// untouched ORM would prove nothing about NonClosingSqliteDriver.
|
||||
await local.repo(Users).findOne({ id: 1 });
|
||||
await local.close();
|
||||
expect(testDb.prepare('SELECT 1 AS one').get()).toEqual({ one: 1 });
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user