Files
TREK/server/tests/helpers/factories/collections.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

66 lines
2.3 KiB
TypeScript

import type { EntityData, EntityDTO } from '@mikro-orm/core';
import { CollectionMembers } from '../../../src/db/entities/CollectionMembers.entity';
import { CollectionPlaces } from '../../../src/db/entities/CollectionPlaces.entity';
import { Collections } from '../../../src/db/entities/Collections.entity';
import { inContext, nextSeq, type FactoryOrm } from './context';
import { createRow } from './rows';
export type CollectionRow = EntityDTO<Collections>;
export type CollectionPlaceRow = EntityDTO<CollectionPlaces>;
/** A collection owned by `ownerId`. */
export function makeCollection(
orm: FactoryOrm,
ownerId: number,
overrides: EntityData<Collections> = {},
): Promise<CollectionRow> {
return createRow(orm, Collections, {
owner: ownerId,
name: `Test Collection ${nextSeq('collection')}`,
...overrides,
});
}
/** Invites the user into the collection; `status: 'accepted'` makes them a member right away. */
export function addCollectionMember(
orm: FactoryOrm,
collectionId: number,
userId: number,
overrides: EntityData<CollectionMembers> = {},
): Promise<EntityDTO<CollectionMembers>> {
return createRow(orm, CollectionMembers, {
collection: collectionId,
user: userId,
status: 'accepted',
role: 'editor',
...overrides,
});
}
/** A place saved into the collection by `ownerId`. */
export function makeCollectionPlace(
orm: FactoryOrm,
collectionId: number,
ownerId: number,
overrides: EntityData<CollectionPlaces> = {},
): Promise<CollectionPlaceRow> {
return createRow(orm, CollectionPlaces, {
collection: collectionId,
owner: ownerId,
name: `Saved Place ${nextSeq('collection-place')}`,
lat: 48.8566,
lng: 2.3522,
...overrides,
});
}
/** Puts the tags on the saved place. */
export async function tagCollectionPlace(orm: FactoryOrm, collectionPlaceId: number, tagIds: number[]): Promise<void> {
await inContext(orm, (em) => em.getRepository(CollectionPlaces).attachTags(collectionPlaceId, tagIds));
}
/** Files the saved place under the collection label. */
export async function labelCollectionPlace(orm: FactoryOrm, collectionPlaceId: number, labelId: number): Promise<void> {
await inContext(orm, (em) => em.getRepository(CollectionPlaces).assignLabel(collectionPlaceId, labelId));
}