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

85 lines
2.1 KiB
JavaScript
Raw Normal View History

'use strict'
2018-11-21 19:07:37 +01:00
const testEarlierLaterJourneys = async (cfg) => {
const {
test: t,
fetchJourneys,
fromId,
toId,
when
// todo: validate
} = cfg
2018-11-21 19:07:37 +01:00
const model = await 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.journeys) {
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
}
2018-11-21 19:07:37 +01:00
const earlier = await fetchJourneys(fromId, toId, {
results: 3,
// todo: single journey ref?
earlierThan: model.earlierRef
})
for (let j of earlier.journeys) {
const firstLeg = j.legs[0]
2019-02-14 14:06:54 +01:00
const dep = new Date(firstLeg.departure || firstLeg.scheduledDeparture)
t.ok(dep < earliestDep)
}
2018-11-21 19:07:37 +01:00
const later = await fetchJourneys(fromId, toId, {
results: 3,
// todo: single journey ref?
laterThan: model.laterRef
})
for (let j of later.journeys) {
const firstLeg = j.legs[0]
2019-02-14 14:06:54 +01:00
const dep = new Date(firstLeg.departure || firstLeg.scheduledDeparture)
t.ok(dep > latestDep)
}
2018-11-21 19:07:37 +01:00
}
module.exports = testEarlierLaterJourneys