Files
TREK/server/tests/helpers/tours-repos.ts
T
Maurice f7edd849ab fix(tours): one tour type catalogue, and no column nothing writes
tour_types now holds every key the shared contract accepts, all but hike
disabled, and create and edit turn a disabled type away with a 400 instead
of failing on the foreign key. tours.tour_group_id is dropped: nothing ever
wrote or read it. difficulty and wanderer_ref stay for the Wanderer import.
2026-10-07 20:54:10 +02:00

45 lines
2.0 KiB
TypeScript

import type Database from 'better-sqlite3';
import { sharedTestOrm } from './test-uow';
import { Tours } from '../../src/db/entities/Tours.entity';
import type { ToursRepository } from '../../src/db/repositories/Tours.repository';
import { TourTypes } from '../../src/db/entities/TourTypes.entity';
import type { TourTypesRepository } from '../../src/db/repositories/TourTypes.repository';
import { TourWaypoints } from '../../src/db/entities/TourWaypoints.entity';
import type { TourWaypointsRepository } from '../../src/db/repositories/TourWaypoints.repository';
/**
* Tours test-only repository factories, bound to a suite's own better-sqlite3
* handle through the same memoised `sharedTestOrm` `test-uow.ts` exports, so
* a repository built here and a `UnitOfWork` from `createTestUnitOfWork`
* resolve the same `EntityManager` (the `todo-repos.ts` precedent).
*/
export function createTestToursRepo(db: Database.Database): Promise<ToursRepository> {
return sharedTestOrm(db).then((t) => t.repo(Tours));
}
export function createTestTourTypesRepo(db: Database.Database): Promise<TourTypesRepository> {
return sharedTestOrm(db).then((t) => t.repo(TourTypes));
}
export function createTestTourWaypointsRepo(db: Database.Database): Promise<TourWaypointsRepository> {
return sharedTestOrm(db).then((t) => t.repo(TourWaypoints));
}
/** A `tours` row for an existing place, with the defaults a GPX import writes. */
export function createTour(
db: Database.Database,
placeId: number,
overrides: Partial<{ distance: number | null; match_confidence: number | null; max_hiking_difficulty: number; created_at: string }> = {},
): void {
db.prepare(
`INSERT INTO tours (place_id, tour_type, distance, match_confidence, max_hiking_difficulty, created_at)
VALUES (?, 'hike', ?, ?, ?, COALESCE(?, CURRENT_TIMESTAMP))`,
).run(
placeId,
overrides.distance ?? null,
overrides.match_confidence ?? null,
overrides.max_hiking_difficulty ?? 2,
overrides.created_at ?? null,
);
}