Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix journey pattern ref parsing in timetables data inserter #214

Merged
merged 5 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion test/hasura/timetables-data-inserter/generic/example.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@
},
{
"scheduled_stop_point_sequence": 2,
"scheduled_stop_point_label": "H2203"
"scheduled_stop_point_label": "H2203",
"timing_place_label": "TP001"
},
{
"scheduled_stop_point_sequence": 3,
Expand Down
189 changes: 115 additions & 74 deletions test/hasura/timetables-data-inserter/generic/json-schemas.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { TypeOfLine } from 'generic/networkdb/datasets/types';
import { TimetablePriority } from 'generic/timetablesdb/datasets/types';
import {
RouteDirection,
TimetablePriority,
} from 'generic/timetablesdb/datasets/types';
import { DateTime, Duration } from 'luxon';
import { z } from 'zod';
import { defaultDayTypeIds } from '../day-types';
Expand All @@ -24,6 +27,7 @@ export const localizedStringSchema = z
fi_FI: z.string(),
sv_FI: z.string(),
})
.strict()
.transform((name) => name as LocalizedString);

// Design choice: pass these enums by key value instead of the final value. Makes reading the easier.
Expand All @@ -49,76 +53,113 @@ const defaultDayTypeIdKeys = [
'SUNDAY',
] as const;

export type testType = typeof test;
const stopPointSchema = z.object({
scheduled_stop_point_label: z.string(),
scheduled_stop_point_sequence: z.number(),
});

const journeyPatternRefSchema = {
journey_pattern_ref_id: z.string().uuid().optional(),
observation_timestamp: dateTimeSchema.optional(),
type_of_line: z.nativeEnum(TypeOfLine).optional(),
snapshot_timestamp: dateTimeSchema.optional(),
journey_pattern_id: z.string().uuid().optional(),

_stop_points: z.array(stopPointSchema).optional(),
};

export const timetabledPassingTimeSchema = z.object({
timetabled_passing_time_id: z.string().uuid().optional(),
arrival_time: durationSchema.nullable().optional(),
departure_time: durationSchema.nullable().optional(),
vehicle_journey_id: z.string().uuid().optional(),
scheduled_stop_point_in_journey_pattern_ref_id: z.string().uuid().optional(),
_scheduled_stop_point_label: z.string(),
});

export const vehicleJourneySchema = z.object({
vehicle_journey_id: z.string().uuid().optional(),
block_id: z.string().uuid().optional(),
journey_pattern_ref_id: z.string().uuid().optional(),

_journey_pattern_ref_name: z.string(),
_passing_times: z.array(timetabledPassingTimeSchema).optional(),
});

export const vehicleServiceBlockSchema = z.object({
block_id: z.string().uuid().optional(),
vehicle_service_id: z.string().uuid().optional(),

_vehicle_journeys: z.record(vehicleJourneySchema).optional(),
});

export const vehicleServiceSchema = z.object({
vehicle_service_id: z.string().uuid().optional(),
day_type_id: z
.enum(defaultDayTypeIdKeys)
.transform((dayTypeIdKey) => defaultDayTypeIds[dayTypeIdKey])
.or(z.string().uuid()),
vehicle_schedule_frame_id: z.string().uuid().optional(),

_blocks: z.record(vehicleServiceBlockSchema).optional(),
});

export const vehicleScheduleFrameSchema = z.object({
vehicle_schedule_frame_id: z.string().uuid().optional(),
label: z.string().optional(),
// Note: ideally name and name_18n would be mutually exclusive, but there doesn't seem to be a way to implement that with zod.
name: z.string().optional(),
name_i18n: localizedStringSchema.optional(),
validity_start: dateSchema.optional(),
validity_end: dateSchema.optional(),
priority: z
.enum(timetablePriorities)
.transform((priority) => TimetablePriority[priority])
.optional(),
created_at: dateTimeSchema.optional(),

_vehicle_services: z.record(vehicleServiceSchema).optional(),
});

export const genericTimetablesJsonSchema = z.object({
_vehicle_schedule_frames: z.record(vehicleScheduleFrameSchema).optional(),
_journey_pattern_refs: z.record(z.object(journeyPatternRefSchema)).optional(),
});
const routeDirections = [
'inbound',
'outbound',
'clockwise',
'anticlockwise',
'northbound',
'southbound',
'eastbound',
'westbound',
] as const;

const stopPointSchema = z
.object({
scheduled_stop_point_label: z.string(),
scheduled_stop_point_sequence: z.number(),
timing_place_label: z.string().optional(),
})
.strict();

const journeyPatternRefSchema = z
.object({
journey_pattern_ref_id: z.string().uuid().optional(),
observation_timestamp: dateTimeSchema.optional(),
type_of_line: z.nativeEnum(TypeOfLine).optional(),
snapshot_timestamp: dateTimeSchema.optional(),
journey_pattern_id: z.string().uuid().optional(),
route_label: z.string().optional(),
route_direction: z
.enum(routeDirections)
.transform((direction) => direction as RouteDirection)
.optional(),
route_validity_start: dateSchema.optional(),
route_validity_end: dateSchema.optional(),

_stop_points: z.array(stopPointSchema).optional(),
})
.strict();

export const timetabledPassingTimeSchema = z
.object({
timetabled_passing_time_id: z.string().uuid().optional(),
arrival_time: durationSchema.nullable().optional(),
departure_time: durationSchema.nullable().optional(),
vehicle_journey_id: z.string().uuid().optional(),
scheduled_stop_point_in_journey_pattern_ref_id: z
.string()
.uuid()
.optional(),
_scheduled_stop_point_label: z.string(),
})
.strict();

