Add test for calculateLength()

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-08-05 22:59:56 +01:00
parent ca6f245a84
commit de6f735edd
3 changed files with 69 additions and 6 deletions

View File

@ -104,15 +104,15 @@ function transformLocation(input: any): ServiceLocation[] {
return output
}
function calculateLength(input: TrainServices): number | undefined {
export function calculateLength(input: any): number | undefined {
let length: number;
if (input?.length) {
length = input.length
return length
return Number(length)
}
if (input?.formation?.coaches?.coach) {
length = input.formation.coaches.coach.length
return length
return Number(length)
}
return undefined
}

View File

@ -1,7 +1,12 @@
import { transform } from "../../../../src/utils/translators/ldb/staffStation";
import {
transform,
calculateLength
} from "../../../../src/utils/translators/ldb/staffStation";
import { inputs } from "./stationInputs";
import { outputs } from "./stationOutputs";
import { noLength as serviceNoLength } from "./trainServiceInputs";
import { trainServices } from "./trainServiceInputs";
describe('transform', () => {
test('Should return null for empty input', () => {
@ -10,7 +15,7 @@ describe('transform', () => {
});
for (const testNo in inputs) {
test(`Should correctly transform data ${testNo + 1}`, () => {
test(`Should correctly transform data ${testNo}`, () => {
const input = inputs[testNo]
const expectedOutput = outputs[testNo]
@ -20,4 +25,21 @@ describe('transform', () => {
}
});
// Write test for calculateLength(input: TrainServices): number | undefined
// Write test for calculateLength(input: TrainServices): number | undefined
describe('calculateLength', () => {
test('Should return ubdefined for no length', () => {
const input = serviceNoLength;
expect(calculateLength(input)).toBeUndefined();
});
for (const testNo in trainServices) {
test(`Should correctly calculate ${testNo}`, () => {
const input = trainServices[testNo]
const expectedOutput = 4
expect(calculateLength(input)).toEqual(expectedOutput);
});
}
});

View File

@ -0,0 +1,41 @@
import type { TrainServices } from "@owlboard/ts-types";
export const noLength: any = {
"rid": "202308058004480",
"uid": "P04480",
"trainid": "1A39",
"sdd": "2023-08-05",
"operator": "Great Western Railway",
"operatorCode": "GW",
"sta": "2023-08-05T21:51:00",
"eta": "2023-08-05T23:04:18",
"arrivalType": "Forecast",
"std": "2023-08-05T22:00:00",
"etd": "2023-08-05T23:05:18",
"departureType": "Forecast",
"departureSource": "Darwin",
"platform": "7",
"origin": {
"location": {
"locationName": "Penzance",
"crs": "PNZ",
"tiploc": "PENZNCE"
}
},
"destination": {
"location": {
"locationName": "London Paddington",
"crs": "PAD",
"tiploc": "PADTON"
}
},
"delayReason": "887",
"category": "XX",
"activities": "T"
}
export const trainServices: any[] = [
{"rid":"202308058005927","uid":"P05927","trainid":"2T53","sdd":"2023-08-05","operator":"Great Western Railway","operatorCode":"GW","sta":"2023-08-05T19:52:00","eta":"2023-08-05T19:52:00","arrivalType":"Forecast","std":"2023-08-05T19:56:00","etd":"2023-08-05T19:56:00","departureType":"Forecast","departureSource":"Darwin","platform":"2","formation":{"coaches":{"coach":[{"coachClass":"Standard"},{"coachClass":"Standard"},{"coachClass":"Standard"},{"coachClass":"Standard"}]}},"origin":{"location":{"locationName":"Worcester Foregate Street","crs":"WOF","tiploc":"WORCSFS"}},"destination":{"location":{"locationName":"Bristol Temple Meads","crs":"BRI","tiploc":"BRSTLTM","via":"via Gloucester"}},"category":"OO","activities":"T"},
{"rid":"202308057126314","uid":"G26314","trainid":"2V88","sdd":"2023-08-05","operator":"West Midlands Trains","operatorCode":"LM","sta":"2023-08-05T18:28:00","eta":"2023-08-05T18:28:00","arrivalType":"Forecast","std":"2023-08-05T18:33:00","etd":"2023-08-05T18:33:00","departureType":"Forecast","departureSource":"Darwin","platform":"2","formation":{"coaches":{"coach":[{"coachClass":"Standard"},{"coachClass":"Standard","toilet":"Accessible"},{"coachClass":"Standard"},{"coachClass":"Standard","toilet":"Accessible"}]}},"origin":{"location":{"locationName":"Dorridge","crs":"DDG","tiploc":"DORIDGE"}},"destination":{"location":{"locationName":"Worcester Foregate Street","crs":"WOF","tiploc":"WORCSFS"}},"category":"OO","activities":"T RM","length":"4"},
{"rid":"202308057126318","uid":"G26318","trainid":"2V96","sdd":"2023-08-05","operator":"West Midlands Trains","operatorCode":"LM","sta":"2023-08-05T19:28:00","eta":"2023-08-05T19:28:00","arrivalType":"Forecast","std":"2023-08-05T19:33:00","etd":"2023-08-05T19:33:00","departureType":"Forecast","departureSource":"Darwin","platform":"2","origin":{"location":{"locationName":"Dorridge","crs":"DDG","tiploc":"DORIDGE"}},"destination":{"location":{"locationName":"Worcester Foregate Street","crs":"WOF","tiploc":"WORCSFS"}},"category":"OO","activities":"T RM","length":"4"}
]