db-vendo-client/test/db.js

324 lines
8.6 KiB
JavaScript
Raw Normal View History

'use strict'
const getStations = require('db-stations').full
const tapePromise = require('tape-promise').default
const tape = require('tape')
const isRoughlyEqual = require('is-roughly-equal')
2018-01-23 01:49:41 +01:00
const co = require('./co')
const createClient = require('..')
2017-11-12 23:51:39 +01:00
const dbProfile = require('../p/db')
2017-11-13 00:30:14 +01:00
const modes = require('../p/db/modes')
const {
assertValidStation,
assertValidPoi,
assertValidAddress,
assertValidLocation,
assertValidLine,
assertValidStopover,
createWhen, assertValidWhen
} = require('./util.js')
const when = createWhen('Europe/Berlin', 'de-DE')
const assertValidStationProducts = (t, p) => {
t.ok(p)
t.equal(typeof p.nationalExp, 'boolean')
t.equal(typeof p.national, 'boolean')
t.equal(typeof p.regionalExp, 'boolean')
t.equal(typeof p.regional, 'boolean')
t.equal(typeof p.suburban, 'boolean')
t.equal(typeof p.bus, 'boolean')
t.equal(typeof p.ferry, 'boolean')
t.equal(typeof p.subway, 'boolean')
t.equal(typeof p.tram, 'boolean')
t.equal(typeof p.taxi, 'boolean')
}
const findStation = (id) => new Promise((yay, nay) => {
const stations = getStations()
stations
.once('error', nay)
.on('data', (s) => {
if (
s.id === id ||
(s.additionalIds && s.additionalIds.includes(id))
) {
yay(s)
stations.destroy()
}
})
.once('end', yay)
})
2017-11-12 21:23:29 +01:00
const isJungfernheide = (s) => {
return s.type === 'station' &&
(s.id === '008011167' || s.id === '8011167') &&
s.name === 'Berlin Jungfernheide' &&
s.location &&
isRoughlyEqual(s.location.latitude, 52.530408, .0005) &&
isRoughlyEqual(s.location.longitude, 13.299424, .0005)
2017-11-12 21:23:29 +01:00
}
const assertIsJungfernheide = (t, s) => {
t.equal(s.type, 'station')
t.ok(s.id === '008011167' || s.id === '8011167', 'id should be 8011167')
t.equal(s.name, 'Berlin Jungfernheide')
t.ok(s.location)
t.ok(isRoughlyEqual(s.location.latitude, 52.530408, .0005))
t.ok(isRoughlyEqual(s.location.longitude, 13.299424, .0005))
2017-11-12 21:23:29 +01:00
}
2017-12-11 19:40:46 +01:00
// todo: this doesnt seem to work
// todo: DRY with assertValidStationProducts
2017-11-13 00:30:14 +01:00
const assertValidProducts = (t, p) => {
for (let k of Object.keys(modes)) {
t.ok('boolean', typeof modes[k], 'mode ' + k + ' must be a boolean')
}
}
2017-12-11 16:06:37 +01:00
const assertValidPrice = (t, p) => {
t.ok(p)
if (p.amount !== null) {
t.equal(typeof p.amount, 'number')
t.ok(p.amount > 0)
}
if (p.hint !== null) {
t.equal(typeof p.hint, 'string')
t.ok(p.hint)
}
}
const test = tapePromise(tape)
const client = createClient(dbProfile)
2018-01-23 01:49:41 +01:00
test('Berlin Jungfernheide to München Hbf', co(function* (t) {
2017-11-29 02:27:31 +01:00
const journeys = yield client.journeys('8011167', '8000261', {
when, passedStations: true
})
t.ok(Array.isArray(journeys))
t.ok(journeys.length > 0, 'no journeys')
for (let journey of journeys) {
t.equal(journey.type, 'journey')
assertValidStation(t, journey.origin)
assertValidStationProducts(t, journey.origin.products)
2017-11-29 02:27:31 +01:00
if (!(yield findStation(journey.origin.id))) {
console.error('unknown station', journey.origin.id, journey.origin.name)
}
2017-11-13 00:30:14 +01:00
if (journey.origin.products) {
assertValidProducts(t, journey.origin.products)
}
assertValidWhen(t, journey.departure, when)
assertValidStation(t, journey.destination)
assertValidStationProducts(t, journey.origin.products)
2017-11-29 02:27:31 +01:00
if (!(yield findStation(journey.origin.id))) {
console.error('unknown station', journey.destination.id, journey.destination.name)
}
2017-11-13 00:30:14 +01:00
if (journey.destination.products) {
assertValidProducts(t, journey.destination.products)
}
assertValidWhen(t, journey.arrival, when)
t.ok(Array.isArray(journey.legs))
t.ok(journey.legs.length > 0, 'no legs')
const leg = journey.legs[0]
assertValidStation(t, leg.origin)
assertValidStationProducts(t, leg.origin.products)
if (!(yield findStation(leg.origin.id))) {
console.error('unknown station', leg.origin.id, leg.origin.name)
}
assertValidWhen(t, leg.departure, when)
t.equal(typeof leg.departurePlatform, 'string')
assertValidStation(t, leg.destination)
assertValidStationProducts(t, leg.origin.products)
if (!(yield findStation(leg.destination.id))) {
console.error('unknown station', leg.destination.id, leg.destination.name)
}
assertValidWhen(t, leg.arrival, when)
t.equal(typeof leg.arrivalPlatform, 'string')
assertValidLine(t, leg.line)
t.ok(Array.isArray(leg.passed))
for (let stopover of leg.passed) assertValidStopover(t, stopover)
2017-12-11 16:06:37 +01:00
if (journey.price) assertValidPrice(t, journey.price)
}
t.end()
2017-11-29 02:27:31 +01:00
}))
2018-01-23 01:49:41 +01:00
test('Berlin Jungfernheide to Torfstraße 17', co(function* (t) {
2017-11-29 02:27:31 +01:00
const journeys = yield client.journeys('8011167', {
2017-12-12 23:24:55 +01:00
type: 'location', address: 'Torfstraße 17',
latitude: 52.5416823, longitude: 13.3491223
}, {when})
t.ok(Array.isArray(journeys))
t.ok(journeys.length >= 1, 'no journeys')
const journey = journeys[0]
const leg = journey.legs[journey.legs.length - 1]
assertValidStation(t, leg.origin)
assertValidStationProducts(t, leg.origin.products)
if (!(yield findStation(leg.origin.id))) {
console.error('unknown station', leg.origin.id, leg.origin.name)
}
if (leg.origin.products) assertValidProducts(t, leg.origin.products)
assertValidWhen(t, leg.departure, when)
assertValidWhen(t, leg.arrival, when)
const d = leg.destination
assertValidAddress(t, d)
2017-12-11 19:40:46 +01:00
t.equal(d.address, 'Torfstraße 17')
t.ok(isRoughlyEqual(.0001, d.latitude, 52.5416823))
t.ok(isRoughlyEqual(.0001, d.longitude, 13.3491223))
t.end()
2017-11-29 02:27:31 +01:00
}))
2018-01-23 01:49:41 +01:00
test('Berlin Jungfernheide to ATZE Musiktheater', co(function* (t) {
2017-11-29 02:27:31 +01:00
const journeys = yield client.journeys('8011167', {
2017-12-12 23:24:55 +01:00
type: 'location', id: '991598902', name: 'ATZE Musiktheater',
latitude: 52.542417, longitude: 13.350437
}, {when})
t.ok(Array.isArray(journeys))
t.ok(journeys.length >= 1, 'no journeys')
const journey = journeys[0]
const leg = journey.legs[journey.legs.length - 1]
assertValidStation(t, leg.origin)
assertValidStationProducts(t, leg.origin.products)
if (!(yield findStation(leg.origin.id))) {
console.error('unknown station', leg.origin.id, leg.origin.name)
}
if (leg.origin.products) assertValidProducts(t, leg.origin.products)
assertValidWhen(t, leg.departure, when)
assertValidWhen(t, leg.arrival, when)
const d = leg.destination
assertValidPoi(t, d)
t.equal(d.name, 'ATZE Musiktheater')
t.ok(isRoughlyEqual(.0001, d.latitude, 52.542399))
t.ok(isRoughlyEqual(.0001, d.longitude, 13.350402))
t.end()
2017-11-29 02:27:31 +01:00
}))
2018-01-23 01:49:41 +01:00
test('Berlin Hbf to München Hbf with stopover at Hannover Hbf', co(function* (t) {
const berlinHbf = '8011160'
const münchenHbf = '8000261'
const hannoverHbf = '8000152'
const [journey] = yield client.journeys(berlinHbf, münchenHbf, {
via: hannoverHbf,
results: 1
})
const i = journey.legs.findIndex(leg => leg.destination.id === hannoverHbf)
t.ok(i >= 0, 'no leg with Hannover Hbf as destination')
const nextLeg = journey.legs[i + 1]
t.ok(nextLeg)
t.equal(nextLeg.origin.id, hannoverHbf)
t.end()
}))
2018-01-23 01:49:41 +01:00
test('departures at Berlin Jungfernheide', co(function* (t) {
2017-11-29 02:27:31 +01:00
const deps = yield client.departures('8011167', {
duration: 5, when
})
t.ok(Array.isArray(deps))
for (let dep of deps) {
assertValidStation(t, dep.station)
assertValidStationProducts(t, dep.station.products)
2017-11-29 02:27:31 +01:00
if (!(yield findStation(dep.station.id))) {
console.error('unknown station', dep.station.id, dep.station.name)
}
2017-11-13 00:30:14 +01:00
if (dep.station.products) assertValidProducts(t, dep.station.products)
assertValidWhen(t, dep.when, when)
}
t.end()
2017-11-29 02:27:31 +01:00
}))
2018-01-23 01:49:41 +01:00
test('departures with station object', co(function* (t) {
yield client.departures({
type: 'station',
id: '8011167',
name: 'Berlin Jungfernheide',
location: {
type: 'location',
latitude: 1.23,
longitude: 2.34
}
}, {when})
t.ok('did not fail')
t.end()
}))
2018-01-23 01:49:41 +01:00
test('nearby Berlin Jungfernheide', co(function* (t) {
2018-01-05 14:53:03 +01:00
const nearby = yield client.nearby({
type: 'location',
latitude: 52.530273,
longitude: 13.299433
}, {
results: 2, distance: 400
})
t.ok(Array.isArray(nearby))
t.equal(nearby.length, 2)
assertIsJungfernheide(t, nearby[0])
t.ok(nearby[0].distance >= 0)
t.ok(nearby[0].distance <= 100)
2017-12-11 19:53:26 +01:00
for (let n of nearby) {
if (n.type === 'station') assertValidStation(t, n)
else assertValidLocation(t, n)
}
t.end()
2017-11-29 02:27:31 +01:00
}))
2018-01-23 01:49:41 +01:00
test('locations named Jungfernheide', co(function* (t) {
2017-11-29 02:27:31 +01:00
const locations = yield client.locations('Jungfernheide', {
results: 10
})
t.ok(Array.isArray(locations))
t.ok(locations.length > 0)
t.ok(locations.length <= 10)
2017-12-11 19:53:26 +01:00
for (let l of locations) {
if (l.type === 'station') assertValidStation(t, l)
else assertValidLocation(t, l)
}
2017-11-12 21:23:29 +01:00
t.ok(locations.some(isJungfernheide))
t.end()
2017-11-29 02:27:31 +01:00
}))
2018-01-26 17:08:07 +01:00
test('location', co(function* (t) {
const regensburgHbf = '8000309'
const loc = yield client.location(regensburgHbf)
assertValidStation(t, loc)
t.equal(loc.id, regensburgHbf)
t.ok(Array.isArray(loc.lines))
if (Array.isArray(loc.lines)) {
for (let line of loc.lines) assertValidLine(t, line)
}
2018-01-26 17:08:07 +01:00
t.end()
}))