Skip to content

Commit

Permalink
Add DHID and DLID (#171)
Browse files Browse the repository at this point in the history
* Added DHID and DLID

* Fixed some tests due to API-Changes

* Rewrite my own test

* Added a new contributor
  • Loading branch information
justusjonas74 authored Nov 22, 2023
1 parent 23301c0 commit c525ff5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
5 changes: 5 additions & 0 deletions packages/dvbjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
{
"name": "Adwirawien",
"url": "https://github.com/Adwirawien"
},
{
"name": "Francis Doege",
"email": "[email protected]",
"url": "https://github.com/justusjonas74"
}
],
"files": [
Expand Down
2 changes: 2 additions & 0 deletions packages/dvbjs/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export interface IStop extends ILocation {
platform?: IPlatform;
arrival: Date;
departure: Date;
dhid: string;
}

export interface IStopLocation extends ILocation {
Expand All @@ -114,6 +115,7 @@ export interface INode {
line: string;
direction: string;
diva?: IDiva;
dlid?: string;
duration: number;
path: coord[];
}
Expand Down
2 changes: 2 additions & 0 deletions packages/dvbjs/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ export function parsePoiID(id: string): { id: string; type: POI_TYPE } {
function extractStop(stop: any): IStop {
return {
id: stop.DataId,
dhid: stop.DhId,
name: stop.Name.trim(),
city: stop.Place,
type: stop.Type,
Expand Down Expand Up @@ -461,6 +462,7 @@ function extractNode(node: any, mapData: any): INode {
line: node.Mot.Name ? node.Mot.Name : "",
direction: node.Mot.Direction ? node.Mot.Direction.trim() : "",
diva: parseDiva(node.Mot.Diva),
dlid: node.Mot.DlId,
duration: node.Duration || 1,
path: convertCoordinates(mapData[node.MapDataIndex]),
};
Expand Down
62 changes: 41 additions & 21 deletions packages/dvbjs/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint @typescript-eslint/no-non-null-assertion: 0 */

import axios from "axios";
import chai, { assert } from "chai";
import chai, { assert, expect } from "chai";
import chaiAsPromised from "chai-as-promised";
import * as dvb from "../src/index";
import { IRoute, IStop } from "../src/index";
Expand Down Expand Up @@ -113,7 +113,37 @@ describe("dvb.route", () => {
assert.notEqual(0, durationSum);
});
});

it("all stops should have all prperties", () => {
const stops = data.trips.flatMap((trip) => {
return trip.nodes.flatMap((node) => {
return node.stops
})
})
stops.forEach(stop => {
assert.property(stop, 'id')
assert.property(stop, 'dhid')
assert.property(stop, 'name')
assert.property(stop, 'city')
assert.property(stop, 'type')
assert.property(stop, 'platform')
assert.property(stop, 'coords')
assert.property(stop, 'arrival')
assert.property(stop, 'departure')
expect(stop.dhid).not.to.be.empty
})
})

it("all nodes except footpaths should have prperty 'dlid' ", () => {
const nodes = data.trips.flatMap((trip) => {
return trip.nodes
}).filter(node => node.mode && node.mode.name !== 'Footpath')
nodes.forEach(node => {
expect(node.dlid).to.exist.and.to.be.an('string')
})
})
});

describe('dvb.route "33000742 (Helmholtzstraße) --> via: 33000016 (Bahnhof Neustadt) --> 33000037 (Postplatz Dresden)"', () => {
let data: dvb.IRoute;

Expand All @@ -134,32 +164,22 @@ describe("dvb.route", () => {
route: IRoute,
stopId: string
): IStop[][] => {
const filteredTrips: IStop[][] = [];
route.trips.forEach((trip) => {
const stopsPerTrip: IStop[] = [];
trip.nodes.forEach((node) => {
const filteredStops = node.stops.find((stop) => {
return stop.id === stopId;
});
if (filteredStops) {
stopsPerTrip.push(filteredStops);
}
});
filteredTrips.push(stopsPerTrip);
});
return filteredTrips;
};

return route.trips.map(trip => {
return trip.nodes.flatMap(node => {
return node.stops.filter(stop => stop.id == stopId)
})
})
}
getStopsFromTripByID(data, "33000016").forEach((filteredTripByID) => {
assert.isNotEmpty(filteredTripByID);
});
assert.isNotEmpty(data);
});
});

describe('dvb.route "0 -> 0"', () => {
describe('dvb.route "33000016 -> 33000016"', () => {
it("should reject too close routes", () =>
assert.isRejected(dvb.route("0", "0"), "origin too close to destination"));
assert.isRejected(dvb.route("33000016", "33000016"), "origin too close to destination"));
});
});

Expand All @@ -181,7 +201,7 @@ describe("dvb.findStop", () => {

it("should find the correct exact stop", () =>
dvb.findStop("Postplatz (Am Zwingerteich)").then((data) => {
assert.strictEqual("Postplatz (Am Zwingerteich)", data[0].name);
assert.strictEqual("Postplatz (Am Zwingert.)", data[0].name);
}));
});

Expand Down Expand Up @@ -394,7 +414,7 @@ describe("dvb.findAddress", () => {
it("should resolve into an object with city, address and coords properties", () =>
dvb.findAddress(lng, lat).then((address) => {
assert.isDefined(address);
assert.strictEqual(address!.name, "Nöthnitzer Straße 44");
assert.strictEqual(address!.name, "Nöthnitzer Straße 44a");
assert.strictEqual(address!.city, "Dresden");
assert.strictEqual(address!.type, dvb.POI_TYPE.Coords);
assert.approximately(address!.coords[0], lng, 0.001);
Expand Down

0 comments on commit c525ff5

Please sign in to comment.