Files
TREK/server/tests/unit/db/repositories/ReservationDayPositions.repository.test.ts
T
Isaias Tavares 62912e5db1 fix(db): run every repository statement the Postgres probe reaches
The probe's baseline is empty now: no repository method Postgres refuses,
none it cannot measure.

- reservations.accommodation_id (TEXT) is cast where it is compared with
  day_accommodations.id (INTEGER), in the four reservation reads and the two
  stay reads; SQLite coerced the bare comparison, Postgres refuses it. The
  joined RS18/RR1 read moves to reservation-join.ts beside the repository.
- The usage counters qualify the target row's column in DO UPDATE SET, which
  Postgres reads as ambiguous with `excluded` otherwise.
- ReservationDayPositions.upsertScoped upserts on its primary key instead of
  INSERT OR REPLACE; position is the only other column and nothing references
  the table, so the row left behind is the same.
- PRAGMA wal_checkpoint and VACUUM INTO move from MaintenanceRepository to
  SqliteMaintenance under src/db/engine/sqlite/, the SQLite engine's folder,
  where lint:dialect allows PRAGMA.
- The places list takes category and tag ids as numbers: the service converts
  the query text with toRowId and answers an empty list for one that is no
  id, instead of binding the text to an integer column.
- The probe samples the `present` flag of a partial update's pair as true, so
  the four update methods that sent no SQL are measured, and it now holds
  SQLSTATE class 22 too: a value of the wrong type reaching a column is the
  code's, since the sampler sends what each parameter declares.
2026-11-04 22:10:18 -03:00

117 lines
5.0 KiB
TypeScript

/**
* ReservationDayPositionsRepository — parity test (Plan 3d Task 7 whole-plan
* review, item 9/"missing parity tests": this repository had no standalone
* test file). ONE seeded world, one `toEqual(<legacy raw>)` test for the
* read method (RS19); RS31 (`upsertScoped`, a write) has coverage through
* `reservations.service.test.ts`'s `updatePositions` cases and, for the
* overwrite and the trip scoping its ON CONFLICT clause holds, below.
*/
import { ReservationDayPositions } from '../../../../src/db/entities/ReservationDayPositions.entity';
import type { ReservationDayPositionsRepository } from '../../../../src/db/repositories/ReservationDayPositions.repository';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { createDay, createReservation, createTrip, createUser } from '../../../helpers/factories';
import { findRows, insertRow } from '../../../helpers/factories/rows';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
const testDb = createSnapshotTestDb();
let t: TestOrm;
let repo: ReservationDayPositionsRepository;
beforeAll(async () => {
t = await createTestOrm(testDb);
repo = t.repo(ReservationDayPositions);
});
beforeEach(() => {
resetTestDb(testDb);
t.clear();
});
afterAll(async () => {
await t.close();
testDb.close();
});
describe('ReservationDayPositionsRepository — fully seeded world', () => {
const seed = async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day1 = createDay(testDb, trip.id);
const day2 = createDay(testDb, trip.id);
const multiDay = createReservation(testDb, trip.id, { title: 'Rental car', type: 'car' });
await insertRow(t, ReservationDayPositions, { reservation: multiDay.id, day: day1.id, position: 0 });
await insertRow(t, ReservationDayPositions, { reservation: multiDay.id, day: day2.id, position: 1 });
const otherTrip = createTrip(testDb, user.id);
const otherDay = createDay(testDb, otherTrip.id);
const foreignRes = createReservation(testDb, otherTrip.id, { title: 'Foreign rental', type: 'car' });
await insertRow(t, ReservationDayPositions, { reservation: foreignRes.id, day: otherDay.id, position: 0 });
return { trip, otherTrip, multiDay, day1, day2 };
};
it('RS19 listForTrip — matches the legacy JOIN statement, scoped by trip, both persist(false) mirror columns present', async () => {
const { trip, multiDay, day1, day2 } = await seed();
// test-sql-allow: the legacy statement is the oracle the repository read is held to.
const legacy = testDb
.prepare(
`
SELECT rdp.reservation_id, rdp.day_id, rdp.position FROM reservation_day_positions rdp
JOIN reservations r ON rdp.reservation_id = r.id WHERE r.trip_id = ?`,
)
.all(trip.id);
const typed = await repo.listForTrip(trip.id);
expect(typed).toEqual(legacy);
expect(typed.map((r) => [r.reservation_id, r.day_id, r.position]).sort()).toEqual(
[
[multiDay.id, day1.id, 0],
[multiDay.id, day2.id, 1],
].sort(),
);
});
it('RS19 listForTrip — an empty trip returns an empty array, not a throw', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
expect(await repo.listForTrip(trip.id)).toEqual([]);
});
it('listForReservation — the slots of one booking per day, none of another booking', async () => {
const { multiDay, day1, day2 } = await seed();
const rows = await repo.listForReservation(multiDay.id);
expect(rows.map((r) => [r.day_id, r.position]).sort()).toEqual([
[day1.id, 0],
[day2.id, 1],
]);
expect(await repo.listForReservation(999999)).toEqual([]);
});
});
describe('ReservationDayPositionsRepository.upsertScoped', () => {
it('RDP-UPSERT-001: a second write to the same reservation and day overwrites its position', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const reservation = createReservation(testDb, trip.id, { day_id: day.id });
await repo.upsertScoped(trip.id, reservation.id, day.id, 1.5);
await repo.upsertScoped(trip.id, reservation.id, day.id, 4);
const rows = await findRows(t.orm, ReservationDayPositions, { reservation_id: reservation.id });
expect(rows.map((r) => [r.day_id, r.position])).toEqual([[day.id, 4]]);
});
it('RDP-UPSERT-002: a reservation of another trip writes no row', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const otherTrip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const foreign = createReservation(testDb, otherTrip.id);
await repo.upsertScoped(trip.id, foreign.id, day.id, 2);
expect(await findRows(t.orm, ReservationDayPositions, { reservation_id: foreign.id })).toEqual([]);
});
});