Files
TREK/server/tests/helpers/factories/budget.ts
T
Maurice 1bba65634c test(server): seed and read test data through ORM factories
The server suite seeds and reads its database through some 5,500 raw
better-sqlite3 statements on the sync handle. They restate the schema by
hand, skip the repositories the app goes through and run on SQLite only,
so nothing in the suite could ever exercise the dialect layer on another
engine.

tests/helpers/factories/ adds typed factories per domain (users, trips and
days, places, assignments, bookings, budget, packing, to-dos, files,
journeys, tours, collections, notifications, settings, plugins, tokens,
atlas, vacay, photos, collab) and generic readers (findRow, findRows,
countRows, updateRows, deleteRows) that go through MikroORM in a request
context of their own, against the app's ORM or a createTestOrm handle.
They refuse a write to a read-only foreign-key twin, which MikroORM would
otherwise drop without a word. The README there is the conversion guide.

The categories integration suite and the tags and calendar-feed e2e suites
are converted in full as worked examples; the tags suite also moves from
hand-rolled tables to the migrated snapshot.

lint:test-sql (scripts/test-sql-ratchet.mjs) holds the raw statements left
to a per-file baseline: growth and stale entries fail, files whose subject
is raw SQL are exempt with a reason, and a single statement can be allowed
with a test-sql-allow comment. CI runs it next to lint:boundaries.
2026-10-08 18:44:39 +02:00

44 lines
1.5 KiB
TypeScript

import type { EntityData, EntityDTO } from '@mikro-orm/core';
import { BudgetItemMembers } from '../../../src/db/entities/BudgetItemMembers.entity';
import { BudgetItemPayers } from '../../../src/db/entities/BudgetItemPayers.entity';
import { BudgetItems } from '../../../src/db/entities/BudgetItems.entity';
import type { FactoryOrm } from './context';
import { createRow } from './rows';
export type BudgetItemRow = EntityDTO<BudgetItems>;
/** An expense on the trip: "Test Budget Item", Transport, 100 in the trip currency. */
export function makeBudgetItem(
orm: FactoryOrm,
tripId: number,
overrides: EntityData<BudgetItems> = {},
): Promise<BudgetItemRow> {
return createRow(orm, BudgetItems, {
trip: tripId,
name: 'Test Budget Item',
category: 'Transport',
total_price: 100,
...overrides,
});
}
/** Adds the user to the expense's split, with an explicit share when `amount` is given. */
export function addBudgetItemMember(
orm: FactoryOrm,
budgetItemId: number,
userId: number,
overrides: EntityData<BudgetItemMembers> = {},
): Promise<EntityDTO<BudgetItemMembers>> {
return createRow(orm, BudgetItemMembers, { budgetItem: budgetItemId, user: userId, ...overrides });
}
/** Records that the user paid `amount` of the expense. */
export function addBudgetItemPayer(
orm: FactoryOrm,
budgetItemId: number,
userId: number,
amount: number,
): Promise<EntityDTO<BudgetItemPayers>> {
return createRow(orm, BudgetItemPayers, { budgetItem: budgetItemId, user: userId, amount });
}