vbb tests: code style 👕

This commit is contained in:
Jannis R 2017-11-14 02:59:17 +01:00
parent c9739cf27d
commit 7cf1261347
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5

View file

@ -1,10 +1,10 @@
'use strict' 'use strict'
const test = require('tape')
const a = require('assert') const a = require('assert')
const isRoughlyEqual = require('is-roughly-equal') const isRoughlyEqual = require('is-roughly-equal')
const stations = require('vbb-stations-autocomplete') const stations = require('vbb-stations-autocomplete')
const floor = require('floordate') const tapePromise = require('tape-promise').default
const tape = require('tape')
const createClient = require('..') const createClient = require('..')
const vbbProfile = require('../p/vbb') const vbbProfile = require('../p/vbb')
@ -20,59 +20,59 @@ const {
assertValidWhen assertValidWhen
} = require('./util') } = require('./util')
// todo
const findStation = (query) => stations(query, true, false) const findStation = (query) => stations(query, true, false)
const test = tapePromise(tape)
const client = createClient(vbbProfile) const client = createClient(vbbProfile)
test('journeys  station to station', (t) => { test('journeys  station to station', async (t) => {
// U Spichernstr. to U Amrumer Str. // U Spichernstr. to U Amrumer Str.
client.journeys('900000042101', '900000009101', { const journeys = await client.journeys('900000042101', '900000009101', {
results: 3, when, passedStations: true results: 3, when, passedStations: true
}) })
.then((journeys) => {
t.ok(Array.isArray(journeys))
t.strictEqual(journeys.length, 3)
for (let journey of journeys) { t.ok(Array.isArray(journeys))
assertValidStation(t, journey.origin) t.strictEqual(journeys.length, 3)
t.ok(journey.origin.name.indexOf('(Berlin)') === -1)
t.strictEqual(journey.origin.id, '900000042101')
assertValidWhen(t, journey.departure)
assertValidStation(t, journey.destination) for (let journey of journeys) {
t.strictEqual(journey.destination.id, '900000009101') assertValidStation(t, journey.origin)
assertValidWhen(t, journey.arrival) t.ok(journey.origin.name.indexOf('(Berlin)') === -1)
t.strictEqual(journey.origin.id, '900000042101')
assertValidWhen(t, journey.departure)
t.ok(Array.isArray(journey.parts)) assertValidStation(t, journey.destination)
t.strictEqual(journey.parts.length, 1) t.strictEqual(journey.destination.id, '900000009101')
const part = journey.parts[0] assertValidWhen(t, journey.arrival)
t.equal(typeof part.id, 'string') t.ok(Array.isArray(journey.parts))
t.ok(part.id) t.strictEqual(journey.parts.length, 1)
assertValidStation(t, part.origin) const part = journey.parts[0]
t.ok(part.origin.name.indexOf('(Berlin)') === -1)
t.strictEqual(part.origin.id, '900000042101')
assertValidWhen(t, part.departure)
assertValidStation(t, part.destination) t.equal(typeof part.id, 'string')
t.strictEqual(part.destination.id, '900000009101') t.ok(part.id)
assertValidWhen(t, part.arrival) assertValidStation(t, part.origin)
t.ok(part.origin.name.indexOf('(Berlin)') === -1)
t.strictEqual(part.origin.id, '900000042101')
assertValidWhen(t, part.departure)
assertValidLine(t, part.line) assertValidStation(t, part.destination)
t.ok(findStation(part.direction)) t.strictEqual(part.destination.id, '900000009101')
t.ok(part.direction.indexOf('(Berlin)') === -1) assertValidWhen(t, part.arrival)
t.ok(Array.isArray(part.passed)) assertValidLine(t, part.line)
for (let passed of part.passed) assertValidPassed(t, passed) t.ok(findStation(part.direction))
} t.ok(part.direction.indexOf('(Berlin)') === -1)
})
.catch(t.ifError) t.ok(Array.isArray(part.passed))
.then(() => t.end()) for (let passed of part.passed) assertValidPassed(t, passed)
}
t.end()
}) })
test('journeys  only subway', (t) => { test('journeys  only subway', async (t) => {
// U Spichernstr. to U Bismarckstr. // U Spichernstr. to U Bismarckstr.
client.journeys('900000042101', '900000024201', { const journeys = await client.journeys('900000042101', '900000024201', {
results: 20, when, results: 20, when,
products: { products: {
suburban: false, suburban: false,
@ -84,246 +84,239 @@ test('journeys  only subway', (t) => {
regional: false regional: false
} }
}) })
.then((journeys) => {
t.ok(Array.isArray(journeys))
t.ok(journeys.length > 1)
for (let journey of journeys) { t.ok(Array.isArray(journeys))
for (let part of journey.parts) { t.ok(journeys.length > 1)
if (part.line) {
t.equal(part.line.mode, 'train') for (let journey of journeys) {
t.equal(part.line.product, 'subway') for (let part of journey.parts) {
t.equal(part.line.public, true) if (part.line) {
} t.equal(part.line.mode, 'train')
t.equal(part.line.product, 'subway')
t.equal(part.line.public, true)
} }
} }
}) }
.catch(t.ifError) t.end()
.then(() => t.end())
}) })
test('journeys  fails with no product', (t) => { test('journeys  fails with no product', async (t) => {
// U Spichernstr. to U Bismarckstr. try {
client.journeys('900000042101', '900000024201', { // U Spichernstr. to U Bismarckstr.
when, await client.journeys('900000042101', '900000024201', {
products: { when,
suburban: false, products: {
subway: false, suburban: false,
tram: false, subway: false,
bus: false, tram: false,
ferry: false, bus: false,
express: false, ferry: false,
regional: false express: false,
} regional: false
}) }
.catch((err) => { })
} catch (err) {
t.ok(err, 'error thrown') t.ok(err, 'error thrown')
t.end() t.end()
}) }
}) })
test('journey part details', (t) => { test('journey part details', async (t) => {
// U Spichernstr. to U Amrumer Str. // U Spichernstr. to U Amrumer Str.
client.journeys('900000042101', '900000009101', {results: 1, when}) const journeys = await client.journeys('900000042101', '900000009101', {
.then((journeys) => { results: 1, when
const part = journeys[0].parts[0]
t.ok(part.id, 'precondition failed')
t.ok(part.line.name, 'precondition failed')
return client.journeyPart(part.id, part.line.name, {when})
}) })
.then((part) => {
t.equal(typeof part.id, 'string')
t.ok(part.id)
assertValidLine(t, part.line) const p = journeys[0].parts[0]
t.ok(p.id, 'precondition failed')
t.ok(p.line.name, 'precondition failed')
const part = await client.journeyPart(p.id, p.line.name, {when})
t.equal(typeof part.direction, 'string') t.equal(typeof part.id, 'string')
t.ok(part.direction) t.ok(part.id)
t.ok(Array.isArray(part.passed)) assertValidLine(t, part.line)
for (let passed of part.passed) assertValidPassed(t, passed)
}) t.equal(typeof part.direction, 'string')
.catch(t.ifError) t.ok(part.direction)
.then(() => t.end())
t.ok(Array.isArray(part.passed))
for (let passed of part.passed) assertValidPassed(t, passed)
t.end()
}) })
test('journeys  station to address', (t) => { test('journeys  station to address', async (t) => {
// U Spichernstr. to Torfstraße 17 // U Spichernstr. to Torfstraße 17
client.journeys('900000042101', { const journeys = await client.journeys('900000042101', {
type: 'address', name: 'Torfstraße 17', type: 'address', name: 'Torfstraße 17',
latitude: 52.5416823, longitude: 13.3491223 latitude: 52.5416823, longitude: 13.3491223
}, {results: 1, when}) }, {results: 1, when})
.then((journeys) => {
t.ok(Array.isArray(journeys))
t.strictEqual(journeys.length, 1)
const journey = journeys[0]
const part = journey.parts[journey.parts.length - 1]
assertValidStation(t, part.origin) t.ok(Array.isArray(journeys))
assertValidWhen(t, part.departure) t.strictEqual(journeys.length, 1)
const journey = journeys[0]
const part = journey.parts[journey.parts.length - 1]
const dest = part.destination assertValidStation(t, part.origin)
assertValidAddress(t, dest) assertValidWhen(t, part.departure)
t.strictEqual(dest.name, 'Torfstr. 17')
t.ok(isRoughlyEqual(.0001, dest.coordinates.latitude, 52.5416823)) const dest = part.destination
t.ok(isRoughlyEqual(.0001, dest.coordinates.longitude, 13.3491223)) assertValidAddress(t, dest)
assertValidWhen(t, part.arrival) t.strictEqual(dest.name, 'Torfstr. 17')
}) t.ok(isRoughlyEqual(.0001, dest.coordinates.latitude, 52.5416823))
.catch(t.ifError) t.ok(isRoughlyEqual(.0001, dest.coordinates.longitude, 13.3491223))
.then(() => t.end()) assertValidWhen(t, part.arrival)
t.end()
}) })
test('journeys  station to POI', (t) => { test('journeys  station to POI', async (t) => {
// U Spichernstr. to ATZE Musiktheater // U Spichernstr. to ATZE Musiktheater
client.journeys('900000042101', { const journeys = await client.journeys('900000042101', {
type: 'poi', name: 'ATZE Musiktheater', id: 9980720, type: 'poi', name: 'ATZE Musiktheater', id: 9980720,
latitude: 52.543333, longitude: 13.351686 latitude: 52.543333, longitude: 13.351686
}, {results: 1, when}) }, {results: 1, when})
.then((journeys) => {
t.ok(Array.isArray(journeys))
t.strictEqual(journeys.length, 1)
const journey = journeys[0]
const part = journey.parts[journey.parts.length - 1]
assertValidStation(t, part.origin) t.ok(Array.isArray(journeys))
assertValidWhen(t, part.departure) t.strictEqual(journeys.length, 1)
const journey = journeys[0]
const part = journey.parts[journey.parts.length - 1]
const dest = part.destination assertValidStation(t, part.origin)
assertValidPoi(t, dest) assertValidWhen(t, part.departure)
t.strictEqual(dest.name, 'ATZE Musiktheater')
t.ok(isRoughlyEqual(.0001, dest.coordinates.latitude, 52.543333)) const dest = part.destination
t.ok(isRoughlyEqual(.0001, dest.coordinates.longitude, 13.351686)) assertValidPoi(t, dest)
assertValidWhen(t, part.arrival) t.strictEqual(dest.name, 'ATZE Musiktheater')
}) t.ok(isRoughlyEqual(.0001, dest.coordinates.latitude, 52.543333))
.catch(t.ifError) t.ok(isRoughlyEqual(.0001, dest.coordinates.longitude, 13.351686))
.then(() => t.end()) assertValidWhen(t, part.arrival)
t.end()
}) })
test('departures', (t) => { test('departures', async (t) => {
client.departures('900000042101', {duration: 5, when}) // U Spichernstr. // U Spichernstr.
.then((deps) => { const deps = await client.departures('900000042101', {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.ref, 'string')
t.ok(dep.ref)
t.equal(dep.station.name, 'U Spichernstr.') t.ok(Array.isArray(deps))
assertValidStation(t, dep.station) t.deepEqual(deps, deps.sort((a, b) => t.when > b.when))
t.strictEqual(dep.station.id, '900000042101') for (let dep of deps) {
t.equal(typeof dep.ref, 'string')
t.ok(dep.ref)
assertValidWhen(t, dep.when) t.equal(dep.station.name, 'U Spichernstr.')
t.ok(findStation(dep.direction)) assertValidStation(t, dep.station)
assertValidLine(t, dep.line) t.strictEqual(dep.station.id, '900000042101')
}
}) assertValidWhen(t, dep.when)
.catch(t.ifError) t.ok(findStation(dep.direction))
.then(() => t.end()) assertValidLine(t, dep.line)
}
t.end()
}) })
test('departures at 7-digit station', (t) => { test('departures at 7-digit station', async (t) => {
const eisenach = '8010097' // see derhuerst/vbb-hafas#22 const eisenach = '8010097' // see derhuerst/vbb-hafas#22
client.departures(eisenach, {when}) await client.departures(eisenach, {when})
.then(() => { t.pass('did not fail')
t.pass('did not fail')
t.end() t.end()
})
.catch(t.ifError)
}) })
test('nearby', (t) => { test('nearby', async (t) => {
client.nearby(52.4873452,13.3310411, {distance: 200}) // Berliner Str./Bundesallee // Berliner Str./Bundesallee
.then((nearby) => { const nearby = await client.nearby(52.4873452,13.3310411, {distance: 200})
t.ok(Array.isArray(nearby))
for (let n of nearby) assertValidLocation(t, n, false)
t.equal(nearby[0].id, '900000044201') t.ok(Array.isArray(nearby))
t.equal(nearby[0].name, 'U Berliner Str.') for (let n of nearby) assertValidLocation(t, n, false)
t.ok(nearby[0].distance > 0)
t.ok(nearby[0].distance < 100)
t.equal(nearby[1].id, '900000043252') t.equal(nearby[0].id, '900000044201')
t.equal(nearby[1].name, 'Landhausstr.') t.equal(nearby[0].name, 'U Berliner Str.')
t.ok(nearby[1].distance > 100) t.ok(nearby[0].distance > 0)
t.ok(nearby[1].distance < 200) t.ok(nearby[0].distance < 100)
})
.catch(t.ifError) t.equal(nearby[1].id, '900000043252')
.then(() => t.end()) t.equal(nearby[1].name, 'Landhausstr.')
t.ok(nearby[1].distance > 100)
t.ok(nearby[1].distance < 200)
t.end()
}) })
test('locations', (t) => { test('locations', async (t) => {
client.locations('Alexanderplatz', {results: 10}) const locations = await client.locations('Alexanderplatz', {results: 10})
.then((locations) => {
t.ok(Array.isArray(locations)) t.ok(Array.isArray(locations))
t.ok(locations.length > 0) t.ok(locations.length > 0)
t.ok(locations.length <= 10) t.ok(locations.length <= 10)
for (let l of locations) assertValidLocation(t, l) for (let l of locations) 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.type === 'poi')) t.ok(locations.find((s) => s.type === 'poi'))
t.ok(locations.find((s) => s.type === 'address')) t.ok(locations.find((s) => s.type === 'address'))
})
.catch(t.ifError) t.end()
.then(() => t.end())
}) })
test('radar', (t) => { test('radar', async (t) => {
client.radar(52.52411, 13.41002, 52.51942, 13.41709, {duration: 5 * 60, when}) const vehicles = await client.radar(52.52411, 13.41002, 52.51942, 13.41709, {
.then((vehicles) => { duration: 5 * 60, when
t.ok(Array.isArray(vehicles)) })
t.ok(vehicles.length > 0)
for (let v of vehicles) {
t.ok(findStation(v.direction)) t.ok(Array.isArray(vehicles))
assertValidLine(t, v.line) t.ok(vehicles.length > 0)
for (let v of vehicles) {
t.equal(typeof v.coordinates.latitude, 'number') t.ok(findStation(v.direction))
t.ok(v.coordinates.latitude <= 55, 'vehicle is too far away') assertValidLine(t, v.line)
t.ok(v.coordinates.latitude >= 45, 'vehicle is too far away')
t.equal(typeof v.coordinates.longitude, 'number')
t.ok(v.coordinates.longitude >= 9, 'vehicle is too far away')
t.ok(v.coordinates.longitude <= 15, 'vehicle is too far away')
t.ok(Array.isArray(v.nextStops)) t.equal(typeof v.coordinates.latitude, 'number')
for (let s of v.nextStops) { t.ok(v.coordinates.latitude <= 55, 'vehicle is too far away')
assertValidFrameStation(t, s.station) t.ok(v.coordinates.latitude >= 45, 'vehicle is too far away')
if (!s.arrival && !s.departure) t.equal(typeof v.coordinates.longitude, 'number')
t.ifError(new Error('neither arrival nor departure return')) t.ok(v.coordinates.longitude >= 9, 'vehicle is too far away')
if (s.arrival) { t.ok(v.coordinates.longitude <= 15, 'vehicle is too far away')
t.equal(typeof s.arrival, 'string')
const arr = +new Date(s.arrival) t.ok(Array.isArray(v.nextStops))
t.ok(!Number.isNaN(arr)) for (let s of v.nextStops) {
// note that this can be an ICE train assertValidFrameStation(t, s.station)
t.ok(isRoughlyEqual(14 * hour, +when, arr)) if (!s.arrival && !s.departure)
} t.ifError(new Error('neither arrival nor departure return'))
if (s.departure) { if (s.arrival) {
t.equal(typeof s.departure, 'string') t.equal(typeof s.arrival, 'string')
const dep = +new Date(s.departure) const arr = +new Date(s.arrival)
t.ok(!Number.isNaN(dep)) t.ok(!Number.isNaN(arr))
// note that this can be an ICE train // note that this can be an ICE train
t.ok(isRoughlyEqual(14 * hour, +when, dep)) t.ok(isRoughlyEqual(14 * hour, +when, arr))
}
} }
if (s.departure) {
t.ok(Array.isArray(v.frames)) t.equal(typeof s.departure, 'string')
for (let f of v.frames) { const dep = +new Date(s.departure)
assertValidFrameStation(t, f.origin) t.ok(!Number.isNaN(dep))
assertValidFrameStation(t, f.destination) // note that this can be an ICE train
t.equal(typeof f.t, 'number') t.ok(isRoughlyEqual(14 * hour, +when, dep))
} }
} }
})
.catch(t.ifError) t.ok(Array.isArray(v.frames))
.then(() => t.end()) for (let f of v.frames) {
assertValidFrameStation(t, f.origin)
assertValidFrameStation(t, f.destination)
t.equal(typeof f.t, 'number')
}
}
t.end()
}) })