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

54 lines
1.8 KiB
TypeScript

import type { EntityDTO } from '@mikro-orm/core';
import { TrekPhotos } from '../../../src/db/entities/TrekPhotos.entity';
import { TripAlbumLinks } from '../../../src/db/entities/TripAlbumLinks.entity';
import { TripPhotos } from '../../../src/db/entities/TripPhotos.entity';
import type { FactoryOrm } from './context';
import { createRow, findRow, insertRow } from './rows';
export type TripPhotoRow = EntityDTO<TripPhotos>;
/**
* Registers the provider asset in the central photo table (once per
* provider, asset and owner) and puts it on the trip, private unless
* `shared` is set.
*/
export async function addTripPhoto(
orm: FactoryOrm,
tripId: number,
userId: number,
assetId: string,
provider: string,
opts: { shared?: boolean; albumLinkId?: number } = {},
): Promise<TripPhotoRow> {
// Looked up first rather than upserted: the unique index on (provider,
// asset_id, owner_id) is partial, and SQLite only matches an ON CONFLICT
// target to a partial index that repeats its WHERE clause.
const known = await findRow(orm, TrekPhotos, { provider, asset_id: assetId, owner: userId });
const photoId = known?.id ?? (await insertRow(orm, TrekPhotos, { provider, asset_id: assetId, owner: userId }));
return createRow(orm, TripPhotos, {
trip: tripId,
user: userId,
photo: photoId,
shared: opts.shared ? 1 : 0,
albumLink: opts.albumLinkId ?? null,
});
}
/** Links a provider album to the trip for the user. */
export function addAlbumLink(
orm: FactoryOrm,
tripId: number,
userId: number,
provider: string,
albumId: string,
albumName = 'Test Album',
): Promise<EntityDTO<TripAlbumLinks>> {
return createRow(orm, TripAlbumLinks, {
trip: tripId,
user: userId,
provider,
album_id: albumId,
album_name: albumName,
});
}