Files
TREK/server/tests/helpers/tours-repos.ts
T
Maurice c956fe92ba Merge dev into feat/tours and port Tours onto MikroORM
dev now runs on MikroORM, so the positional migrations, seeds.ts and
DatabaseService the Tours server side was built on are gone. Behaviour
is unchanged; persistence moves onto dev's architecture:

- one MikroORM migration for tour_types, tours and tour_waypoints,
  idempotent so instances that ran the old positional steps keep
  their data; the hike tour type is seeded inside it, the addon row
  in the AddonSeeder
- entities and repositories for the three tables, tours domain on
  repositories and UnitOfWork
- tour facet joins in places, assignments and days re-applied on the
  repository code

Fixes found while porting:

- GPX import checks addon, trip access and permission in the handler
  instead of guards, so a refused upload gets a 403/404 instead of a
  connection reset
- copying a trip copies its tours and waypoints
- a tour cannot be moved onto a day that already has it
- updateTour checks existence inside its transaction

Adds tours e2e tests and rewrites the tours unit tests onto the
migrated test database.
2026-10-06 22:43:09 +02:00

39 lines
1.7 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 { 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 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,
);
}