db-vendo-client/test/lib/earlier-later-journeys.js

87 lines
2.1 KiB
JavaScript
Raw Normal View History

'use strict'
const co = require('./co')
const testEarlierLaterJourneys = co(function* (cfg) {
const {
test: t,
fetchJourneys,
fromId,
toId,
when
// todo: validate
} = cfg
const model = yield fetchJourneys(fromId, toId, {
2018-05-31 13:42:54 +02:00
results: 3, departure: when
})
// todo: move to journeys validator?
t.equal(typeof model.earlierRef, 'string')
t.ok(model.earlierRef)
t.equal(typeof model.laterRef, 'string')
t.ok(model.laterRef)
2018-05-31 13:42:54 +02:00
// departure/arrival and earlierThan/laterThan should be mutually exclusive
t.throws(() => {
fetchJourneys(fromId, toId, {
2018-05-31 13:42:54 +02:00
departure: when, earlierThan: model.earlierRef
})
// silence rejections, we're only interested in exceptions
.catch(() => {})
})
t.throws(() => {
fetchJourneys(fromId, toId, {
2018-05-31 13:42:54 +02:00
departure: when, laterThan: model.laterRef
})
// silence rejections, we're only interested in exceptions
.catch(() => {})
})
t.throws(() => {
fetchJourneys(fromId, toId, {
arrival: when, earlierThan: model.earlierRef
})
// silence rejections, we're only interested in exceptions
.catch(() => {})
})
t.throws(() => {
fetchJourneys(fromId, toId, {
arrival: when, laterThan: model.laterRef
})
// silence rejections, we're only interested in exceptions
.catch(() => {})
})
let earliestDep = Infinity, latestDep = -Infinity
for (let j of model) {
2018-05-07 12:33:53 +02:00
if (j.legs[0].departure === null) continue
const dep = +new Date(j.legs[0].departure)
if (dep < earliestDep) earliestDep = dep
else if (dep > latestDep) latestDep = dep
}
const earlier = yield fetchJourneys(fromId, toId, {
results: 3,
// todo: single journey ref?
earlierThan: model.earlierRef
})
for (let j of earlier) {
const firstLeg = j.legs[0]
const dep = new Date(firstLeg.departure || firstLeg.formerScheduledDeparture)
t.ok(dep < earliestDep)
}
const later = yield fetchJourneys(fromId, toId, {
results: 3,
// todo: single journey ref?
laterThan: model.laterRef
})
for (let j of later) {
const firstLeg = j.legs[0]
const dep = new Date(firstLeg.departure || firstLeg.formerScheduledDeparture)
t.ok(dep > latestDep)
}
})
module.exports = testEarlierLaterJourneys