From 75432fcf4cabb2be33dba0a9057625a07408bf96 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sun, 10 Jun 2018 20:44:12 +0200 Subject: [PATCH] CFL: tests :white_check_mark: --- p/cfl/example.js | 4 +- p/cfl/index.js | 3 +- test/cfl.js | 252 +++++++++++++++++++++++++++++++++++++++++++++++ test/index.js | 1 + 4 files changed, 257 insertions(+), 3 deletions(-) create mode 100644 test/cfl.js diff --git a/p/cfl/example.js b/p/cfl/example.js index f8799895..43ba94a4 100644 --- a/p/cfl/example.js +++ b/p/cfl/example.js @@ -3,7 +3,7 @@ const createClient = require('../..') const cflProfile = require('.') -const client = createClient(cflProfile) +const client = createClient(cflProfile, 'hafas-client-example') // from Mersch to Pfaffenthal-Kirchberg // client.journeys('009864348', '008200102', {results: 1}) @@ -24,7 +24,7 @@ client.departures('009864348', { duration: 5 }) // .then(([journey]) => { // const leg = journey.legs[0] -// return client.journeyLeg(leg.id, leg.line.name) +// return client.trip(leg.id, leg.line.name) // }) .then(data => { diff --git a/p/cfl/index.js b/p/cfl/index.js index e6dc6a79..dc165959 100644 --- a/p/cfl/index.js +++ b/p/cfl/index.js @@ -25,8 +25,9 @@ const cflProfile = { products: products, - journeyLeg: true, + trip: true, radar: true + // todo: reachableFrom? } module.exports = cflProfile; diff --git a/test/cfl.js b/test/cfl.js new file mode 100644 index 00000000..0b3e1c79 --- /dev/null +++ b/test/cfl.js @@ -0,0 +1,252 @@ +'use strict' + +const assert = require('assert') +const tapePromise = require('tape-promise').default +const tape = require('tape') +const isRoughlyEqual = require('is-roughly-equal') + +const {createWhen} = require('./lib/util') +const createClient = require('..') +const cflProfile = require('../p/cfl') +const products = require('../p/cfl/products') +const { + movement: _validateMovement +} = require('./lib/validators') +const createValidate = require('./lib/validate-fptf-with') +const testJourneysStationToStation = require('./lib/journeys-station-to-station') +const testJourneysStationToAddress = require('./lib/journeys-station-to-address') +const testJourneysStationToPoi = require('./lib/journeys-station-to-poi') +const testEarlierLaterJourneys = require('./lib/earlier-later-journeys') +const journeysFailsWithNoProduct = require('./lib/journeys-fails-with-no-product') +const testDepartures = require('./lib/departures') +const testArrivals = require('./lib/arrivals') + +const when = createWhen('Europe/Berlin', 'de-DE') + +const cfg = { + when, + products, + minLatitude: 47.24, + maxLatitude: 52.9, + minLongitude: -0.63, + maxLongitude: 14.07 +} + +const validateMovement = (val, m, name = 'movement') => { + // todo: fix this upstream + const withFakeLocation = Object.assign({}, m) + withFakeLocation.location = Object.assign({}, m.location, { + latitude: 50, + longitude: 12 + }) + _validateMovement(val, withFakeLocation, name) + + assert.ok(m.location.latitude <= 55, name + '.location.latitude is too small') + assert.ok(m.location.latitude >= 45, name + '.location.latitude is too large') + assert.ok(m.location.longitude >= 1, name + '.location.longitude is too small') + assert.ok(m.location.longitude <= 11, name + '.location.longitude is too small') +} + +const validate = createValidate(cfg, { + movement: validateMovement +}) + +const test = tapePromise(tape) +const client = createClient(cflProfile, 'public-transport/hafas-client:test') + +const ettelbruck = '9258199' +const luxembourg = '9217081' + +test('journeys – Ettelbruck to Luxembourg', async (t) => { + const res = await client.journeys(ettelbruck, luxembourg, { + results: 4, + departure: when, + stopovers: true + }) + + await testJourneysStationToStation({ + test: t, + res, + validate, + fromId: ettelbruck, + toId: luxembourg + }) + t.end() +}) + +// todo: journeys, only one product + +test('journeys – fails with no product', (t) => { + journeysFailsWithNoProduct({ + test: t, + fetchJourneys: client.journeys, + fromId: ettelbruck, + toId: luxembourg, + when, + products + }) + t.end() +}) + +test('Luxembourg to 9071 Ettelbruck, Rue des Romains 4', async (t) => { + const rueDeRomain = { + type: 'location', + address: '9071 Ettelbruck, Rue des Romains 4', + latitude: 49.847469, + longitude: 6.097608 + } + + const res = await client.journeys(luxembourg, rueDeRomain, { + results: 3, + departure: when + }) + + await testJourneysStationToAddress({ + test: t, + res, + validate, + fromId: luxembourg, + to: rueDeRomain + }) + t.end() +}) + +test('Luxembourg to Centre Hospitalier du Nord', async (t) => { + const centreHospitalier = { + type: 'location', + id: '140701020', + poi: true, + name: 'Ettelbruck, Centre Hospitalier du Nord', + latitude: 49.853096, + longitude: 6.094075 + } + const res = await client.journeys(luxembourg, centreHospitalier, { + results: 3, + departure: when + }) + + await testJourneysStationToPoi({ + test: t, + res, + validate, + fromId: luxembourg, + to: kloster + }) + t.end() +}) + +// todo: journeys: via works – with detour +// todo: without detour + +test('earlier/later journeys', async (t) => { + await testEarlierLaterJourneys({ + test: t, + fetchJourneys: client.journeys, + validate, + fromId: luxembourg, + toId: ettelbruck + }) + + t.end() +}) + +test('trip', async (t) => { + const { journeys } = await client.journeys(luxembourg, ettelbruck, { + results: 1, departure: when + }) + + const p = journeys[0].legs[0] + t.ok(p.tripId, 'precondition failed') + t.ok(p.line.name, 'precondition failed') + const trip = await client.trip(p.tripId, p.line.name, {when}) + + validate(t, trip, 'trip', 'trip') + t.end() +}) + +test('departures at Ettelbruck.', async (t) => { + const departures = await client.departures(ettelbruck, { + duration: 20, when + }) + + await testDepartures({ + test: t, + departures, + validate, + id: ettelbruck + }) + t.end() +}) + +test('arrivals at Ettelbruck.', async (t) => { + const arrivals = await client.arrivals(ettelbruck, { + duration: 20, when + }) + + await testArrivals({ + test: t, + arrivals, + validate, + id: ettelbruck + }) + t.end() +}) + +test('departures with station object', async (t) => { + const deps = await client.departures({ + type: 'station', + id: ettelbruck, + name: 'Ettelbruck', + location: { + type: 'location', + latitude: 49.847298, + longitude: 6.106157 + } + }, {when}) + + validate(t, deps, 'departures', 'departures') + t.end() +}) + +// todo: nearby + +test('locations named Ettelbruck', async (t) => { + const locations = await client.locations('Ettelbruck', { + results: 20 + }) + + validate(t, locations, 'locations', 'locations') + t.ok(locations.length <= 20) + + t.ok(locations.find(s => s.type === 'stop' || s.type === 'station')) + t.ok(locations.find(s => s.poi)) + t.ok(locations.some((loc) => { + if (loc.station && loc.station.id === ettelbruck) return true + return loc.id === ettelbruck + })) + + t.end() +}) + +test('stop Ettelbruck', async (t) => { + const s = await client.stop(ettelbruck) + + validate(t, s, ['stop', 'station'], 'stop') + t.equal(s.id, ettelbruck) + + t.end() +}) + +test('radar', async (t) => { + const vehicles = await client.radar({ + north: 49.9, + west: 6.05, + south: 49.8, + east: 6.15 + }, { + duration: 5 * 60, when, results: 10 + }) + + validate(t, vehicles, 'movements', 'vehicles') + t.end() +}) diff --git a/test/index.js b/test/index.js index e759f763..bd9b0e3e 100644 --- a/test/index.js +++ b/test/index.js @@ -10,5 +10,6 @@ require('./nahsh') require('./cmta') require('./sbahn-muenchen') require('./saarfahrplan') +require('./cfl') require('./throttle') require('./retry')