VBB tests: use new validators

This commit is contained in:
Jannis R 2018-04-18 23:56:11 +02:00
parent a2abf3b47d
commit 1fb6d64f8a
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5

View file

@ -1,52 +1,99 @@
'use strict' 'use strict'
const a = require('assert')
const isRoughlyEqual = require('is-roughly-equal')
const stations = require('vbb-stations-autocomplete') const stations = require('vbb-stations-autocomplete')
const a = require('assert')
const shorten = require('vbb-short-station-name')
const tapePromise = require('tape-promise').default const tapePromise = require('tape-promise').default
const tape = require('tape') 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 co = require('./lib/co')
const createClient = require('..') const createClient = require('..')
const vbbProfile = require('../p/vbb') const vbbProfile = require('../p/vbb')
const allProducts = require('../p/vbb/products') const products = require('../p/vbb/products')
const { const {
assertValidStation: _assertValidStation, station: createValidateStation,
assertValidPoi, line: createValidateLine,
assertValidAddress, journeyLeg: createValidateJourneyLeg,
assertValidLocation, departure: createValidateDeparture,
assertValidLine: _assertValidLine, movement: _validateMovement
assertValidStopover, } = require('./lib/validators')
hour, createWhen, const createValidate = require('./lib/validate-fptf-with')
assertValidWhen,
assertValidTicket const isObj = o => o !== null && 'object' === typeof o && !Array.isArray(o)
} = require('./lib/util')
const when = createWhen('Europe/Berlin', 'de-DE') const when = createWhen('Europe/Berlin', 'de-DE')
// todo: DRY with other tests, move into lib const cfg = {
const assertValidStation = (t, s, coordsOptional = false) => { when,
_assertValidStation(t, s, coordsOptional) stationCoordsOptional: false,
t.equal(s.name, shorten(s.name)) products
t.ok(s.products) }
for (let product of allProducts) {
product = product.id const validateDirection = (dir, name) => {
const msg = `station.products[${product}] must be a boolean` a.strictEqual(typeof dir, 'string', name + ' must be a string')
t.equal(typeof s.products[product], 'boolean', msg) 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) => { // todo: coordsOptional = false
_assertValidLine(t, l) const _validateStation = createValidateStation(cfg)
if (l.symbol !== null) t.equal(typeof l.symbol, 'string') const validateStation = (validate, s, name) => {
if (l.nr !== null) t.equal(typeof l.nr, 'number') _validateStation(validate, s, name)
if (l.metro !== null) t.equal(typeof l.metro, 'boolean') a.equal(s.name, shorten(s.name), name + '.name must be shortened')
if (l.express !== null) t.equal(typeof l.express, 'boolean')
if (l.night !== null) t.equal(typeof l.night, 'boolean')
} }
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 test = tapePromise(tape)
const client = createClient(vbbProfile) const client = createClient(vbbProfile)
@ -54,49 +101,29 @@ const client = createClient(vbbProfile)
const amrumerStr = '900000009101' const amrumerStr = '900000009101'
const spichernstr = '900000042101' const spichernstr = '900000042101'
const bismarckstr = '900000024201' 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) { test('journeys  station to station', co(function* (t) {
const journeys = yield client.journeys(spichernstr, amrumerStr, { const journeys = yield client.journeys(spichernstr, amrumerStr, {
results: 3, when, passedStations: true results: 3, when, passedStations: true
}) })
t.ok(Array.isArray(journeys)) validate(t, journeys, 'journeys', 'journeys')
t.strictEqual(journeys.length, 3) t.strictEqual(journeys.length, 3)
for (let i = 0; i < journeys.length; i++) {
const j = journeys[i]
for (let journey of journeys) { const firstLeg = j.legs[0]
t.equal(journey.type, 'journey') const lastLeg = j.legs[j.legs.length - 1]
t.strictEqual(firstLeg.origin.id, spichernstr)
t.ok(Array.isArray(journey.legs)) t.strictEqual(lastLeg.destination.id, amrumerStr)
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)
// todo: find a journey where there ticket info is always available // 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() 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) 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) { const name = `journeys[${i}].legs[${i}].line`
for (let leg of journey.legs) {
if (leg.line) { if (leg.line) {
assertValidLine(t, leg.line) t.equal(leg.line.mode, 'train', name + '.mode is invalid')
t.equal(leg.line.mode, 'train') t.equal(leg.line.product, 'subway', name + '.product is invalid')
t.equal(leg.line.product, 'subway')
} }
t.ok(journey.legs.some(l => l.line), name + '.legs has no subway leg')
} }
} }
t.end() 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 { try {
client.journeys(spichernstr, bismarckstr, { client.journeys(spichernstr, bismarckstr, {
when, when,
@ -148,15 +181,16 @@ test('journeys  fails with no product', co(function* (t) {
.catch(() => {}) .catch(() => {})
} catch (err) { } catch (err) {
t.ok(err, 'error thrown') t.ok(err, 'error thrown')
t.end()
} }
})) t.end()
})
test('earlier/later journeys', co(function* (t) { test('earlier/later journeys', co(function* (t) {
const model = yield client.journeys(spichernstr, bismarckstr, { const model = yield client.journeys(spichernstr, bismarckstr, {
results: 3, when results: 3, when
}) })
// todo: move to journeys validator?
t.equal(typeof model.earlierRef, 'string') t.equal(typeof model.earlierRef, 'string')
t.ok(model.earlierRef) t.ok(model.earlierRef)
t.equal(typeof model.laterRef, 'string') t.equal(typeof model.laterRef, 'string')
@ -167,11 +201,15 @@ test('earlier/later journeys', co(function* (t) {
client.journeys(spichernstr, bismarckstr, { client.journeys(spichernstr, bismarckstr, {
when, earlierThan: model.earlierRef when, earlierThan: model.earlierRef
}) })
// silence rejections, we're only interested in exceptions
.catch(() => {})
}) })
t.throws(() => { t.throws(() => {
client.journeys(spichernstr, bismarckstr, { client.journeys(spichernstr, bismarckstr, {
when, laterThan: model.laterRef when, laterThan: model.laterRef
}) })
// silence rejections, we're only interested in exceptions
.catch(() => {})
}) })
let earliestDep = Infinity, latestDep = -Infinity let earliestDep = Infinity, latestDep = -Infinity
@ -212,72 +250,52 @@ test('journey leg details', co(function* (t) {
t.ok(p.line.name, 'precondition failed') t.ok(p.line.name, 'precondition failed')
const leg = yield client.journeyLeg(p.id, p.line.name, {when}) const leg = yield client.journeyLeg(p.id, p.line.name, {when})
t.equal(typeof leg.id, 'string') validate(t, leg, 'journeyLeg', 'leg')
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)
t.end() t.end()
})) }))
test('journeys  station to address', co(function* (t) { test('journeys  station to address', co(function* (t) {
const latitude = 52.541797
const longitude = 13.350042
const journeys = yield client.journeys(spichernstr, { const journeys = yield client.journeys(spichernstr, {
type: 'location', type: 'location',
address: 'Torfstr. 17, Berlin', address: 'Torfstr. 17, Berlin',
latitude: 52.541797, longitude: 13.350042 latitude, longitude
}, {results: 1, when}) }, {results: 1, when})
t.ok(Array.isArray(journeys)) validate(t, journeys, 'journeys', 'journeys')
t.strictEqual(journeys.length, 1)
const journey = journeys[0]
const leg = journey.legs[journey.legs.length - 1]
assertValidStation(t, leg.origin) const i = journeys[0].legs.length - 1
assertValidWhen(t, leg.departure, when) const d = journeys[0].legs[i].destination
const name = `journeys[0].legs[${i}].destination`
const dest = leg.destination t.strictEqual(d.address, '13353 Berlin-Wedding, Torfstr. 17', name + '.address is invalid')
assertValidAddress(t, dest) t.ok(isRoughlyEqual(.0001, d.latitude, latitude), name + '.latitude is invalid')
t.strictEqual(dest.address, '13353 Berlin-Wedding, Torfstr. 17') t.ok(isRoughlyEqual(.0001, d.longitude, longitude), name + '.longitude is invalid')
t.ok(isRoughlyEqual(.0001, dest.latitude, 52.541797))
t.ok(isRoughlyEqual(.0001, dest.longitude, 13.350042))
assertValidWhen(t, leg.arrival, when)
t.end() t.end()
})) }))
test('journeys  station to POI', co(function* (t) { test('journeys  station to POI', co(function* (t) {
const latitude = 52.543333
const longitude = 13.351686
const journeys = yield client.journeys(spichernstr, { const journeys = yield client.journeys(spichernstr, {
type: 'location', type: 'location',
id: '900980720', id: atze,
name: 'Berlin, Atze Musiktheater für Kinder', name: 'Berlin, Atze Musiktheater für Kinder',
latitude: 52.543333, longitude: 13.351686 latitude, longitude
}, {results: 1, when}) }, {results: 1, when})
t.ok(Array.isArray(journeys)) validate(t, journeys, 'journeys', 'journeys')
t.strictEqual(journeys.length, 1)
const journey = journeys[0]
const leg = journey.legs[journey.legs.length - 1]
assertValidStation(t, leg.origin) const i = journeys[0].legs.length - 1
assertValidWhen(t, leg.departure, when) const d = journeys[0].legs[i].destination
const name = `journeys[0].legs[${i}].destination`
const dest = leg.destination t.strictEqual(d.id, atze, name + '.id is invalid')
assertValidPoi(t, dest) t.strictEqual(d.name, 'Berlin, Atze Musiktheater für Kinder', name + '.name is invalid')
t.strictEqual(dest.id, '900980720') t.ok(isRoughlyEqual(.0001, d.latitude, latitude), name + '.latitude is invalid')
t.strictEqual(dest.name, 'Berlin, Atze Musiktheater für Kinder') t.ok(isRoughlyEqual(.0001, d.longitude, longitude), name + '.longitude is invalid')
t.ok(isRoughlyEqual(.0001, dest.latitude, 52.543333))
t.ok(isRoughlyEqual(.0001, dest.longitude, 13.351686))
assertValidWhen(t, leg.arrival, when)
t.end() t.end()
})) }))
@ -285,41 +303,21 @@ test('journeys  station to POI', co(function* (t) {
test('journeys: via works with detour', co(function* (t) { test('journeys: via works with detour', co(function* (t) {
// Going from Westhafen to Wedding via Württembergalle without detour // Going from Westhafen to Wedding via Württembergalle without detour
// is currently impossible. We check if the routing engine computes a detour. // is currently impossible. We check if the routing engine computes a detour.
const westhafen = '900000001201' const journeys = yield client.journeys(westhafen, wedding, {
const wedding = '900000009104'
const württembergallee = '900000026153'
const [journey] = yield client.journeys(westhafen, wedding, {
via: württembergallee, via: württembergallee,
results: 1, results: 1,
when, when,
passedStations: true 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)) const leg = journeys[0].legs.some((leg) => {
t.ok(l, 'Württembergalle is not being passed') return leg.passed && leg.passed.some((passed) => {
return passed.station.id === württembergallee
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
}) })
t.ok(leg, 'Württembergalle is not being passed')
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.end() t.end()
})) }))
@ -327,29 +325,21 @@ test('journeys: via works without detour', co(function* (t) {
test('departures', co(function* (t) { test('departures', co(function* (t) {
const deps = yield client.departures(spichernstr, {duration: 5, when}) const deps = yield client.departures(spichernstr, {duration: 5, when})
t.ok(Array.isArray(deps)) validate(t, deps, 'departures', 'departures')
t.deepEqual(deps, deps.sort((a, b) => t.when > b.when)) for (let i = 0; i < deps.length; i++) {
for (let dep of deps) { const dep = deps[i]
t.equal(typeof dep.journeyId, 'string') const name = `deps[${i}]`
t.ok(dep.journeyId)
t.equal(dep.station.name, 'U Spichernstr.') t.equal(dep.station.name, 'U Spichernstr.', name + '.station.name is invalid')
assertValidStation(t, dep.station) t.equal(dep.station.id, spichernstr, name + '.station.id is invalid')
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.deepEqual(deps, deps.sort((a, b) => t.when > b.when))
t.end() t.end()
})) }))
test('departures with station object', co(function* (t) { test('departures with station object', co(function* (t) {
yield client.departures({ const deps = yield client.departures({
type: 'station', type: 'station',
id: spichernstr, id: spichernstr,
name: 'U Spichernstr', name: 'U Spichernstr',
@ -360,7 +350,7 @@ test('departures with station object', co(function* (t) {
} }
}, {when}) }, {when})
t.ok('did not fail') validate(t, deps, 'departures', 'departures')
t.end() t.end()
})) }))
@ -368,12 +358,9 @@ test('departures at 7-digit station', co(function* (t) {
const eisenach = '8010097' // see derhuerst/vbb-hafas#22 const eisenach = '8010097' // see derhuerst/vbb-hafas#22
yield client.departures(eisenach, {when}) yield client.departures(eisenach, {when})
t.pass('did not fail') t.pass('did not fail')
t.end() t.end()
})) }))
test('nearby', co(function* (t) { test('nearby', co(function* (t) {
// Berliner Str./Bundesallee // Berliner Str./Bundesallee
const nearby = yield client.nearby({ const nearby = yield client.nearby({
@ -382,18 +369,14 @@ test('nearby', co(function* (t) {
longitude: 13.3310411 longitude: 13.3310411
}, {distance: 200}) }, {distance: 200})
t.ok(Array.isArray(nearby)) validate(t, nearby, 'locations', 'nearby')
for (let n of nearby) {
if (n.type === 'station') assertValidStation(t, n)
else assertValidLocation(t, n, false)
}
t.equal(nearby[0].id, '900000044201') t.equal(nearby[0].id, berlinerStr)
t.equal(nearby[0].name, 'U Berliner Str.') t.equal(nearby[0].name, 'U Berliner Str.')
t.ok(nearby[0].distance > 0) t.ok(nearby[0].distance > 0)
t.ok(nearby[0].distance < 100) 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.equal(nearby[1].name, 'Landhausstr.')
t.ok(nearby[1].distance > 100) t.ok(nearby[1].distance > 100)
t.ok(nearby[1].distance < 200) t.ok(nearby[1].distance < 200)
@ -401,18 +384,12 @@ test('nearby', co(function* (t) {
t.end() t.end()
})) }))
test('locations', co(function* (t) { test('locations', co(function* (t) {
const locations = yield client.locations('Alexanderplatz', {results: 20}) const locations = yield client.locations('Alexanderplatz', {results: 20})
t.ok(Array.isArray(locations)) validate(t, locations, 'locations', 'locations')
t.ok(locations.length > 0)
t.ok(locations.length <= 20) 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.type === 'station'))
t.ok(locations.find(s => s.id && s.name)) // POIs t.ok(locations.find(s => s.id && s.name)) // POIs
t.ok(locations.find(s => !s.name && s.address)) // addresses t.ok(locations.find(s => !s.name && s.address)) // addresses
@ -421,21 +398,14 @@ test('locations', co(function* (t) {
})) }))
test('location', co(function* (t) { test('location', co(function* (t) {
const loc = yield client.location(spichernstr) const s = yield client.location(spichernstr)
assertValidStation(t, loc) validate(t, s, 'station', 'station')
t.equal(loc.id, spichernstr) t.equal(s.id, spichernstr)
t.ok(Array.isArray(loc.lines))
if (Array.isArray(loc.lines)) {
for (let line of loc.lines) assertValidLine(t, line)
}
t.end() t.end()
})) }))
test('radar', co(function* (t) { test('radar', co(function* (t) {
const vehicles = yield client.radar({ const vehicles = yield client.radar({
north: 52.52411, north: 52.52411,
@ -446,51 +416,6 @@ test('radar', co(function* (t) {
duration: 5 * 60, when duration: 5 * 60, when
}) })
t.ok(Array.isArray(vehicles)) validate(t, vehicles, 'movements', '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')
}
}
t.end() t.end()
})) }))