Skip to content

Commit

Permalink
Add a constraint that special priority frames must be exactly one day…
Browse files Browse the repository at this point in the history
… long

Since they are meant to be used for "special days".
  • Loading branch information
Leitsi committed Oct 17, 2023
1 parent e11ae13 commit 483724a
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE vehicle_schedule.vehicle_schedule_frame
DROP CONSTRAINT special_priority_validity_exactly_one_day
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ALTER TABLE vehicle_schedule.vehicle_schedule_frame
ADD CONSTRAINT special_priority_validity_exactly_one_day
CHECK (NOT (
priority = internal_utils.const_timetables_priority_special()
AND validity_start != validity_end
));
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import { expectErrorResponse, expectNoErrorResponse } from '@util/response';
import { getPartialTableData, insertTableData, setupDb } from '@util/setup';
import { defaultTimetablesDataset } from 'generic/timetablesdb/datasets/defaultSetup/default-timetables-dataset';
import { GenericTimetablesDbTables } from 'generic/timetablesdb/datasets/schema';
import { TimetablePriority } from 'generic/timetablesdb/datasets/types';
import {
TimetablePriority,
VehicleScheduleFrame,
} from 'generic/timetablesdb/datasets/types';
import {
buildUpdateBlockMutation,
buildUpdateJourneyPatternRefMutation,
Expand Down Expand Up @@ -149,15 +152,15 @@ describe('Vehicle schedule frame - journey pattern ref uniqueness constraint', (
dataset.passingTimes.push(...newStopPoints);
};

const setBaseDatasetSchedulePriority = async (
priority: TimetablePriority,
const updateBaseDatasetFrame = async (
toUpdate: Partial<VehicleScheduleFrame>,
) => {
const updateMutation = addMutationWrapper(
buildUpdateVehicleScheduleFrameMutation(
builtDefaultDataset._vehicle_schedule_frames.winter2022
.vehicle_schedule_frame_id,
{
priority,
...toUpdate,
},
),
);
Expand Down Expand Up @@ -226,24 +229,33 @@ describe('Vehicle schedule frame - journey pattern ref uniqueness constraint', (
it('should succeed when priorities are different', async () => {
const dataset = cloneBaseDataset();
expect(dataset.frame.priority).toBe(TimetablePriority.Standard);
dataset.frame.priority = TimetablePriority.Special;
dataset.frame.priority = TimetablePriority.Temporary;

await expect(insertDataset(dataset)).resolves.not.toThrow();
});

it('should fail if trying to insert with same priority if priority is Special (or lower)', async () => {
await setBaseDatasetSchedulePriority(TimetablePriority.Special);
// Special day priority frames must have validity of exactly one day, so need to set that first.
const specialDay = DateTime.fromISO('2023-01-23');
await updateBaseDatasetFrame({
validity_start: specialDay,
validity_end: specialDay,
});

await updateBaseDatasetFrame({ priority: TimetablePriority.Special });

const dataset = cloneBaseDataset();
dataset.frame.priority = TimetablePriority.Special;
dataset.frame.validity_start = specialDay;
dataset.frame.validity_end = specialDay;

await expect(insertDataset(dataset)).rejects.toThrow(
'conflicting schedules detected',
);
});

it('should successfully insert with same priority if priority is Draft, even if schedule otherwise conflicts', async () => {
await setBaseDatasetSchedulePriority(TimetablePriority.Draft);
await updateBaseDatasetFrame({ priority: TimetablePriority.Draft });

const dataset = cloneBaseDataset();
dataset.frame.priority = TimetablePriority.Draft;
Expand All @@ -252,7 +264,7 @@ describe('Vehicle schedule frame - journey pattern ref uniqueness constraint', (
});

it('should successfully insert with same priority if priority is Staging, even if schedule otherwise conflicts', async () => {
await setBaseDatasetSchedulePriority(TimetablePriority.Staging);
await updateBaseDatasetFrame({ priority: TimetablePriority.Staging });

const dataset = cloneBaseDataset();
dataset.frame.priority = TimetablePriority.Staging;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { timetablesDbConfig } from '@config';
import {
DbConnection,
closeDbConnection,
createDbConnection,
singleQuery,
} from '@util/db';
import { setupDb } from '@util/setup';
import { defaultTimetablesDataset } from 'generic/timetablesdb/datasets/defaultSetup/default-timetables-dataset';
import { TimetablePriority } from 'generic/timetablesdb/datasets/types';
import {
buildGenericTimetablesDataset,
createGenericTableData,
} from 'timetables-data-inserter';

describe('Vehicle schedule frame - special day priority validity range constraint', () => {
let dbConnection: DbConnection;

beforeAll(() => {
dbConnection = createDbConnection(timetablesDbConfig);
});

afterAll(() => closeDbConnection(dbConnection));

const builtDefaultDataset = buildGenericTimetablesDataset(
defaultTimetablesDataset,
);

const testFrameId =
builtDefaultDataset._vehicle_schedule_frames.winter2022
.vehicle_schedule_frame_id;

beforeEach(async () =>
setupDb(dbConnection, createGenericTableData(builtDefaultDataset)),
);

const setTestFramePriorityAndValidity = (
priority: TimetablePriority,
validityStart: string,
validityEnd: string,
) => {
return singleQuery(
dbConnection,
`UPDATE vehicle_schedule.vehicle_schedule_frame
SET "priority" = ${priority},
"validity_start" = '${validityStart}',
"validity_end" = '${validityEnd}'
WHERE "vehicle_schedule_frame_id" = '${testFrameId}'
`,
);
};

it('should be able to set Special priority to have one day validity period', async () => {
await expect(
setTestFramePriorityAndValidity(
TimetablePriority.Special,
'2023-01-23',
'2023-01-23',
),
).resolves.not.toThrow();
});

it('should not be able to set Special priority to have longer than one day validity period', async () => {
await expect(
setTestFramePriorityAndValidity(
TimetablePriority.Special,
'2023-01-23',
'2023-01-24',
),
).rejects.toThrow('special_priority_validity_exactly_one_day');
});

const otherPrioritiesToTest: Array<keyof typeof TimetablePriority> = [
'Standard',
'Temporary',
'Draft',
'Staging',
];
otherPrioritiesToTest.forEach((priorityName) => {
it(`should also be able to set other priorities to have one day validity range: ${priorityName}`, async () => {
await expect(
setTestFramePriorityAndValidity(
TimetablePriority[priorityName],
'2023-01-23',
'2023-01-23',
),
).resolves.not.toThrow();
});
});
});

0 comments on commit 483724a

Please sign in to comment.