Skip to content

Commit c6e6e3c

Browse files
committed
test(backend): test routingSvcs error conditions
1 parent aa556e4 commit c6e6e3c

File tree

2 files changed

+52
-5
lines changed

2 files changed

+52
-5
lines changed

backend/services/routing.test.ts

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ import { describe, expect, test } from "@jest/globals";
88
import { CongestionSvc } from "./congestion";
99
import { initDB } from "../clients/db";
1010
import { RoutingSvcFactory } from "../factories/routing";
11+
import { NotFoundError, ValidationError } from "../error";
1112

1213
describe("RoutingSvc", () => {
1314
const congestionSvc = new CongestionSvc(initDB());
1415
// test routing with and without congestion routing
15-
const routingSvc = RoutingSvcFactory.create(congestionSvc);
16+
const routingSvcs = [
17+
RoutingSvcFactory.create(congestionSvc),
18+
// RoutingSvcFactory.create(),
19+
];
1620

17-
test("route() returns routes", async () => {
21+
test.each(routingSvcs)("route() returns routes", async (routingSvc) => {
1822
const routes = await routingSvc.route(
1923
// NTU nanyang circle
2024
{
@@ -30,9 +34,48 @@ describe("RoutingSvc", () => {
3034
expect(routes.length).toBeGreaterThan(0);
3135
});
3236

37+
test.each(routingSvcs)(
38+
"route() throws NotFoundError on no route",
39+
async (routingSvc) => {
40+
expect(
41+
routingSvc.route(
42+
// NTU nanyang circle
43+
{
44+
longitude: 103.6849923,
45+
latitude: 1.3437504,
46+
},
47+
{
48+
longitude: 0,
49+
latitude: 0,
50+
},
51+
),
52+
).rejects.toThrowError(NotFoundError);
53+
},
54+
);
55+
56+
test.each(routingSvcs)(
57+
"route() throws ValidationError on bad coordinate",
58+
async (routingSvc) => {
59+
expect(
60+
routingSvc.route(
61+
// NTU nanyang circle
62+
{
63+
longitude: 103.6849923,
64+
latitude: 1.3437504,
65+
},
66+
// changi airport (invalid latitude)
67+
{
68+
longitude: 103.98847034565972,
69+
latitude: -91.0,
70+
},
71+
),
72+
).rejects.toThrowError(ValidationError);
73+
},
74+
);
75+
3376
test("geolookup() returns correct GeoLocation for a given postcode", async () => {
3477
const postcode = "639798"; // Example postcode for NTU area
35-
const location = await routingSvc.geolookup(postcode);
78+
const location = await routingSvcs[0].geolookup(postcode);
3679

3780
expect(location).toBeDefined();
3881
expect(location.latitude).toBeCloseTo(1.3437504, 2);
@@ -42,7 +85,7 @@ describe("RoutingSvc", () => {
4285
test("geolookup() throws an error for invalid postcode", async () => {
4386
const invalidPostcode = "000000"; // Example of an invalid postcode
4487

45-
await expect(routingSvc.geolookup(invalidPostcode)).rejects.toThrow(
88+
await expect(routingSvcs[0].geolookup(invalidPostcode)).rejects.toThrow(
4689
`No location found for postcode: ${invalidPostcode}`,
4790
);
4891
});

backend/services/routing.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as path from "path";
99
import csv from "csv-parser";
1010
import { components, paths } from "../api";
1111
import { CongestionSvc } from "./congestion";
12-
import { NotFoundError } from "../error";
12+
import { NotFoundError, ValidationError } from "../error";
1313

1414
// routing endpoint url constants
1515
export const CONGEST_ROUTING_API =
@@ -131,6 +131,10 @@ export class RoutingSvc {
131131
// no route found
132132
throw new NotFoundError("No route found between given locations.");
133133
}
134+
if (r["code"] === "InvalidValue") {
135+
// no route found
136+
throw new ValidationError("Invalid routing parameters given.");
137+
}
134138
throw new Error(
135139
`OSRM ${url.pathname} request failed with error: ${r["code"]}: ${r["message"]}`,
136140
);

0 commit comments

Comments
 (0)