mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-23 07:26:30 +00:00
1bba65634c
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.
79 lines
2.3 KiB
TypeScript
79 lines
2.3 KiB
TypeScript
import type { EntityData, EntityDTO } from '@mikro-orm/core';
|
|
import { DayAccommodations } from '../../../src/db/entities/DayAccommodations.entity';
|
|
import { DayAssignments } from '../../../src/db/entities/DayAssignments.entity';
|
|
import { DayNotes } from '../../../src/db/entities/DayNotes.entity';
|
|
import { inContext, type FactoryOrm } from './context';
|
|
import { createRow } from './rows';
|
|
|
|
export type DayAssignmentRow = EntityDTO<DayAssignments>;
|
|
export type DayNoteRow = EntityDTO<DayNotes>;
|
|
export type DayAccommodationRow = EntityDTO<DayAccommodations>;
|
|
|
|
/** The next free `order_index` on the day: one past the highest, 0 on an empty day. */
|
|
function nextOrderIndex(orm: FactoryOrm, dayId: number): Promise<number> {
|
|
return inContext(orm, async (em) => {
|
|
const last = await em.findOne(
|
|
DayAssignments,
|
|
{ day: dayId },
|
|
{ orderBy: { order_index: 'desc' }, disableIdentityMap: true },
|
|
);
|
|
return last?.order_index == null ? 0 : last.order_index + 1;
|
|
});
|
|
}
|
|
|
|
/** Schedules the place on the day, after whatever the day already holds. */
|
|
export async function makeDayAssignment(
|
|
orm: FactoryOrm,
|
|
dayId: number,
|
|
placeId: number,
|
|
overrides: EntityData<DayAssignments> = {},
|
|
): Promise<DayAssignmentRow> {
|
|
const orderIndex = overrides.order_index ?? (await nextOrderIndex(orm, dayId));
|
|
return createRow(orm, DayAssignments, {
|
|
day: dayId,
|
|
place: placeId,
|
|
notes: null,
|
|
...overrides,
|
|
order_index: orderIndex,
|
|
});
|
|
}
|
|
|
|
/** A note on the day, sorted last unless `sort_order` says otherwise. */
|
|
export function makeDayNote(
|
|
orm: FactoryOrm,
|
|
dayId: number,
|
|
tripId: number,
|
|
overrides: EntityData<DayNotes> = {},
|
|
): Promise<DayNoteRow> {
|
|
return createRow(orm, DayNotes, {
|
|
day: dayId,
|
|
trip: tripId,
|
|
text: 'Test note',
|
|
time: null,
|
|
icon: '📝',
|
|
sort_order: 9999,
|
|
...overrides,
|
|
});
|
|
}
|
|
|
|
/** A stay at `placeId` from the start day to the end day. */
|
|
export function makeDayAccommodation(
|
|
orm: FactoryOrm,
|
|
tripId: number,
|
|
placeId: number | null,
|
|
startDayId: number,
|
|
endDayId: number,
|
|
overrides: EntityData<DayAccommodations> = {},
|
|
): Promise<DayAccommodationRow> {
|
|
return createRow(orm, DayAccommodations, {
|
|
trip: tripId,
|
|
place: placeId,
|
|
startDay: startDayId,
|
|
endDay: endDayId,
|
|
check_in: null,
|
|
check_out: null,
|
|
confirmation: null,
|
|
...overrides,
|
|
});
|
|
}
|