diff --git a/test/db.js b/test/db.js index 89cf28c1..09371669 100644 --- a/test/db.js +++ b/test/db.js @@ -16,6 +16,7 @@ const { } = require('./lib/validators') const createValidate = require('./lib/validate-fptf-with') const testJourneysStationToStation = require('./lib/journeys-station-to-station') +const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') const isObj = o => o !== null && 'object' === typeof o && !Array.isArray(o) @@ -154,57 +155,14 @@ test('journeys: via works – with detour', co(function* (t) { })) test('earlier/later journeys, Jungfernheide -> München Hbf', co(function* (t) { - const model = yield client.journeys(jungfernheide, münchenHbf, { - results: 3, when + yield testEarlierLaterJourneys({ + test: t, + fetchJourneys: client.journeys, + validate, + fromId: jungfernheide, + toId: münchenHbf }) - // 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) - - // when and earlierThan/laterThan should be mutually exclusive - t.throws(() => { - client.journeys(jungfernheide, münchenHbf, { - when, earlierThan: model.earlierRef - }) - // silence rejections, we're only interested in exceptions - .catch(() => {}) - }) - t.throws(() => { - client.journeys(jungfernheide, münchenHbf, { - when, laterThan: model.laterRef - }) - // silence rejections, we're only interested in exceptions - .catch(() => {}) - }) - - let earliestDep = Infinity, latestDep = -Infinity - for (let j of model) { - const dep = +new Date(j.legs[0].departure) - if (dep < earliestDep) earliestDep = dep - else if (dep > latestDep) latestDep = dep - } - - const earlier = yield client.journeys(jungfernheide, münchenHbf, { - results: 3, - // todo: single journey ref? - earlierThan: model.earlierRef - }) - for (let j of earlier) { - t.ok(new Date(j.legs[0].departure) < earliestDep) - } - - const later = yield client.journeys(jungfernheide, münchenHbf, { - results: 3, - // todo: single journey ref? - laterThan: model.laterRef - }) - for (let j of later) { - t.ok(new Date(j.legs[0].departure) > latestDep) - } - t.end() })) diff --git a/test/insa.js b/test/insa.js index 99b48497..2ce5a0c8 100644 --- a/test/insa.js +++ b/test/insa.js @@ -11,6 +11,7 @@ const insaProfile = require('../p/insa') const products = require('../p/insa/products') const createValidate = require('./lib/validate-fptf-with') const testJourneysStationToStation = require('./lib/journeys-station-to-station') +const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') const isObj = o => o !== null && 'object' === typeof o && !Array.isArray(o) @@ -126,57 +127,14 @@ test('journeys: via works – with detour', co(function* (t) { })) test('earlier/later journeys', co(function* (t) { - const model = yield client.journeys(magdeburgHbf, magdeburgBuckau, { - results: 3, when + yield testEarlierLaterJourneys({ + test: t, + fetchJourneys: client.journeys, + validate, + fromId: magdeburgHbf, + toId: magdeburgBuckau }) - // 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) - - // when and earlierThan/laterThan should be mutually exclusive - t.throws(() => { - client.journeys(magdeburgHbf, magdeburgBuckau, { - when, earlierThan: model.earlierRef - }) - // silence rejections, we're only interested in exceptions - .catch(() => {}) - }) - t.throws(() => { - client.journeys(magdeburgHbf, magdeburgBuckau, { - when, laterThan: model.laterRef - }) - // silence rejections, we're only interested in exceptions - .catch(() => {}) - }) - - let earliestDep = Infinity, latestDep = -Infinity - for (let j of model) { - const dep = +new Date(j.legs[0].departure) - if (dep < earliestDep) earliestDep = dep - else if (dep > latestDep) latestDep = dep - } - - const earlier = yield client.journeys(magdeburgHbf, magdeburgBuckau, { - results: 3, - // todo: single journey ref? - earlierThan: model.earlierRef - }) - for (let j of earlier) { - t.ok(new Date(j.legs[0].departure) < earliestDep) - } - - const later = yield client.journeys(magdeburgHbf, magdeburgBuckau, { - results: 3, - // todo: single journey ref? - laterThan: model.laterRef - }) - for (let j of later) { - t.ok(new Date(j.legs[0].departure) > latestDep) - } - t.end() })) diff --git a/test/lib/earlier-later-journeys.js b/test/lib/earlier-later-journeys.js new file mode 100644 index 00000000..a9323815 --- /dev/null +++ b/test/lib/earlier-later-journeys.js @@ -0,0 +1,67 @@ +'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, { + results: 3, 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) + + // when and earlierThan/laterThan should be mutually exclusive + t.throws(() => { + fetchJourneys(fromId, toId, { + when, earlierThan: model.earlierRef + }) + // silence rejections, we're only interested in exceptions + .catch(() => {}) + }) + t.throws(() => { + fetchJourneys(fromId, toId, { + when, laterThan: model.laterRef + }) + // silence rejections, we're only interested in exceptions + .catch(() => {}) + }) + + let earliestDep = Infinity, latestDep = -Infinity + for (let j of model) { + 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) { + t.ok(new Date(j.legs[0].departure) < earliestDep) + } + + const later = yield fetchJourneys(fromId, toId, { + results: 3, + // todo: single journey ref? + laterThan: model.laterRef + }) + for (let j of later) { + t.ok(new Date(j.legs[0].departure) > latestDep) + } +}) + +module.exports = testEarlierLaterJourneys diff --git a/test/nahsh.js b/test/nahsh.js index 6b41e9b3..dfe1f0af 100644 --- a/test/nahsh.js +++ b/test/nahsh.js @@ -15,6 +15,7 @@ const { } = require('./lib/validators') const createValidate = require('./lib/validate-fptf-with') const testJourneysStationToStation = require('./lib/journeys-station-to-station') +const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') const when = createWhen('Europe/Berlin', 'de-DE') @@ -153,57 +154,14 @@ test('Husum to Lübeck Hbf with stopover at Kiel Hbf', co(function* (t) { })) test('earlier/later journeys, Kiel Hbf -> Flensburg', co(function* (t) { - const model = yield client.journeys(kielHbf, flensburg, { - results: 3, when + yield testEarlierLaterJourneys({ + test: t, + fetchJourneys: client.journeys, + validate, + fromId: kielHbf, + toId: flensburg }) - // 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) - - // when and earlierThan/laterThan should be mutually exclusive - t.throws(() => { - client.journeys(kielHbf, flensburg, { - when, earlierThan: model.earlierRef - }) - // silence rejections, we're only interested in exceptions - .catch(() => {}) - }) - t.throws(() => { - client.journeys(kielHbf, flensburg, { - when, laterThan: model.laterRef - }) - // silence rejections, we're only interested in exceptions - .catch(() => {}) - }) - - let earliestDep = Infinity, latestDep = -Infinity - for (let j of model) { - const dep = +new Date(j.legs[0].departure) - if (dep < earliestDep) earliestDep = dep - else if (dep > latestDep) latestDep = dep - } - - const earlier = yield client.journeys(kielHbf, flensburg, { - results: 3, - // todo: single journey ref? - earlierThan: model.earlierRef - }) - for (let j of earlier) { - t.ok(new Date(j.legs[0].departure) < earliestDep) - } - - const later = yield client.journeys(kielHbf, flensburg, { - results: 3, - // todo: single journey ref? - laterThan: model.laterRef - }) - for (let j of later) { - t.ok(new Date(j.legs[0].departure) > latestDep) - } - t.end() })) diff --git a/test/oebb.js b/test/oebb.js index 529b0a40..90083c06 100644 --- a/test/oebb.js +++ b/test/oebb.js @@ -15,6 +15,7 @@ const { } = require('./lib/validators') const createValidate = require('./lib/validate-fptf-with') const testJourneysStationToStation = require('./lib/journeys-station-to-station') +const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') const when = createWhen('Europe/Vienna', 'de-AT') @@ -187,57 +188,14 @@ test('journeys: via works – without detour', co(function* (t) { })) test('earlier/later journeys, Salzburg Hbf -> Wien Westbahnhof', co(function* (t) { - const model = yield client.journeys(salzburgHbf, wienWestbahnhof, { - results: 3, when + yield testEarlierLaterJourneys({ + test: t, + fetchJourneys: client.journeys, + validate, + fromId: salzburgHbf, + toId: wienWestbahnhof }) - // 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) - - // when and earlierThan/laterThan should be mutually exclusive - t.throws(() => { - client.journeys(salzburgHbf, wienWestbahnhof, { - when, earlierThan: model.earlierRef - }) - // silence rejections, we're only interested in exceptions - .catch(() => {}) - }) - t.throws(() => { - client.journeys(salzburgHbf, wienWestbahnhof, { - when, laterThan: model.laterRef - }) - // silence rejections, we're only interested in exceptions - .catch(() => {}) - }) - - let earliestDep = Infinity, latestDep = -Infinity - for (let j of model) { - const dep = +new Date(j.legs[0].departure) - if (dep < earliestDep) earliestDep = dep - else if (dep > latestDep) latestDep = dep - } - - const earlier = yield client.journeys(salzburgHbf, wienWestbahnhof, { - results: 3, - // todo: single journey ref? - earlierThan: model.earlierRef - }) - for (let j of earlier) { - t.ok(new Date(j.legs[0].departure) < earliestDep) - } - - const later = yield client.journeys(salzburgHbf, wienWestbahnhof, { - results: 3, - // todo: single journey ref? - laterThan: model.laterRef - }) - for (let j of later) { - t.ok(new Date(j.legs[0].departure) > latestDep) - } - t.end() })) diff --git a/test/vbb.js b/test/vbb.js index e49d5ef1..4b31a1dd 100644 --- a/test/vbb.js +++ b/test/vbb.js @@ -21,6 +21,7 @@ const { } = require('./lib/validators') const createValidate = require('./lib/validate-fptf-with') const testJourneysStationToStation = require('./lib/journeys-station-to-station') +const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') const when = createWhen('Europe/Berlin', 'de-DE') @@ -180,57 +181,14 @@ test('journeys – fails with no product', (t) => { }) test('earlier/later journeys', co(function* (t) { - const model = yield client.journeys(spichernstr, bismarckstr, { - results: 3, when + yield testEarlierLaterJourneys({ + test: t, + fetchJourneys: client.journeys, + validate, + fromId: spichernstr, + toId: bismarckstr }) - // 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) - - // when and earlierThan/laterThan should be mutually exclusive - t.throws(() => { - client.journeys(spichernstr, bismarckstr, { - when, earlierThan: model.earlierRef - }) - // silence rejections, we're only interested in exceptions - .catch(() => {}) - }) - t.throws(() => { - client.journeys(spichernstr, bismarckstr, { - when, laterThan: model.laterRef - }) - // silence rejections, we're only interested in exceptions - .catch(() => {}) - }) - - let earliestDep = Infinity, latestDep = -Infinity - for (let j of model) { - const dep = +new Date(j.legs[0].departure) - if (dep < earliestDep) earliestDep = dep - else if (dep > latestDep) latestDep = dep - } - - const earlier = yield client.journeys(spichernstr, bismarckstr, { - results: 3, - // todo: single journey ref? - earlierThan: model.earlierRef - }) - for (let j of earlier) { - t.ok(new Date(j.legs[0].departure) < earliestDep) - } - - const later = yield client.journeys(spichernstr, bismarckstr, { - results: 3, - // todo: single journey ref? - laterThan: model.laterRef - }) - for (let j of later) { - t.ok(new Date(j.legs[0].departure) > latestDep) - } - t.end() }))