From 1fb6d64f8abebc9d51a7969a77a1f3b3a34bc5a8 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Wed, 18 Apr 2018 23:56:11 +0200 Subject: [PATCH] VBB tests: use new validators --- test/vbb.js | 395 +++++++++++++++++++++------------------------------- 1 file changed, 160 insertions(+), 235 deletions(-) diff --git a/test/vbb.js b/test/vbb.js index af1ec977..7995a066 100644 --- a/test/vbb.js +++ b/test/vbb.js @@ -1,52 +1,99 @@ 'use strict' -const a = require('assert') -const isRoughlyEqual = require('is-roughly-equal') const stations = require('vbb-stations-autocomplete') +const a = require('assert') +const shorten = require('vbb-short-station-name') const tapePromise = require('tape-promise').default const tape = require('tape') -const shorten = require('vbb-short-station-name') +const isRoughlyEqual = require('is-roughly-equal') +const {createWhen} = require('./lib/util') const co = require('./lib/co') const createClient = require('..') const vbbProfile = require('../p/vbb') -const allProducts = require('../p/vbb/products') +const products = require('../p/vbb/products') const { - assertValidStation: _assertValidStation, - assertValidPoi, - assertValidAddress, - assertValidLocation, - assertValidLine: _assertValidLine, - assertValidStopover, - hour, createWhen, - assertValidWhen, - assertValidTicket -} = require('./lib/util') + station: createValidateStation, + line: createValidateLine, + journeyLeg: createValidateJourneyLeg, + departure: createValidateDeparture, + movement: _validateMovement +} = require('./lib/validators') +const createValidate = require('./lib/validate-fptf-with') + +const isObj = o => o !== null && 'object' === typeof o && !Array.isArray(o) const when = createWhen('Europe/Berlin', 'de-DE') -// todo: DRY with other tests, move into lib -const assertValidStation = (t, s, coordsOptional = false) => { - _assertValidStation(t, s, coordsOptional) - t.equal(s.name, shorten(s.name)) - t.ok(s.products) - for (let product of allProducts) { - product = product.id - const msg = `station.products[${product}] must be a boolean` - t.equal(typeof s.products[product], 'boolean', msg) +const cfg = { + when, + stationCoordsOptional: false, + products +} + +const validateDirection = (dir, name) => { + a.strictEqual(typeof dir, 'string', name + ' must be a string') + a.ok(dir, name + ' must not be empty') + if (!stations(dir, true, false)[0]) { + console.error(name + `: station "${dir}" is unknown`) } } -const assertValidLine = (t, l) => { - _assertValidLine(t, l) - if (l.symbol !== null) t.equal(typeof l.symbol, 'string') - if (l.nr !== null) t.equal(typeof l.nr, 'number') - if (l.metro !== null) t.equal(typeof l.metro, 'boolean') - if (l.express !== null) t.equal(typeof l.express, 'boolean') - if (l.night !== null) t.equal(typeof l.night, 'boolean') +// todo: coordsOptional = false +const _validateStation = createValidateStation(cfg) +const validateStation = (validate, s, name) => { + _validateStation(validate, s, name) + a.equal(s.name, shorten(s.name), name + '.name must be shortened') } -const findStation = (query) => stations(query, true, false)[0] +const _validateLine = createValidateLine(cfg) +const validateLine = (validate, l, name) => { + _validateLine(validate, l, name) + if (l.symbol !== null) { + a.strictEqual(typeof l.symbol, 'string', name + '.symbol must be a string') + a.ok(l.symbol, name + '.symbol must not be empty') + } + if (l.nr !== null) { + a.strictEqual(typeof l.nr, 'number', name + '.nr must be a string') + a.ok(l.nr, name + '.nr must not be empty') + } + if (l.metro !== null) { + a.strictEqual(typeof l.metro, 'boolean', name + '.metro must be a boolean') + } + if (l.express !== null) { + a.strictEqual(typeof l.express, 'boolean', name + '.express must be a boolean') + } + if (l.night !== null) { + a.strictEqual(typeof l.night, 'boolean', name + '.night must be a boolean') + } +} + +const _validateJourneyLeg = createValidateJourneyLeg(cfg) +const validateJourneyLeg = (validate, l, name) => { + _validateJourneyLeg(validate, l, name) + if (l.mode !== 'walking') { + validateDirection(l.direction, name + '.direction') + } +} + +const _validateDeparture = createValidateDeparture(cfg) +const validateDeparture = (validate, dep, name) => { + _validateDeparture(validate, dep, name) + validateDirection(dep.direction, name + '.direction') +} + +const validateMovement = (validate, m, name) => { + _validateMovement(validate, m, name) + validateDirection(m.direction, name + '.direction') +} + +const validate = createValidate(cfg, { + station: validateStation, + line: validateLine, + journeyLeg: validateJourneyLeg, + departure: validateDeparture, + movement: validateMovement +}) const test = tapePromise(tape) const client = createClient(vbbProfile) @@ -54,49 +101,29 @@ const client = createClient(vbbProfile) const amrumerStr = '900000009101' const spichernstr = '900000042101' const bismarckstr = '900000024201' +const atze = '900980720' +const westhafen = '900000001201' +const wedding = '900000009104' +const württembergallee = '900000026153' +const berlinerStr = '900000044201' +const landhausstr = '900000043252' test('journeys – station to station', co(function* (t) { const journeys = yield client.journeys(spichernstr, amrumerStr, { results: 3, when, passedStations: true }) - t.ok(Array.isArray(journeys)) + validate(t, journeys, 'journeys', 'journeys') t.strictEqual(journeys.length, 3) + for (let i = 0; i < journeys.length; i++) { + const j = journeys[i] - for (let journey of journeys) { - t.equal(journey.type, 'journey') - - t.ok(Array.isArray(journey.legs)) - t.strictEqual(journey.legs.length, 1) - const leg = journey.legs[0] // todo: all legs - - t.equal(typeof leg.id, 'string') - t.ok(leg.id) - assertValidStation(t, leg.origin) - t.ok(leg.origin.name.indexOf('(Berlin)') === -1) - t.strictEqual(leg.origin.id, spichernstr) - assertValidWhen(t, leg.departure, when) - - assertValidStation(t, leg.destination) - t.strictEqual(leg.destination.id, amrumerStr) - assertValidWhen(t, leg.arrival, when) - - assertValidLine(t, leg.line) - if (!findStation(leg.direction)) { - const err = new Error('unknown direction: ' + leg.direction) - err.stack = err.stack.split('\n').slice(0, 2).join('\n') - console.error(err) - } - t.ok(leg.direction.indexOf('(Berlin)') === -1) - - t.ok(Array.isArray(leg.passed)) - for (let passed of leg.passed) assertValidStopover(t, passed) + const firstLeg = j.legs[0] + const lastLeg = j.legs[j.legs.length - 1] + t.strictEqual(firstLeg.origin.id, spichernstr) + t.strictEqual(lastLeg.destination.id, amrumerStr) // todo: find a journey where there ticket info is always available - if (journey.tickets) { - t.ok(Array.isArray(journey.tickets)) - for (let ticket of journey.tickets) assertValidTicket(t, ticket) - } } t.end() })) @@ -115,22 +142,28 @@ test('journeys – only subway', co(function* (t) { } }) - t.ok(Array.isArray(journeys)) + validate(t, journeys, 'journeys', 'journeys') t.ok(journeys.length > 1) + for (let i = 0; i < journeys.length; i++) { + const journey = journeys[i] + for (let j = 0; j < journey.legs.length; j++) { + const leg = journey.legs[j] - for (let journey of journeys) { - for (let leg of journey.legs) { + const name = `journeys[${i}].legs[${i}].line` if (leg.line) { - assertValidLine(t, leg.line) - t.equal(leg.line.mode, 'train') - t.equal(leg.line.product, 'subway') + t.equal(leg.line.mode, 'train', name + '.mode is invalid') + t.equal(leg.line.product, 'subway', name + '.product is invalid') } + t.ok(journey.legs.some(l => l.line), name + '.legs has no subway leg') } } + t.end() })) -test('journeys – fails with no product', co(function* (t) { +test('journeys – fails with no product', (t) => { + // todo: make this test work + // t.plan(1) try { client.journeys(spichernstr, bismarckstr, { when, @@ -148,15 +181,16 @@ test('journeys – fails with no product', co(function* (t) { .catch(() => {}) } catch (err) { t.ok(err, 'error thrown') - t.end() } -})) + t.end() +}) test('earlier/later journeys', co(function* (t) { const model = yield client.journeys(spichernstr, bismarckstr, { results: 3, when }) + // todo: move to journeys validator? t.equal(typeof model.earlierRef, 'string') t.ok(model.earlierRef) t.equal(typeof model.laterRef, 'string') @@ -167,11 +201,15 @@ test('earlier/later journeys', co(function* (t) { client.journeys(spichernstr, bismarckstr, { when, earlierThan: model.earlierRef }) + // silence rejections, we're only interested in exceptions + .catch(() => {}) }) t.throws(() => { client.journeys(spichernstr, bismarckstr, { when, laterThan: model.laterRef }) + // silence rejections, we're only interested in exceptions + .catch(() => {}) }) let earliestDep = Infinity, latestDep = -Infinity @@ -212,72 +250,52 @@ test('journey leg details', co(function* (t) { t.ok(p.line.name, 'precondition failed') const leg = yield client.journeyLeg(p.id, p.line.name, {when}) - t.equal(typeof leg.id, 'string') - t.ok(leg.id) - - assertValidLine(t, leg.line) - - t.equal(typeof leg.direction, 'string') - t.ok(leg.direction) - - t.ok(Array.isArray(leg.passed)) - for (let passed of leg.passed) assertValidStopover(t, passed) - + validate(t, leg, 'journeyLeg', 'leg') t.end() })) - - test('journeys – station to address', co(function* (t) { + const latitude = 52.541797 + const longitude = 13.350042 const journeys = yield client.journeys(spichernstr, { type: 'location', address: 'Torfstr. 17, Berlin', - latitude: 52.541797, longitude: 13.350042 + latitude, longitude }, {results: 1, when}) - t.ok(Array.isArray(journeys)) - t.strictEqual(journeys.length, 1) - const journey = journeys[0] - const leg = journey.legs[journey.legs.length - 1] + validate(t, journeys, 'journeys', 'journeys') - assertValidStation(t, leg.origin) - assertValidWhen(t, leg.departure, when) + const i = journeys[0].legs.length - 1 + const d = journeys[0].legs[i].destination + const name = `journeys[0].legs[${i}].destination` - const dest = leg.destination - assertValidAddress(t, dest) - t.strictEqual(dest.address, '13353 Berlin-Wedding, Torfstr. 17') - t.ok(isRoughlyEqual(.0001, dest.latitude, 52.541797)) - t.ok(isRoughlyEqual(.0001, dest.longitude, 13.350042)) - assertValidWhen(t, leg.arrival, when) + t.strictEqual(d.address, '13353 Berlin-Wedding, Torfstr. 17', name + '.address is invalid') + t.ok(isRoughlyEqual(.0001, d.latitude, latitude), name + '.latitude is invalid') + t.ok(isRoughlyEqual(.0001, d.longitude, longitude), name + '.longitude is invalid') t.end() })) - - test('journeys – station to POI', co(function* (t) { + const latitude = 52.543333 + const longitude = 13.351686 const journeys = yield client.journeys(spichernstr, { type: 'location', - id: '900980720', + id: atze, name: 'Berlin, Atze Musiktheater für Kinder', - latitude: 52.543333, longitude: 13.351686 + latitude, longitude }, {results: 1, when}) - t.ok(Array.isArray(journeys)) - t.strictEqual(journeys.length, 1) - const journey = journeys[0] - const leg = journey.legs[journey.legs.length - 1] + validate(t, journeys, 'journeys', 'journeys') - assertValidStation(t, leg.origin) - assertValidWhen(t, leg.departure, when) + const i = journeys[0].legs.length - 1 + const d = journeys[0].legs[i].destination + const name = `journeys[0].legs[${i}].destination` - const dest = leg.destination - assertValidPoi(t, dest) - t.strictEqual(dest.id, '900980720') - t.strictEqual(dest.name, 'Berlin, Atze Musiktheater für Kinder') - t.ok(isRoughlyEqual(.0001, dest.latitude, 52.543333)) - t.ok(isRoughlyEqual(.0001, dest.longitude, 13.351686)) - assertValidWhen(t, leg.arrival, when) + t.strictEqual(d.id, atze, name + '.id is invalid') + t.strictEqual(d.name, 'Berlin, Atze Musiktheater für Kinder', name + '.name is invalid') + t.ok(isRoughlyEqual(.0001, d.latitude, latitude), name + '.latitude is invalid') + t.ok(isRoughlyEqual(.0001, d.longitude, longitude), name + '.longitude is invalid') t.end() })) @@ -285,41 +303,21 @@ test('journeys – station to POI', co(function* (t) { test('journeys: via works – with detour', co(function* (t) { // Going from Westhafen to Wedding via Württembergalle without detour // is currently impossible. We check if the routing engine computes a detour. - const westhafen = '900000001201' - const wedding = '900000009104' - const württembergallee = '900000026153' - const [journey] = yield client.journeys(westhafen, wedding, { + const journeys = yield client.journeys(westhafen, wedding, { via: württembergallee, results: 1, when, passedStations: true }) - t.ok(journey) + validate(t, journeys, 'journeys', 'journeys') - const l = journey.legs.some(l => l.passed && l.passed.some(p => p.station.id === württembergallee)) - t.ok(l, 'Württembergalle is not being passed') - - t.end() -})) - -test('journeys: via works – without detour', co(function* (t) { - // When going from Ruhleben to Zoo via Kastanienallee, there is *no need* - // to change trains / no need for a "detour". - const ruhleben = '900000025202' - const zoo = '900000023201' - const kastanienallee = '900000020152' - const [journey] = yield client.journeys(ruhleben, zoo, { - via: kastanienallee, - results: 1, - when, - passedStations: true + const leg = journeys[0].legs.some((leg) => { + return leg.passed && leg.passed.some((passed) => { + return passed.station.id === württembergallee + }) }) - - t.ok(journey) - - const l = journey.legs.some(l => l.passed && l.passed.some(p => p.station.id === kastanienallee)) - t.ok(l, 'Kastanienallee is not being passed') + t.ok(leg, 'Württembergalle is not being passed') t.end() })) @@ -327,29 +325,21 @@ test('journeys: via works – without detour', co(function* (t) { test('departures', co(function* (t) { const deps = yield client.departures(spichernstr, {duration: 5, when}) - t.ok(Array.isArray(deps)) - t.deepEqual(deps, deps.sort((a, b) => t.when > b.when)) - for (let dep of deps) { - t.equal(typeof dep.journeyId, 'string') - t.ok(dep.journeyId) + validate(t, deps, 'departures', 'departures') + for (let i = 0; i < deps.length; i++) { + const dep = deps[i] + const name = `deps[${i}]` - t.equal(dep.station.name, 'U Spichernstr.') - assertValidStation(t, dep.station) - t.strictEqual(dep.station.id, spichernstr) - - assertValidWhen(t, dep.when, when) - if (!findStation(dep.direction)) { - const err = new Error('unknown direction: ' + dep.direction) - err.stack = err.stack.split('\n').slice(0, 2).join('\n') - console.error(err) - } - assertValidLine(t, dep.line) + t.equal(dep.station.name, 'U Spichernstr.', name + '.station.name is invalid') + t.equal(dep.station.id, spichernstr, name + '.station.id is invalid') } + t.deepEqual(deps, deps.sort((a, b) => t.when > b.when)) + t.end() })) test('departures with station object', co(function* (t) { - yield client.departures({ + const deps = yield client.departures({ type: 'station', id: spichernstr, name: 'U Spichernstr', @@ -360,7 +350,7 @@ test('departures with station object', co(function* (t) { } }, {when}) - t.ok('did not fail') + validate(t, deps, 'departures', 'departures') t.end() })) @@ -368,12 +358,9 @@ test('departures at 7-digit station', co(function* (t) { const eisenach = '8010097' // see derhuerst/vbb-hafas#22 yield client.departures(eisenach, {when}) t.pass('did not fail') - t.end() })) - - test('nearby', co(function* (t) { // Berliner Str./Bundesallee const nearby = yield client.nearby({ @@ -382,18 +369,14 @@ test('nearby', co(function* (t) { longitude: 13.3310411 }, {distance: 200}) - t.ok(Array.isArray(nearby)) - for (let n of nearby) { - if (n.type === 'station') assertValidStation(t, n) - else assertValidLocation(t, n, false) - } + validate(t, nearby, 'locations', 'nearby') - t.equal(nearby[0].id, '900000044201') + t.equal(nearby[0].id, berlinerStr) t.equal(nearby[0].name, 'U Berliner Str.') t.ok(nearby[0].distance > 0) t.ok(nearby[0].distance < 100) - t.equal(nearby[1].id, '900000043252') + t.equal(nearby[1].id, landhausstr) t.equal(nearby[1].name, 'Landhausstr.') t.ok(nearby[1].distance > 100) t.ok(nearby[1].distance < 200) @@ -401,18 +384,12 @@ test('nearby', co(function* (t) { t.end() })) - - test('locations', co(function* (t) { const locations = yield client.locations('Alexanderplatz', {results: 20}) - t.ok(Array.isArray(locations)) - t.ok(locations.length > 0) + validate(t, locations, 'locations', 'locations') t.ok(locations.length <= 20) - for (let l of locations) { - if (l.type === 'station') assertValidStation(t, l) - else assertValidLocation(t, l) - } + t.ok(locations.find(s => s.type === 'station')) t.ok(locations.find(s => s.id && s.name)) // POIs t.ok(locations.find(s => !s.name && s.address)) // addresses @@ -421,21 +398,14 @@ test('locations', co(function* (t) { })) test('location', co(function* (t) { - const loc = yield client.location(spichernstr) + const s = yield client.location(spichernstr) - assertValidStation(t, loc) - t.equal(loc.id, spichernstr) - - t.ok(Array.isArray(loc.lines)) - if (Array.isArray(loc.lines)) { - for (let line of loc.lines) assertValidLine(t, line) - } + validate(t, s, 'station', 'station') + t.equal(s.id, spichernstr) t.end() })) - - test('radar', co(function* (t) { const vehicles = yield client.radar({ north: 52.52411, @@ -446,51 +416,6 @@ test('radar', co(function* (t) { duration: 5 * 60, when }) - t.ok(Array.isArray(vehicles)) - t.ok(vehicles.length > 0) - for (let v of vehicles) { - - if (!findStation(v.direction)) { - const err = new Error('unknown direction: ' + v.direction) - err.stack = err.stack.split('\n').slice(0, 2).join('\n') - console.error(err) - } - assertValidLine(t, v.line) - - t.equal(typeof v.location.latitude, 'number') - t.ok(v.location.latitude <= 55, 'vehicle is too far away') - t.ok(v.location.latitude >= 45, 'vehicle is too far away') - t.equal(typeof v.location.longitude, 'number') - t.ok(v.location.longitude >= 9, 'vehicle is too far away') - t.ok(v.location.longitude <= 15, 'vehicle is too far away') - - t.ok(Array.isArray(v.nextStops)) - for (let st of v.nextStops) { - assertValidStopover(t, st, true) - t.strictEqual(st.station.name.indexOf('(Berlin)'), -1) - - if (st.arrival) { - t.equal(typeof st.arrival, 'string') - const arr = +new Date(st.arrival) - // note that this can be an ICE train - t.ok(isRoughlyEqual(14 * hour, +when, arr)) - } - if (st.departure) { - t.equal(typeof st.departure, 'string') - const dep = +new Date(st.departure) - // note that this can be an ICE train - t.ok(isRoughlyEqual(14 * hour, +when, dep)) - } - } - - t.ok(Array.isArray(v.frames)) - for (let f of v.frames) { - assertValidStation(t, f.origin, true) - t.strictEqual(f.origin.name.indexOf('(Berlin)'), -1) - assertValidStation(t, f.destination, true) - t.strictEqual(f.destination.name.indexOf('(Berlin)'), -1) - t.equal(typeof f.t, 'number') - } - } + validate(t, vehicles, 'movements', 'vehicles') t.end() }))