test(reservations): cover the traveller insert that ignores existing rows

ReservationTravelers.insertIgnore had no test of its own. MikroORM 7.2.10 changed how upsertMany with a composite conflict key matches rows read back after a conflict, which is exactly the case of a traveller already on the booking next to a new one in one call. RS9-001 to RS9-004 pin that case, a duplicate id, another booking's row and an empty list; the first two fail with a plain insert.
This commit is contained in:
Maurice
2026-11-26 12:57:32 +01:00
parent 6905729ab6
commit 05f4c84885
@@ -7,10 +7,11 @@
import { ReservationTravelers } from '../../../../src/db/entities/ReservationTravelers.entity';
import { Users } from '../../../../src/db/entities/Users.entity';
import type { ReservationTravelersRepository } from '../../../../src/db/repositories/ReservationTravelers.repository';
import { withRequestContext } from '../../../../src/nest/database/request-context';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { createReservation, createTrip, createUser } from '../../../helpers/factories';
import { addReservationTraveler } from '../../../helpers/factories/reservations';
import { updateRows } from '../../../helpers/factories/rows';
import { findRows, updateRows } from '../../../helpers/factories/rows';
import { resetTestDb } from '../../../helpers/test-db';
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
@@ -89,3 +90,63 @@ describe('ReservationTravelersRepository — fully seeded world', () => {
expect((typed[0] as Record<string, unknown>).reservation_id).toBeUndefined();
});
});
describe('ReservationTravelersRepository.insertIgnore', () => {
const travelerIds = async (reservationId: number) =>
(await findRows(t, ReservationTravelers, { reservation: reservationId }))
.map((row) => row.user_id)
.sort((a, b) => a - b);
const insertIgnore = (reservationId: number, userIds: number[]) =>
withRequestContext(t.orm, () => repo.insertIgnore(reservationId, userIds));
it('RS9-001: a traveller already on the booking keeps their row and a new one is added in the same call', async () => {
const { user: owner } = createUser(testDb);
const { user: kept } = createUser(testDb);
const { user: added } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const reservation = createReservation(testDb, trip.id);
await addReservationTraveler(t, reservation.id, kept.id);
const [before] = await findRows(t, ReservationTravelers, { reservation: reservation.id });
await insertIgnore(reservation.id, [kept.id, added.id]);
expect(await travelerIds(reservation.id)).toEqual([kept.id, added.id].sort((a, b) => a - b));
const keptRow = (await findRows(t, ReservationTravelers, { reservation: reservation.id, user: kept.id }))[0];
expect(keptRow.id).toBe(before.id);
});
it('RS9-002: an id given twice in one call is written once', async () => {
const { user: owner } = createUser(testDb);
const { user: traveler } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const reservation = createReservation(testDb, trip.id);
await insertIgnore(reservation.id, [traveler.id, traveler.id]);
expect(await travelerIds(reservation.id)).toEqual([traveler.id]);
});
it('RS9-003: the same traveller on another booking neither conflicts nor changes', async () => {
const { user: owner } = createUser(testDb);
const { user: shared } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const first = createReservation(testDb, trip.id);
const second = createReservation(testDb, trip.id);
await addReservationTraveler(t, first.id, shared.id);
await insertIgnore(second.id, [shared.id]);
expect(await travelerIds(first.id)).toEqual([shared.id]);
expect(await travelerIds(second.id)).toEqual([shared.id]);
});
it('RS9-004: an empty list writes nothing', async () => {
const { user: owner } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
const reservation = createReservation(testDb, trip.id);
await insertIgnore(reservation.id, []);
expect(await travelerIds(reservation.id)).toEqual([]);
});
});