mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
leg.alternatives: DB smoke test ✅
This commit is contained in:
parent
9f1a0a0f8a
commit
d9126d531a
3 changed files with 66 additions and 31 deletions
37
test/bvg.js
37
test/bvg.js
|
@ -26,6 +26,7 @@ const testJourneysStationToStation = require('./lib/journeys-station-to-station'
|
||||||
const testJourneysStationToAddress = require('./lib/journeys-station-to-address')
|
const testJourneysStationToAddress = require('./lib/journeys-station-to-address')
|
||||||
const testJourneysStationToPoi = require('./lib/journeys-station-to-poi')
|
const testJourneysStationToPoi = require('./lib/journeys-station-to-poi')
|
||||||
const testEarlierLaterJourneys = require('./lib/earlier-later-journeys')
|
const testEarlierLaterJourneys = require('./lib/earlier-later-journeys')
|
||||||
|
const testLegCycleAlternatives = require('./lib/leg-cycle-alternatives')
|
||||||
const testRefreshJourney = require('./lib/refresh-journey')
|
const testRefreshJourney = require('./lib/refresh-journey')
|
||||||
const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product')
|
const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product')
|
||||||
const testDepartures = require('./lib/departures')
|
const testDepartures = require('./lib/departures')
|
||||||
|
@ -144,38 +145,12 @@ test('earlier/later journeys', co(function* (t) {
|
||||||
}))
|
}))
|
||||||
|
|
||||||
test('journeys – leg cycle & alternatives', co(function* (t) {
|
test('journeys – leg cycle & alternatives', co(function* (t) {
|
||||||
// Apparently the BVG endpoint only returns alternatives for S-Bahn legs,
|
yield testLegCycleAlternatives({
|
||||||
// not for U-Bahn legs. It also won't return the cycle or alternatives
|
test: t,
|
||||||
// more than ~2 hours in advance. This is why we don't pass `when` here.
|
fetchJourneys: client.journeys,
|
||||||
const journeys = yield client.journeys(tiergarten, jannowitzbrücke, {
|
fromId: tiergarten,
|
||||||
results: 3
|
toId: jannowitzbrücke
|
||||||
})
|
})
|
||||||
|
|
||||||
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]
|
|
||||||
if (!leg.line) continue
|
|
||||||
const name = `journeys[${i}].legs[${j}]`
|
|
||||||
|
|
||||||
t.ok(leg.cycle, name + '.cycle is missing')
|
|
||||||
t.equal(typeof leg.cycle.min, 'number', name + '.cycle.min is not a number')
|
|
||||||
t.equal(typeof leg.cycle.max, 'number', name + '.cycle.max is not a number')
|
|
||||||
t.equal(typeof leg.cycle.nr, 'number', name + '.cycle.nr is not a number')
|
|
||||||
|
|
||||||
const lW = +new Date(leg.departure)
|
|
||||||
t.ok(Array.isArray(leg.alternatives), name + '.alternatives must be an array')
|
|
||||||
for (let k = 0; k < leg.alternatives.length; k++) {
|
|
||||||
const a = leg.alternatives[k]
|
|
||||||
const n = name + `.alternatives[${k}]`
|
|
||||||
|
|
||||||
let aW = +new Date(a.when)
|
|
||||||
if ('number' === typeof a.delay) aW -= a.delay * 1000
|
|
||||||
t.ok(isRoughlyEqual(2 * hour, aW, lW), n + '.when seems invalid')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
t.end()
|
t.end()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
|
13
test/db.js
13
test/db.js
|
@ -20,6 +20,7 @@ const testJourneysStationToStation = require('./lib/journeys-station-to-station'
|
||||||
const testJourneysStationToAddress = require('./lib/journeys-station-to-address')
|
const testJourneysStationToAddress = require('./lib/journeys-station-to-address')
|
||||||
const testJourneysStationToPoi = require('./lib/journeys-station-to-poi')
|
const testJourneysStationToPoi = require('./lib/journeys-station-to-poi')
|
||||||
const testEarlierLaterJourneys = require('./lib/earlier-later-journeys')
|
const testEarlierLaterJourneys = require('./lib/earlier-later-journeys')
|
||||||
|
const testLegCycleAlternatives = require('./lib/leg-cycle-alternatives')
|
||||||
const testRefreshJourney = require('./lib/refresh-journey')
|
const testRefreshJourney = require('./lib/refresh-journey')
|
||||||
const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product')
|
const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product')
|
||||||
const testDepartures = require('./lib/departures')
|
const testDepartures = require('./lib/departures')
|
||||||
|
@ -79,6 +80,8 @@ const wedding = '008089131'
|
||||||
const württembergallee = '731084'
|
const württembergallee = '731084'
|
||||||
const regensburgHbf = '8000309'
|
const regensburgHbf = '8000309'
|
||||||
const blnOstbahnhof = '8010255'
|
const blnOstbahnhof = '8010255'
|
||||||
|
const blnTiergarten = '8089091'
|
||||||
|
const blnJannowitzbrücke = '8089019'
|
||||||
|
|
||||||
test('journeys – Berlin Schwedter Str. to München Hbf', co(function* (t) {
|
test('journeys – Berlin Schwedter Str. to München Hbf', co(function* (t) {
|
||||||
const journeys = yield client.journeys(blnSchwedterStr, münchenHbf, {
|
const journeys = yield client.journeys(blnSchwedterStr, münchenHbf, {
|
||||||
|
@ -195,6 +198,16 @@ test('earlier/later journeys, Jungfernheide -> München Hbf', co(function* (t) {
|
||||||
t.end()
|
t.end()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
test('journeys – leg cycle & alternatives', co(function* (t) {
|
||||||
|
yield testLegCycleAlternatives({
|
||||||
|
test: t,
|
||||||
|
fetchJourneys: client.journeys,
|
||||||
|
fromId: blnTiergarten,
|
||||||
|
toId: blnJannowitzbrücke
|
||||||
|
})
|
||||||
|
t.end()
|
||||||
|
}))
|
||||||
|
|
||||||
test('refreshJourney', co(function* (t) {
|
test('refreshJourney', co(function* (t) {
|
||||||
yield testRefreshJourney({
|
yield testRefreshJourney({
|
||||||
test: t,
|
test: t,
|
||||||
|
|
47
test/lib/leg-cycle-alternatives.js
Normal file
47
test/lib/leg-cycle-alternatives.js
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const isRoughlyEqual = require('is-roughly-equal')
|
||||||
|
|
||||||
|
const co = require('./co')
|
||||||
|
const {hour} = require('./util')
|
||||||
|
|
||||||
|
const testLegCycleAlternatives = co(function* (cfg) {
|
||||||
|
const {
|
||||||
|
test: t,
|
||||||
|
fetchJourneys,
|
||||||
|
fromId,
|
||||||
|
toId
|
||||||
|
} = cfg
|
||||||
|
|
||||||
|
// Apparently HAFAS doesn't return the leg cycle or alternatives more
|
||||||
|
// than ~2 hours in advance. This is why we don't pass `when` here.
|
||||||
|
const journeys = yield fetchJourneys(fromId, toId, {results: 3})
|
||||||
|
|
||||||
|
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]
|
||||||
|
const name = `journeys[${i}].legs[${j}]`
|
||||||
|
|
||||||
|
if (!leg.line) continue
|
||||||
|
|
||||||
|
t.ok(leg.cycle, name + '.cycle is missing')
|
||||||
|
t.equal(typeof leg.cycle.min, 'number', name + '.cycle.min is not a number')
|
||||||
|
t.equal(typeof leg.cycle.max, 'number', name + '.cycle.max is not a number')
|
||||||
|
t.equal(typeof leg.cycle.nr, 'number', name + '.cycle.nr is not a number')
|
||||||
|
|
||||||
|
const lineWhen = +new Date(leg.departure)
|
||||||
|
t.ok(Array.isArray(leg.alternatives), name + '.alternatives must be an array')
|
||||||
|
for (let k = 0; k < leg.alternatives.length; k++) {
|
||||||
|
const a = leg.alternatives[k]
|
||||||
|
const n = name + `.alternatives[${k}]`
|
||||||
|
|
||||||
|
let alternativeWhen = +new Date(a.when)
|
||||||
|
if ('number' === typeof a.delay) alternativeWhen -= a.delay * 1000
|
||||||
|
t.ok(isRoughlyEqual(2 * hour, alternativeWhen, lineWhen), n + '.when seems invalid')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
module.exports = testLegCycleAlternatives
|
Loading…
Add table
Reference in a new issue