diff --git a/server/tests/helpers/factories/README.md b/server/tests/helpers/factories/README.md index aa794760c..764096a49 100644 --- a/server/tests/helpers/factories/README.md +++ b/server/tests/helpers/factories/README.md @@ -27,6 +27,21 @@ switches to the snapshot when it converts (`tests/e2e/tags.e2e.test.ts` is the example). `resetTestDb(testDb)` between tests keeps working: each helper call opens a fresh context, so no identity map remembers a row the reset deleted. +A `buildApp()` suite on the real database module resets between tests with +`resetDatabase(orm)` from `../helpers/reset-database`, the same state +`resetTestDb` leaves on the snapshot (user data gone, ids still growing, the +default categories, addons and photo providers back, the seeded admin gone +on the first call) on SQLite and on Postgres alike, so the suite runs in the +Postgres lane too: + +```ts +beforeAll(async () => { + nestApp = await buildApp(); + orm = nestApp.get(MikroORM); +}); +beforeEach(() => resetDatabase(orm)); +``` + ## What is here One file per domain, each returning the row as the app reads it (`EntityDTO` diff --git a/server/tests/helpers/reset-database.ts b/server/tests/helpers/reset-database.ts new file mode 100644 index 000000000..e1055740d --- /dev/null +++ b/server/tests/helpers/reset-database.ts @@ -0,0 +1,156 @@ +/** + * The per-test reset for suites on the real database module, on either engine. + * + * `resetTestDb(testDb)` (test-db.ts) needs the better-sqlite3 handle of the + * SQLite snapshot mock, so a suite that resets with it can only run on + * SQLite. `resetDatabase(orm)` takes the booted app's ORM instead and leaves + * the same state on SQLite and on Postgres: + * + * - every table emptied except KEEP_TABLES, the list read from the live + * schema at reset time, not kept by hand; + * - id sequences left where they are, so ids keep growing from one test to + * the next, as resetTestDb does on purpose (see its comment); + * - the defaults seedDefaults writes put back from the same data + * (categories, addons, photo providers), through the ORM; + * - on its first call for an ORM, the users sequence restarted as well, + * which is the snapshot mock's one normalisation (db-mock.ts, + * createSnapshotTestDb): the seeded first-run admin is gone and the first + * user a suite creates gets id 1. + * + * Usage in a suite that boots the app: + * + * orm = nestApp.get(MikroORM); + * beforeEach(() => resetDatabase(orm)); + * + * How a table is emptied differs by engine, and that is all that does; the + * one branch is in clearUserTables below, on the dialect layer's platform + * check, like every other engine question. + */ +import { isPostgres, isSqlite, unsupported } from '../../src/db/dialect/platform'; +import { Addons } from '../../src/db/entities/Addons.entity'; +import { Categories } from '../../src/db/entities/Categories.entity'; +import { PhotoProviders } from '../../src/db/entities/PhotoProviders.entity'; +import { inContext, type FactoryOrm } from './factories/context'; +import { insertRowIgnoringConflict } from './factories/rows'; +import { DEFAULT_ADDONS, DEFAULT_CATEGORIES, DEFAULT_PHOTO_PROVIDERS, KEEP_TABLES } from './test-db'; +import type { EntityManager } from '@mikro-orm/core'; + +/** The ORMs whose users sequence has been restarted once already. */ +const normalised = new WeakSet(); + +/** Empties the user data and re-seeds the defaults; call it in beforeEach. */ +export async function resetDatabase(orm: FactoryOrm): Promise { + const restartUsers = !normalised.has(orm); + await inContext(orm, (em) => clearUserTables(em, restartUsers)); + normalised.add(orm); + await seedDefaultRows(orm); +} + +const quote = (table: string) => `"${table.replaceAll('"', '""')}"`; + +/** + * Empties every table but KEEP_TABLES, and with `restartUsers` sets the users + * sequence back so the next user is id 1. + * + * SQLite: as resetTestDb does, one DELETE per table with foreign keys off, so + * the order does not matter (the pragma cannot change inside a transaction, + * so this runs outside one). + * + * Postgres: one TRUNCATE for the tables, without RESTART IDENTITY so the + * sequences keep counting. A TRUNCATE refuses a table that a table outside + * the statement references, and CASCADE would follow such a reference into + * the kept tables (categories.user_id references users) and empty them too. + * So the tables a kept table references are left out of the TRUNCATE and + * emptied with a DELETE after it, when nothing else points at them any more; + * their foreign keys apply as on any delete (a category made by a deleted + * user loses its user_id, where SQLite, with foreign keys off, leaves the id + * of a user that is gone). All of it in one transaction. + */ +async function clearUserTables(em: EntityManager, restartUsers: boolean): Promise { + const connection = em.getConnection(); + const platform = em.getPlatform(); + + if (isSqlite(platform)) { + const rows = await connection.execute<{ name: string }[]>( + "select name from sqlite_master where type = 'table'", + [], + 'all', + ); + // sqlite_* are SQLite's own (sqlite_sequence, sqlite_stat1), never ours. + const tables = rows.map((r) => r.name).filter((name) => !name.startsWith('sqlite_') && !KEEP_TABLES.has(name)); + await connection.execute('PRAGMA foreign_keys = OFF', [], 'run'); + try { + for (const table of tables) await connection.execute(`DELETE FROM ${quote(table)}`, [], 'run'); + if (restartUsers) await connection.execute("DELETE FROM sqlite_sequence WHERE name = 'users'", [], 'run'); + } finally { + await connection.execute('PRAGMA foreign_keys = ON', [], 'run'); + } + return; + } + + if (isPostgres(platform)) { + const rows = await connection.execute<{ name: string }[]>( + 'select table_name as name from information_schema.tables ' + + "where table_schema = current_schema() and table_type = 'BASE TABLE'", + [], + 'all', + ); + const tables = rows.map((r) => r.name).filter((name) => !KEEP_TABLES.has(name)); + const references = await connection.execute<{ from_table: string; to_table: string }[]>( + 'select f.relname as from_table, t.relname as to_table from pg_constraint c ' + + 'join pg_class f on f.oid = c.conrelid join pg_class t on t.oid = c.confrelid ' + + "where c.contype = 'f' and f.relnamespace = current_schema()::regnamespace", + [], + 'all', + ); + const heldByKept = new Set(references.filter((r) => KEEP_TABLES.has(r.from_table)).map((r) => r.to_table)); + const truncated = tables.filter((name) => !heldByKept.has(name)); + const deleted = tables.filter((name) => heldByKept.has(name)); + await em.transactional(async (tx) => { + const run = (sql: string) => tx.getConnection().execute(sql, [], 'run', tx.getTransactionContext()); + if (truncated.length > 0) await run(`TRUNCATE ${truncated.map(quote).join(', ')}`); + for (const table of deleted) await run(`DELETE FROM ${quote(table)}`); + if (restartUsers) await run("select setval(pg_get_serial_sequence('users', 'id'), 1, false)"); + }); + return; + } + + unsupported(platform); +} + +/** + * seedDefaults (test-db.ts) through the ORM: a default category is added when + * no category has its name (categories.name is not unique), an addon or photo + * provider when its id is not there. Rows that are there stay as they are, + * an addon a test switched off included. + */ +async function seedDefaultRows(orm: FactoryOrm): Promise { + await inContext(orm, async (em) => { + for (const cat of DEFAULT_CATEGORIES) { + if ((await em.count(Categories, { name: cat.name })) === 0) { + await em.insert(Categories, { name: cat.name, color: cat.color, icon: cat.icon }); + } + } + }); + for (const a of DEFAULT_ADDONS) { + await insertRowIgnoringConflict(orm, Addons, { + id: a.id, + name: a.name, + description: a.description, + type: a.type, + icon: a.icon, + enabled: a.enabled === 1, + sort_order: a.sort_order, + }); + } + for (const p of DEFAULT_PHOTO_PROVIDERS) { + await insertRowIgnoringConflict(orm, PhotoProviders, { + id: p.id, + name: p.name, + description: p.id, + icon: 'Image', + enabled: p.enabled, + sort_order: 0, + }); + } +} diff --git a/server/tests/helpers/test-db.ts b/server/tests/helpers/test-db.ts index 6f5b4663c..9c69f8178 100644 --- a/server/tests/helpers/test-db.ts +++ b/server/tests/helpers/test-db.ts @@ -38,8 +38,9 @@ export { CAN_ACCESS_TRIP_SQL, buildDbMock } from './db-mock'; * is present (categories, addons, the provider catalogues) plus the two * migration bookkeeping tables. Everything else in the schema is user data and * is derived from `sqlite_master` at reset time — see `resetTestDb`. + * `resetDatabase` (reset-database.ts) keeps the same tables on either engine. */ -const KEEP_TABLES = new Set([ +export const KEEP_TABLES: ReadonlySet = new Set([ // Seeded reference data (`seedDefaults` below re-seeds the first three). 'categories', 'addons', @@ -57,7 +58,7 @@ const KEEP_TABLES = new Set([ 'mikro_orm_migrations', ]); -const DEFAULT_CATEGORIES = [ +export const DEFAULT_CATEGORIES = [ { name: 'Hotel', color: '#3b82f6', icon: '🏨' }, { name: 'Restaurant', color: '#ef4444', icon: '🍽️' }, { name: 'Attraction', color: '#8b5cf6', icon: '🏛️' }, @@ -70,7 +71,7 @@ const DEFAULT_CATEGORIES = [ { name: 'Other', color: '#6366f1', icon: '📍' }, ]; -const DEFAULT_ADDONS = [ +export const DEFAULT_ADDONS = [ { id: 'packing', name: 'Packing List', @@ -154,7 +155,7 @@ const DEFAULT_ADDONS = [ }, ]; -const DEFAULT_PHOTO_PROVIDERS = [ +export const DEFAULT_PHOTO_PROVIDERS = [ { id: 'immich', name: 'Immich', enabled: 1 }, { id: 'synologyphotos', name: 'Synology Photos', enabled: 1 }, ]; diff --git a/server/tests/unit/helpers/reset-database.test.ts b/server/tests/unit/helpers/reset-database.test.ts new file mode 100644 index 000000000..a13155667 --- /dev/null +++ b/server/tests/unit/helpers/reset-database.test.ts @@ -0,0 +1,99 @@ +/** + * RESETDB-001..005: resetDatabase (tests/helpers/reset-database.ts), the + * per-test reset of every suite on the real database module, leaves the state + * resetTestDb leaves on the SQLite snapshot: user data gone, the kept tables + * and their rows still there, ids still growing, the defaults back. This + * suite boots the app, so it runs on SQLite and in the Postgres lane alike, + * and each engine's half of the helper is held here. + */ +import { buildApp } from '../../../src/bootstrap'; +import { Addons } from '../../../src/db/entities/Addons.entity'; +import { AppSettings } from '../../../src/db/entities/AppSettings.entity'; +import { Categories } from '../../../src/db/entities/Categories.entity'; +import { PhotoProviders } from '../../../src/db/entities/PhotoProviders.entity'; +import { Trips } from '../../../src/db/entities/Trips.entity'; +import { Users } from '../../../src/db/entities/Users.entity'; +import type { FactoryOrm } from '../../helpers/factories/context'; +import { makeCategory } from '../../helpers/factories/places'; +import { countRows, deleteRows, findRow } from '../../helpers/factories/rows'; +import { setAddonEnabled, setAppSetting } from '../../helpers/factories/settings'; +import { makeTrip } from '../../helpers/factories/trips'; +import { makeUser } from '../../helpers/factories/users'; +import { resetDatabase } from '../../helpers/reset-database'; +import { DEFAULT_ADDONS, DEFAULT_CATEGORIES, DEFAULT_PHOTO_PROVIDERS } from '../../helpers/test-db'; +import { MikroORM } from '@mikro-orm/core'; +import type { INestApplication } from '@nestjs/common'; + +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; + +let app: INestApplication; +let orm: FactoryOrm; + +beforeAll(async () => { + app = await buildApp(); + orm = app.get(MikroORM); +}); + +afterAll(async () => { + await app?.close(); +}); + +describe('resetDatabase', () => { + // First on purpose: the boot's seeded admin is still there until the first reset. + it('RESETDB-001: the first reset removes the seeded admin and the next user gets id 1', async () => { + expect(await countRows(orm, Users)).toBeGreaterThan(0); + await resetDatabase(orm); + expect(await countRows(orm, Users)).toBe(0); + const { user } = await makeUser(orm); + expect(user.id).toBe(1); + }); + + it('RESETDB-002: later resets empty the user data but keep the ids growing', async () => { + await resetDatabase(orm); + const { user: first } = await makeUser(orm); + const trip = await makeTrip(orm, first.id); + await setAppSetting(orm, 'reset_probe', 'x'); + + await resetDatabase(orm); + expect(await countRows(orm, Users)).toBe(0); + expect(await countRows(orm, Trips)).toBe(0); + expect(await findRow(orm, AppSettings, { key: 'reset_probe' })).toBeNull(); + + const { user: second } = await makeUser(orm); + expect(second.id).toBeGreaterThan(first.id); + expect((await makeTrip(orm, second.id)).id).toBeGreaterThan(trip.id); + }); + + it('RESETDB-003: the kept tables keep their rows, a category a deleted user made included', async () => { + await resetDatabase(orm); + const { user } = await makeUser(orm); + const own = await makeCategory(orm, { name: 'Made by a user', user: user.id }); + const before = await countRows(orm, Categories); + + await resetDatabase(orm); + expect(await countRows(orm, Categories)).toBe(before); + expect(await findRow(orm, Categories, { id: own.id })).toMatchObject({ name: 'Made by a user' }); + }); + + it('RESETDB-004: the default categories, addons and photo providers come back, once each', async () => { + await deleteRows(orm, Categories, { name: DEFAULT_CATEGORIES[0].name }); + await deleteRows(orm, Addons, { id: DEFAULT_ADDONS[0].id }); + await deleteRows(orm, PhotoProviders, { id: DEFAULT_PHOTO_PROVIDERS[0].id }); + + await resetDatabase(orm); + await resetDatabase(orm); + for (const cat of DEFAULT_CATEGORIES) expect(await countRows(orm, Categories, { name: cat.name })).toBe(1); + for (const addon of DEFAULT_ADDONS) expect(await countRows(orm, Addons, { id: addon.id })).toBe(1); + for (const p of DEFAULT_PHOTO_PROVIDERS) expect(await countRows(orm, PhotoProviders, { id: p.id })).toBe(1); + const addon = await findRow(orm, Addons, { id: DEFAULT_ADDONS[0].id }); + expect(addon?.name).toBe(DEFAULT_ADDONS[0].name); + expect(Boolean(addon?.enabled)).toBe(DEFAULT_ADDONS[0].enabled === 1); + }); + + it('RESETDB-005: an addon a test switched keeps its setting, as resetTestDb leaves it', async () => { + await setAddonEnabled(orm, 'packing', false); + await resetDatabase(orm); + expect(Boolean((await findRow(orm, Addons, { id: 'packing' }))?.enabled)).toBe(false); + await setAddonEnabled(orm, 'packing', true); + }); +});