From 951c21eecc1f451be2d10c23b82ba95bc2baf3f6 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Wed, 11 Jul 2018 13:25:22 +0200 Subject: [PATCH] adapt tests to 98a051b --- test/db.js | 5 ++++- test/insa.js | 15 +++++++------- test/lib/arrivals.js | 10 ++++++--- test/lib/departures-in-direction.js | 7 ++++--- test/lib/departures.js | 10 ++++++--- test/lib/journeys-station-to-poi.js | 27 ++++++++++++++++--------- test/lib/journeys-station-to-station.js | 10 +++++---- test/lib/journeys-with-detour.js | 7 ++++--- test/lib/validators.js | 22 +++++++++++++++----- test/nahsh.js | 9 +++++---- test/oebb.js | 19 +++++++++++++---- test/vbb.js | 4 ++-- test/vbn.js | 13 ++++++------ 13 files changed, 104 insertions(+), 54 deletions(-) diff --git a/test/db.js b/test/db.js index 7b1fb76f..5448ccfd 100644 --- a/test/db.js +++ b/test/db.js @@ -317,6 +317,9 @@ test('locations named Jungfernheide', co(function* (t) { t.ok(locations.length <= 10) t.ok(locations.some((l) => { // todo: trim IDs + if (l.station) { + if (l.station.id === '008011167' || l.station.id === jungfernheide) return true + } return l.id === '008011167' || l.id === jungfernheide }), 'Jungfernheide not found') @@ -326,7 +329,7 @@ test('locations named Jungfernheide', co(function* (t) { test('station', co(function* (t) { const s = yield client.station(regensburgHbf) - validate(t, s, 'station', 'station') + validate(t, s, ['stop', 'station'], 'station') t.equal(s.id, regensburgHbf) t.end() diff --git a/test/insa.js b/test/insa.js index 0192d544..2a6fe169 100644 --- a/test/insa.js +++ b/test/insa.js @@ -235,13 +235,14 @@ test('locations named Magdeburg', co(function*(t) { validate(t, locations, 'locations', 'locations') t.ok(locations.length <= 20) - t.ok(locations.find(s => s.type === 'station')) + t.ok(locations.find(s => s.type === 'stop' || s.type === 'station')) t.ok(locations.find(s => s.id && s.name)) // POIs - t.ok(locations.some((loc) => { - return ( - loc.id === '008010224' || // todo: trim IDs - loc.id === magdeburgHbf - ) + t.ok(locations.some((l) => { + // todo: trim IDs + if (l.station) { + if (l.station.id === '008010224' || l.station.id === magdeburgHbf) return true + } + return l.id === '008010224' || l.id === magdeburgHbf })) t.end() @@ -250,7 +251,7 @@ test('locations named Magdeburg', co(function*(t) { test('station Magdeburg-Buckau', co(function* (t) { const s = yield client.station(magdeburgBuckau) - validate(t, s, 'station', 'station') + validate(t, s, ['stop', 'station'], 'station') t.equal(s.id, magdeburgBuckau) t.end() diff --git a/test/lib/arrivals.js b/test/lib/arrivals.js index 9483514a..4a21771e 100644 --- a/test/lib/arrivals.js +++ b/test/lib/arrivals.js @@ -8,10 +8,14 @@ const testArrivals = co(function* (cfg) { validate(t, arrs, 'arrivals', 'arrivals') t.ok(arrs.length > 0, 'must be >0 arrivals') for (let i = 0; i < arrs.length; i++) { - const dep = arrs[i] - const name = `arrs[${i}]` + let station = arrs[i].station + let name = `arrs[${i}].station` + if (station.station) { + station = station.station + name += '.station' + } - t.equal(dep.station.id, id, name + '.station.id is invalid') + t.equal(station.id, id, name + '.id is invalid') } // todo: move into arrivals validator diff --git a/test/lib/departures-in-direction.js b/test/lib/departures-in-direction.js index e6c0b9ff..bc82a790 100644 --- a/test/lib/departures-in-direction.js +++ b/test/lib/departures-in-direction.js @@ -28,9 +28,10 @@ const testDeparturesInDirection = co(function* (cfg) { const trip = yield fetchTrip(dep.tripId, line, { when, stopovers: true }) - t.ok(trip.stopovers.some(st => { - return st.stop && directionIds.includes(st.stop.id) - }), `trip ${dep.tripId} of ${name} has no stopover at ${directionIds}`) + t.ok(trip.stopovers.some(st => ( + st.stop.station && directionIds.includes(st.stop.station.id) || + directionIds.includes(st.stop.id) + )), `trip ${dep.tripId} of ${name} has no stopover at ${directionIds}`) } }) diff --git a/test/lib/departures.js b/test/lib/departures.js index 1da624a6..f73015fc 100644 --- a/test/lib/departures.js +++ b/test/lib/departures.js @@ -8,10 +8,14 @@ const testDepartures = co(function* (cfg) { validate(t, deps, 'departures', 'departures') t.ok(deps.length > 0, 'must be >0 departures') for (let i = 0; i < deps.length; i++) { - const dep = deps[i] - const name = `deps[${i}]` + let station = deps[i].station + let name = `deps[${i}].station` + if (station.station) { + station = station.station + name += '.station' + } - t.equal(dep.station.id, id, name + '.station.id is invalid') + t.equal(station.id, id, name + '.id is invalid') } // todo: move into deps validator diff --git a/test/lib/journeys-station-to-poi.js b/test/lib/journeys-station-to-poi.js index e996dc37..ca7fe1da 100644 --- a/test/lib/journeys-station-to-poi.js +++ b/test/lib/journeys-station-to-poi.js @@ -13,17 +13,26 @@ const testJourneysStationToPoi = co(function* (cfg) { for (let i = 0; i < journeys.length; i++) { const j = journeys[i] - const firstLeg = j.legs[0] - t.strictEqual(firstLeg.origin.id, fromId) + let o = j.legs[0].origin + let oN = `journeys[0].legs[0].destination` + if (o.station) { + o = o.station + oN += '.station' + } + t.strictEqual(o.id, fromId) - const d = j.legs[j.legs.length - 1].destination - const n = `journeys[0].legs[${i}].destination` + let d = j.legs[j.legs.length - 1].destination + let dN = `journeys[${i}].legs[${j.legs.length - 1}].destination` + if (d.station) { + d = d.station + dN += '.station' + } - t.strictEqual(d.type, 'location', n + '.type is invalid') - t.strictEqual(d.id, id, n + '.id is invalid') - t.strictEqual(d.name, name, n + '.name is invalid') - t.ok(isRoughlyEqual(.0001, d.latitude, latitude), n + '.latitude is invalid') - t.ok(isRoughlyEqual(.0001, d.longitude, longitude), n + '.longitude is invalid') + t.strictEqual(d.type, 'location', dN + '.type is invalid') + t.strictEqual(d.id, id, dN + '.id is invalid') + t.strictEqual(d.name, name, dN + '.name is invalid') + t.ok(isRoughlyEqual(.0001, d.latitude, latitude), dN + '.latitude is invalid') + t.ok(isRoughlyEqual(.0001, d.longitude, longitude), dN + '.longitude is invalid') } }) diff --git a/test/lib/journeys-station-to-station.js b/test/lib/journeys-station-to-station.js index 8d44fce4..5cc2ef58 100644 --- a/test/lib/journeys-station-to-station.js +++ b/test/lib/journeys-station-to-station.js @@ -10,10 +10,12 @@ const testJourneysStationToStation = co(function* (cfg) { for (let i = 0; i < journeys.length; i++) { const j = journeys[i] - const firstLeg = j.legs[0] - const lastLeg = j.legs[j.legs.length - 1] - t.strictEqual(firstLeg.origin.id, fromId) - t.strictEqual(lastLeg.destination.id, toId) + let origin = j.legs[0].origin + if (origin.station) origin = origin.station + let dest = j.legs[j.legs.length - 1].destination + if (dest.station) dest = dest.station + t.strictEqual(origin.id, fromId) + t.strictEqual(dest.id, toId) } }) diff --git a/test/lib/journeys-with-detour.js b/test/lib/journeys-with-detour.js index 9b725a9b..049f103c 100644 --- a/test/lib/journeys-with-detour.js +++ b/test/lib/journeys-with-detour.js @@ -11,9 +11,10 @@ const testJourneysWithDetour = co(function* (cfg) { validate(t, journeys, 'journeys', 'journeys') const leg = journeys[0].legs.some((leg) => { - return leg.stopovers && leg.stopovers.some((stopover) => { - return detourIds.includes(stopover.stop.id) - }) + return leg.stopovers && leg.stopovers.some((st) => ( + st.stop.station && detourIds.includes(st.stop.station.id) || + detourIds.includes(st.stop.id) + )) }) t.ok(leg, detourIds.join('/') + ' is not being passed') }) diff --git a/test/lib/validators.js b/test/lib/validators.js index 3760a29c..3a91d28f 100644 --- a/test/lib/validators.js +++ b/test/lib/validators.js @@ -32,6 +32,16 @@ const createValidateStation = (cfg) => { return validateStation } + +const validateStop = (val, s, name = 'stop') => { + // HAFAS doesn't always return the station of a stop. We mock it here + // to silence `validate-fptf`. + const station = Object.assign({}, s) + station.type = 'station' + s = Object.assign({station}, s) + defaultValidators.stop(val, s, name) +} + const validatePoi = (val, poi, name = 'location') => { defaultValidators.location(val, poi, name) val.ref(val, poi.id, name + '.id') @@ -46,7 +56,8 @@ const validateAddress = (val, addr, name = 'location') => { const validateLocation = (val, loc, name = 'location') => { a.ok(isObj(loc), name + ' must be an object') - if (loc.type === 'station') val.station(val, loc, name) + if (loc.type === 'stop') val.stop(val, loc, name) + else if (loc.type === 'station') val.station(val, loc, name) else if ('id' in loc) validatePoi(val, loc, name) else if (!('name' in loc) && ('address' in loc)) { validateAddress(val, loc, name) @@ -120,7 +131,7 @@ const createValidateStopover = (cfg) => { a.ok(s.formerScheduledDeparturePlatform, msg + 'not be empty') } - val.station(val, s.stop, name + '.stop') + anyOf(['stop', 'station'], val, s.stop, name + '.stop') } return validateStopover } @@ -251,7 +262,7 @@ const createValidateArrivalOrDeparture = (cfg) => { a.ok(dep.tripId, name + '.tripId must not be empty') a.strictEqual(typeof dep.trip, 'number', name + '.trip must be a number') - val.station(val, dep.station, name + '.station') + anyOf(['stop', 'station'], val, dep.station, name + '.station') assertValidWhen(dep.when, cfg.when, name) if (dep.delay !== null) { @@ -315,8 +326,8 @@ const validateMovement = (val, m, name = 'movement') => { const fName = name + `.frames[${i}]` a.ok(isObj(f), fName + ' must be an object') - anyOf(['location', 'station'], val, f.origin, fName + '.origin') - anyOf(['location', 'station'], val, f.destination, fName + '.destination') + anyOf(['location', 'stop', 'station'], val, f.origin, fName + '.origin') + anyOf(['location', 'stop', 'station'], val, f.destination, fName + '.destination') a.strictEqual(typeof f.t, 'number', fName + '.frames must be a number') } @@ -333,6 +344,7 @@ const validateMovements = (val, ms, name = 'movements') => { module.exports = { station: createValidateStation, + stop: () => validateStop, location: () => validateLocation, locations: () => validateLocations, poi: () => validatePoi, diff --git a/test/nahsh.js b/test/nahsh.js index cbdd2c84..2dc02879 100644 --- a/test/nahsh.js +++ b/test/nahsh.js @@ -164,7 +164,8 @@ test('Husum to Lübeck Hbf with stopover at Kiel Hbf', co(function* (t) { const leg = journeys[0].legs.some((leg) => { return leg.stopovers && leg.stopovers.some((stopover) => { - return stopover.stop.id === kielHbf + const s = stopover.stop + return s.station && s.station.id === kielHbf || s.id === kielHbf }) }) t.ok(leg, 'Kiel Hbf is not being passed') @@ -293,9 +294,9 @@ test('locations named Kiel', co(function* (t) { validate(t, locations, 'locations', 'locations') t.ok(locations.length <= 20) - t.ok(locations.find(s => s.type === 'station')) + t.ok(locations.find(s => s.type === 'stop' || s.type === 'station')) t.ok(locations.find(s => s.id && s.name)) // POIs - t.ok(locations.some(l => l.id === kielHbf)) + t.ok(locations.some(l => l.station && s.station.id === kielHbf || l.id === kielHbf)) t.end() })) @@ -303,7 +304,7 @@ test('locations named Kiel', co(function* (t) { test('station', co(function* (t) { const s = yield client.station(kielHbf) - validate(t, s, 'station', 'station') + validate(t, s, ['stop', 'station'], 'station') t.equal(s.id, kielHbf) t.end() diff --git a/test/oebb.js b/test/oebb.js index d2c665d3..dc1c96ae 100644 --- a/test/oebb.js +++ b/test/oebb.js @@ -11,7 +11,8 @@ const createClient = require('..') const oebbProfile = require('../p/oebb') const products = require('../p/oebb/products') const { - station: createValidateStation + station: createValidateStation, + stop: validateStop } = require('./lib/validators') const createValidate = require('./lib/validate-fptf-with') const testJourneysStationToStation = require('./lib/journeys-station-to-station') @@ -314,9 +315,15 @@ test('locations named Salzburg', co(function* (t) { validate(t, locations, 'locations', 'locations') t.ok(locations.length <= 20) - t.ok(locations.find(s => s.type === 'station')) + t.ok(locations.find(s => s.type === 'stop' || s.type === 'station')) t.ok(locations.find(s => s.id && s.name)) // POIs - t.ok(locations.some(s => s.id === '008100002' || s.id === '8100002')) + t.ok(locations.some((s) => { + // todo: trim IDs + if (s.station) { + if (s.station.id === '008100002' || s.station.id === '8100002') return true + } + return s.id === '008100002' || s.id === '8100002' + })) t.end() })) @@ -329,12 +336,16 @@ test('station', co(function* (t) { const allProducts = products.reduce((acc, p) => (acc[p.id] = true, acc), {}) const validateStation = createValidateStation(cfg) const validate = createValidate(cfg, { + stop: (validate, s, name) => { + const withFakeProducts = Object.assign({products: allProducts}, s) + validateStop(validate, withFakeProducts, name) + }, station: (validate, s, name) => { const withFakeProducts = Object.assign({products: allProducts}, s) validateStation(validate, withFakeProducts, name) } }) - validate(t, loc, 'station', 'station') + validate(t, loc, ['stop', 'station'], 'station') t.equal(loc.id, wienRenngasse) diff --git a/test/vbb.js b/test/vbb.js index 0a657acf..cbda4065 100644 --- a/test/vbb.js +++ b/test/vbb.js @@ -377,7 +377,7 @@ test('locations', co(function* (t) { validate(t, locations, 'locations', 'locations') t.ok(locations.length <= 20) - t.ok(locations.find(s => s.type === 'station')) + t.ok(locations.find(s => s.type === 'stop' || s.type === 'station')) t.ok(locations.find(s => s.id && s.name)) // POIs t.ok(locations.find(s => !s.name && s.address)) // addresses @@ -387,7 +387,7 @@ test('locations', co(function* (t) { test('station', co(function* (t) { const s = yield client.station(spichernstr) - validate(t, s, 'station', 'station') + validate(t, s, ['stop', 'station'], 'station') t.equal(s.id, spichernstr) t.end() diff --git a/test/vbn.js b/test/vbn.js index 93f6638e..f4c65e2f 100644 --- a/test/vbn.js +++ b/test/vbn.js @@ -214,13 +214,14 @@ test.skip('locations named Magdeburg', co(function*(t) { validate(t, locations, 'locations', 'locations') t.ok(locations.length <= 20) - t.ok(locations.find(s => s.type === 'station')) + t.ok(locations.find(s => s.type === 'stop' || s.type === 'station')) t.ok(locations.find(s => s.id && s.name)) // POIs t.ok(locations.some((loc) => { - return ( - loc.id === '008010224' || // todo: trim IDs - loc.id === bremenHbf - ) + // todo: trim IDs + if (l.station) { + if (l.station.id === '008010224' || l.station.id === bremenHbf) return true + } + return l.id === '008010224' || l.id === bremenHbf })) t.end() @@ -229,7 +230,7 @@ test.skip('locations named Magdeburg', co(function*(t) { test.skip('station Magdeburg-Buckau', co(function* (t) { const s = yield client.station(bremerhavenHbf) - validate(t, s, 'station', 'station') + validate(t, s, ['stop', 'station'], 'station') t.equal(s.id, bremerhavenHbf) t.end()