export const vehicleJourneySchema = z
.object({
vehicle_journey_id: z.string().uuid().optional(),
block_id: z.string().uuid().optional(),
journey_pattern_ref_id: z.string().uuid().optional(),

_journey_pattern_ref_name: z.string(),
_passing_times: z.array(timetabledPassingTimeSchema).optional(),
})
.strict();

export const vehicleServiceBlockSchema = z
.object({
block_id: z.string().uuid().optional(),
vehicle_service_id: z.string().uuid().optional(),

_vehicle_journeys: z.record(vehicleJourneySchema).optional(),
})
.strict();

export const vehicleServiceSchema = z
.object({
vehicle_service_id: z.string().uuid().optional(),
day_type_id: z
.enum(defaultDayTypeIdKeys)
.transform((dayTypeIdKey) => defaultDayTypeIds[dayTypeIdKey])
.or(z.string().uuid()),
vehicle_schedule_frame_id: z.string().uuid().optional(),

_blocks: z.record(vehicleServiceBlockSchema).optional(),
})
.strict();

export const vehicleScheduleFrameSchema = z
.object({
vehicle_schedule_frame_id: z.string().uuid().optional(),
label: z.string().optional(),
// Note: ideally name and name_18n would be mutually exclusive, but there doesn't seem to be a way to implement that with zod.
name: z.string().optional(),
name_i18n: localizedStringSchema.optional(),
validity_start: dateSchema.optional(),
validity_end: dateSchema.optional(),
priority: z
.enum(timetablePriorities)
.transform((priority) => TimetablePriority[priority])
.optional(),
created_at: dateTimeSchema.optional(),

_vehicle_services: z.record(vehicleServiceSchema).optional(),
})
.strict();

export const genericTimetablesJsonSchema = z
.object({
_vehicle_schedule_frames: z.record(vehicleScheduleFrameSchema).optional(),
_journey_pattern_refs: z.record(journeyPatternRefSchema).optional(),
})
.strict();
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { DbConnection, closeDbConnection, createDbConnection } from '@util/db';
import { queryTable } from '@util/setup';
import { genericTimetablesDbSchema } from 'generic/timetablesdb/datasets/schema';
import { get } from 'lodash';
import { DateTime } from 'luxon';
import { defaultDayTypeIds } from 'timetables-data-inserter/day-types';
import { insertDatasetFromJson } from './data-insert';
import * as testDatasetJson from './example.json';
import testDatasetJson from './example.json';
import {
GenericTimetablesDatasetOutput,
GenericVehicleScheduleFrameOutput,
Expand Down Expand Up @@ -328,6 +329,22 @@ describe('Generic timetables data inserter json parser', () => {
builtJourneyPatternRef.journey_pattern_ref_id,
);
expect(inboundRef).toBeTruthy();

expect(inboundRef.observation_timestamp).toEqual(
DateTime.fromISO('2023-07-01T00:00:00+00:00'),
);
expect(inboundRef.snapshot_timestamp).toEqual(
DateTime.fromISO('2023-09-28T00:00:00+00:00'),
);
expect(inboundRef.type_of_line).toBe('stopping_bus_service');
expect(inboundRef.route_label).toBe('123');
expect(inboundRef.route_direction).toBe('inbound');
expect(inboundRef.route_validity_start).toEqual(
DateTime.fromISO('2023-06-01'),
);
expect(inboundRef.route_validity_end).toEqual(
DateTime.fromISO('2051-01-01'),
);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DbConnection, closeDbConnection, createDbConnection } from '@util/db';
import { queryTable } from '@util/setup';
import { hslTimetablesDbSchema } from 'hsl/timetablesdb/datasets/schema';
import { insertDatasetFromJson } from './data-insert';
import * as testDatasetJson from './example.json';
import testDatasetJson from './example.json';
import {
HslTimetablesDatasetOutput,
HslVehicleScheduleFrameOutput,
Expand Down
32 changes: 18 additions & 14 deletions test/hasura/timetables-data-inserter/hsl/json-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,25 @@ const hslVehicleScheduleFramesSchema = genericVehicleScheduleFrameSchema.extend(
},
);

const substituteOperatingDayByLineTypeSchema = z.object({
substitute_operating_day_by_line_type_id: z.string().uuid().optional(),
type_of_line: z.nativeEnum(TypeOfLine).optional(),
superseded_date: dateSchema.optional(),
substitute_day_of_week: z.nativeEnum(DayOfWeek).nullable().optional(),
begin_time: durationSchema.nullable().optional(),
end_time: durationSchema.nullable().optional(),
timezone: timezoneSchema.optional(),
});
const substituteOperatingDayByLineTypeSchema = z
.object({
substitute_operating_day_by_line_type_id: z.string().uuid().optional(),
type_of_line: z.nativeEnum(TypeOfLine).optional(),
superseded_date: dateSchema.optional(),
substitute_day_of_week: z.nativeEnum(DayOfWeek).nullable().optional(),
begin_time: durationSchema.nullable().optional(),
end_time: durationSchema.nullable().optional(),
timezone: timezoneSchema.optional(),
})
.strict();

const substituteOperatingPeriodSchema = z.object({
_substitute_operating_day_by_line_types: z
.record(substituteOperatingDayByLineTypeSchema)
.optional(),
});
const substituteOperatingPeriodSchema = z
.object({
_substitute_operating_day_by_line_types: z
.record(substituteOperatingDayByLineTypeSchema)
.optional(),
})
.strict();

export const hslTimetablesJsonSchema = genericTimetablesJsonSchema.extend({
_vehicle_schedule_frames: z.record(hslVehicleScheduleFramesSchema).optional(),
Expand Down
Loading