Skip to content

Commit

Permalink
Add timing point constraints and tests
Browse files Browse the repository at this point in the history
A stop can't have loading time allowed if it is not a regulated timing point and
a stop can't be a regulated timing point if it is not set as timing point.

Resolves HSLdevcom/jore4#1369
  • Loading branch information
HenrikHartiala committed Aug 21, 2023
1 parent 80db149 commit 6546af8
Show file tree
Hide file tree
Showing 4 changed files with 345 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE journey_pattern.scheduled_stop_point_in_journey_pattern
DROP CONSTRAINT ck_is_used_as_timing_point_state;

ALTER TABLE journey_pattern.scheduled_stop_point_in_journey_pattern
DROP CONSTRAINT ck_is_regulated_timing_point_state;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE journey_pattern.scheduled_stop_point_in_journey_pattern
ADD CONSTRAINT ck_is_used_as_timing_point_state CHECK(NOT (is_used_as_timing_point = false AND is_regulated_timing_point = true));

ALTER TABLE journey_pattern.scheduled_stop_point_in_journey_pattern
ADD CONSTRAINT ck_is_regulated_timing_point_state CHECK(NOT (is_regulated_timing_point = false AND is_loading_time_allowed = true));
2 changes: 2 additions & 0 deletions migrations/routesdb-dump.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4250,6 +4250,8 @@ CREATE TABLE journey_pattern.scheduled_stop_point_in_journey_pattern (
scheduled_stop_point_label text NOT NULL,
is_loading_time_allowed boolean DEFAULT false NOT NULL,
is_regulated_timing_point boolean DEFAULT false NOT NULL,
CONSTRAINT ck_is_regulated_timing_point_state CHECK ((NOT ((is_regulated_timing_point = false) AND (is_loading_time_allowed = true)))),
CONSTRAINT ck_is_used_as_timing_point_state CHECK ((NOT ((is_used_as_timing_point = false) AND (is_regulated_timing_point = true)))),
CONSTRAINT ck_is_via_point_state CHECK ((((is_via_point = false) AND (via_point_name_i18n IS NULL) AND (via_point_short_name_i18n IS NULL)) OR ((is_via_point = true) AND (via_point_name_i18n IS NOT NULL) AND (via_point_short_name_i18n IS NOT NULL))))
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,333 @@
import * as config from '@config';
import * as dataset from '@util/dataset';
import { closeDbConnection, createDbConnection, DbConnection } from '@util/db';
import { expectErrorResponse, expectNoErrorResponse } from '@util/response';
import { getPropNameArray, setupDb } from '@util/setup';
import { scheduledStopPoints } from 'generic/networkdb/datasets/defaultSetup';
import {
journeyPatterns,
routesAndJourneyPatternsTableData,
scheduledStopPointInJourneyPattern,
} from 'generic/networkdb/datasets/routesAndJourneyPatterns';
import {
ScheduledStopPointInJourneyPattern,
scheduledStopPointInJourneyPatternProps,
} from 'generic/networkdb/datasets/types';
import * as rp from 'request-promise';

const baseScheduledStopPoint: Partial<ScheduledStopPointInJourneyPattern> = {
scheduled_stop_point_sequence: 0,
journey_pattern_id: journeyPatterns[0].journey_pattern_id,
scheduled_stop_point_label: scheduledStopPoints[0].label,
is_used_as_timing_point: false,
is_regulated_timing_point: false,
is_loading_time_allowed: false,
};

const buildInsertMutation = (
toBeInserted: Partial<ScheduledStopPointInJourneyPattern>,
) => `
mutation {
insert_journey_pattern_scheduled_stop_point_in_journey_pattern(objects: ${dataset.toGraphQlObject(
{
...toBeInserted,
},
)}) {
returning {
${getPropNameArray(scheduledStopPointInJourneyPatternProps).join(',')}
}
}
}
`;

const buildUpdateMutation = (
toBeUpdated: Partial<ScheduledStopPointInJourneyPattern>,
) => `
mutation {
update_journey_pattern_scheduled_stop_point_in_journey_pattern_by_pk(
pk_columns: {
journey_pattern_id: "${
scheduledStopPointInJourneyPattern[0].journey_pattern_id
}",
scheduled_stop_point_sequence: "${
scheduledStopPointInJourneyPattern[0].scheduled_stop_point_sequence
}"
},
_set: ${dataset.toGraphQlObject(toBeUpdated)}
) {
${getPropNameArray(scheduledStopPointInJourneyPatternProps).join(',')}
}
}
`;

describe('Timing point constraints on scheduled_stop_point_on_journey_pattern', () => {
let dbConnection: DbConnection;

beforeAll(() => {
dbConnection = createDbConnection(config.networkDbConfig);
});

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

beforeEach(() => setupDb(dbConnection, routesAndJourneyPatternsTableData));

describe('with conflicts', () => {
describe('loading time allowed but is not regulated timing point', () => {
it('should not insert', async () => {
const response = await rp.post({
...config.hasuraRequestTemplate,
body: {
query: buildInsertMutation({
...baseScheduledStopPoint,
is_loading_time_allowed: true,
}),
},
});

expectErrorResponse('Check constraint violation.')(response);
expectErrorResponse('ck_is_regulated_timing_point_state')(response);
});

it('should not update', async () => {
const toBeUpdated: Partial<ScheduledStopPointInJourneyPattern> = {
is_used_as_timing_point: false,
is_regulated_timing_point: false,
is_loading_time_allowed: true,
};

const response = await rp.post({
...config.hasuraRequestTemplate,
body: { query: buildUpdateMutation(toBeUpdated) },
});

expectErrorResponse('Check constraint violation.')(response);
expectErrorResponse('ck_is_regulated_timing_point_state')(response);
});
});

describe('used as timing point and loading time is allowed but not set as regulated timing point', () => {
it('should not insert', async () => {
const response = await rp.post({
...config.hasuraRequestTemplate,
body: {
query: buildInsertMutation({
...baseScheduledStopPoint,
is_used_as_timing_point: true,
is_loading_time_allowed: true,
}),
},
});

expectErrorResponse('Check constraint violation.')(response);
expectErrorResponse('ck_is_regulated_timing_point_state')(response);
});

it('should not update', async () => {
const toBeUpdated: Partial<ScheduledStopPointInJourneyPattern> = {
is_used_as_timing_point: true,
is_regulated_timing_point: false,
is_loading_time_allowed: true,
};

const response = await rp.post({
...config.hasuraRequestTemplate,
body: { query: buildUpdateMutation(toBeUpdated) },
});

expectErrorResponse('Check constraint violation.')(response);
expectErrorResponse('ck_is_regulated_timing_point_state')(response);
});
});

describe('used as regulated timing point but not set as timing point', () => {
it('should not insert', async () => {
const response = await rp.post({
...config.hasuraRequestTemplate,
body: {
query: buildInsertMutation({
...baseScheduledStopPoint,
is_regulated_timing_point: true,
}),
},
});

expectErrorResponse('Check constraint violation.')(response);
expectErrorResponse('ck_is_used_as_timing_point_state')(response);
});

it('should not update', async () => {
const toBeUpdated: Partial<ScheduledStopPointInJourneyPattern> = {
is_used_as_timing_point: false,
is_regulated_timing_point: true,
is_loading_time_allowed: false,
};

const response = await rp.post({
...config.hasuraRequestTemplate,
body: { query: buildUpdateMutation(toBeUpdated) },
});

expectErrorResponse('Check constraint violation.')(response);
expectErrorResponse('ck_is_used_as_timing_point_state')(response);
});
});

describe('used as regulated timing point, allowed loading time but is not set as timing point', () => {
it('should not insert', async () => {
const response = await rp.post({
...config.hasuraRequestTemplate,
body: {
query: buildInsertMutation({
...baseScheduledStopPoint,
is_regulated_timing_point: true,
is_loading_time_allowed: true,
}),
},
});

expectErrorResponse('Check constraint violation.')(response);
expectErrorResponse('ck_is_used_as_timing_point_state')(response);
});

it('should not update', async () => {
const toBeUpdated: Partial<ScheduledStopPointInJourneyPattern> = {
is_used_as_timing_point: false,
is_regulated_timing_point: true,
is_loading_time_allowed: true,
};

const response = await rp.post({
...config.hasuraRequestTemplate,
body: { query: buildUpdateMutation(toBeUpdated) },
});

expectErrorResponse('Check constraint violation.')(response);
expectErrorResponse('ck_is_used_as_timing_point_state')(response);
});
});
});

describe('without conflicts', () => {
describe('All timing point settings are set to false', () => {
it('should insert without conflicts', async () => {
const response = await rp.post({
...config.hasuraRequestTemplate,
body: { query: buildInsertMutation(baseScheduledStopPoint) },
});

expectNoErrorResponse(response);
});

it('should update without conflicts', async () => {
const toBeUpdated: Partial<ScheduledStopPointInJourneyPattern> = {
is_used_as_timing_point: false,
is_regulated_timing_point: false,
is_loading_time_allowed: false,
};

const response = await rp.post({
...config.hasuraRequestTemplate,
body: { query: buildUpdateMutation(toBeUpdated) },
});

expectNoErrorResponse(response);
});
});

describe('used as timing point but not as regulated timing point', () => {
it('should insert without conflicts', async () => {
const response = await rp.post({
...config.hasuraRequestTemplate,
body: {
query: buildInsertMutation({
...baseScheduledStopPoint,
is_used_as_timing_point: true,
}),
},
});

expectNoErrorResponse(response);
});

it('should update without conflicts', async () => {
const toBeUpdated: Partial<ScheduledStopPointInJourneyPattern> = {
is_used_as_timing_point: true,
is_regulated_timing_point: false,
is_loading_time_allowed: false,
};

const response = await rp.post({
...config.hasuraRequestTemplate,
body: { query: buildUpdateMutation(toBeUpdated) },
});

expectNoErrorResponse(response);
});
});

describe('used as timing point and as regulated timing point', () => {
it('should insert without conflicts', async () => {
const response = await rp.post({
...config.hasuraRequestTemplate,
body: {
query: buildInsertMutation({
...baseScheduledStopPoint,
is_used_as_timing_point: true,
is_regulated_timing_point: true,
}),
},
});

expectNoErrorResponse(response);
});

it('should update without conflicts', async () => {
const toBeUpdated: Partial<ScheduledStopPointInJourneyPattern> = {
is_used_as_timing_point: true,
is_regulated_timing_point: true,
is_loading_time_allowed: false,
};

const response = await rp.post({
...config.hasuraRequestTemplate,
body: { query: buildUpdateMutation(toBeUpdated) },
});

expectNoErrorResponse(response);
});
});

describe('used as timing point, regulated timing point and loading time is allowed', () => {
it('should insert without conflicts', async () => {
const response = await rp.post({
...config.hasuraRequestTemplate,
body: {
query: buildInsertMutation({
...baseScheduledStopPoint,
is_used_as_timing_point: true,
is_regulated_timing_point: true,
is_loading_time_allowed: true,
}),
},
});

expectNoErrorResponse(response);
});

it('should update without conflicts', async () => {
const toBeUpdated: Partial<ScheduledStopPointInJourneyPattern> = {
is_used_as_timing_point: true,
is_regulated_timing_point: true,
is_loading_time_allowed: true,
};

const response = await rp.post({
...config.hasuraRequestTemplate,
body: { query: buildUpdateMutation(toBeUpdated) },
});

expectNoErrorResponse(response);
});
});
});
});

0 comments on commit 6546af8

Please sign in to comment.