mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 19:16:08 +00:00
fix(db): find no nearest day for a date the calendar does not have
The nearest-day fallback of a booking's time cast the bound date to a timestamp on Postgres, which refuses 2024-02-30 with a 500, while SQLite's JULIANDAY rolled it over into March and placed the booking there. dayDistance now refuses anything but a calendar date on both engines (isCalendarDate), so a caller that forgets the check fails on SQLite too, and findNearestDayId answers no day for such a date. The size baseline of Reservations.repository.ts drops by one line.
This commit is contained in:
@@ -226,6 +226,20 @@ describe('Update reservation', () => {
|
||||
expect(res.body.reservation.end_day_id).toBe(day3.id);
|
||||
});
|
||||
|
||||
it('RESV-004d — a reservation_time on a date the calendar does not have lands on no day, on every engine', async () => {
|
||||
const { user } = await makeUser(orm);
|
||||
const trip = await makeTrip(orm, user.id);
|
||||
await makeDay(orm, trip.id, { date: '2024-03-01' });
|
||||
|
||||
// SQLite's JULIANDAY rolls 2024-02-30 over into March; Postgres refuses to cast it.
|
||||
const res = await request(app)
|
||||
.post(`/api/trips/${trip.id}/reservations`)
|
||||
.set('Cookie', authCookie(user.id))
|
||||
.send({ title: 'Event', type: 'event', reservation_time: '2024-02-30T10:00' });
|
||||
expect(res.status).toBe(201);
|
||||
expect(res.body.reservation.day_id).toBeNull();
|
||||
});
|
||||
|
||||
it('RESV-004 — PUT on non-existent reservation returns 404', async () => {
|
||||
const { user } = await makeUser(orm);
|
||||
const trip = await makeTrip(orm, user.id);
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
dateOf,
|
||||
dayDistance,
|
||||
foundAgainState,
|
||||
isCalendarDate,
|
||||
lower,
|
||||
lowerParam,
|
||||
lowerTrim,
|
||||
@@ -48,6 +49,7 @@ import { createJourney, createJourneyEntry } from '../../../helpers/factories';
|
||||
import { resetTestDb } from '../../../helpers/test-db';
|
||||
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
|
||||
import { Platform } from '@mikro-orm/core';
|
||||
import { BasePostgreSqlPlatform, SqlitePlatform } from '@mikro-orm/sql';
|
||||
|
||||
import { expressionBuilder, type ExpressionBuilder } from 'kysely';
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
|
||||
@@ -982,6 +984,26 @@ describe('sql-functions (sqlite)', () => {
|
||||
expect(() => dayDistance(foreign, 'u.date', '2026-01-01')).toThrow(/no implementation for platform FakePlatform/);
|
||||
});
|
||||
|
||||
it('SQLF-100: dayDistance refuses a date the calendar does not have, on every engine, which SQLite would roll over', () => {
|
||||
expect(isCalendarDate('2024-02-29')).toBe(true);
|
||||
expect(isCalendarDate('2026-12-31')).toBe(true);
|
||||
for (const text of [
|
||||
'2024-02-30',
|
||||
'2023-02-29',
|
||||
'2024-13-01',
|
||||
'2024-00-10',
|
||||
'2024-04-31',
|
||||
'2024-1-01',
|
||||
'2024-01-01T10:00',
|
||||
'',
|
||||
]) {
|
||||
expect(isCalendarDate(text), text).toBe(false);
|
||||
}
|
||||
for (const platform of [new SqlitePlatform(), new BasePostgreSqlPlatform()]) {
|
||||
expect(() => dayDistance(platform, 'u.created_at', '2024-02-30')).toThrow(/needs a calendar date/);
|
||||
}
|
||||
});
|
||||
|
||||
// Plan 3d Task 0 review (H1/L1): the five helpers above return a MikroORM
|
||||
// RawQueryFragment, which stringifies to a literal `?` token and throws at
|
||||
// execution when handed into a Kysely statement — verified directly below
|
||||
|
||||
Reference in New Issue
Block a user