Node 6 💚

This commit is contained in:
Jannis R 2017-11-29 02:27:31 +01:00
parent 42f90b790e
commit 817f44ea39
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
3 changed files with 62 additions and 59 deletions

View file

@ -37,6 +37,7 @@
"vbb-translate-ids": "^3.1.0" "vbb-translate-ids": "^3.1.0"
}, },
"devDependencies": { "devDependencies": {
"co": "^4.6.0",
"db-stations": "^1.25.0", "db-stations": "^1.25.0",
"floordate": "^3.0.0", "floordate": "^3.0.0",
"is-roughly-equal": "^0.1.0", "is-roughly-equal": "^0.1.0",

View file

@ -3,6 +3,7 @@
const getStations = require('db-stations').full const getStations = require('db-stations').full
const tapePromise = require('tape-promise').default const tapePromise = require('tape-promise').default
const tape = require('tape') const tape = require('tape')
const co = require('co')
const isRoughlyEqual = require('is-roughly-equal') const isRoughlyEqual = require('is-roughly-equal')
const createClient = require('..') const createClient = require('..')
@ -61,8 +62,8 @@ const assertValidProducts = (t, p) => {
const test = tapePromise(tape) const test = tapePromise(tape)
const client = createClient(dbProfile) const client = createClient(dbProfile)
test('Berlin Jungfernheide to München Hbf', async (t) => { test('Berlin Jungfernheide to München Hbf', co.wrap(function* (t) {
const journeys = await client.journeys('8011167', '8000261', { const journeys = yield client.journeys('8011167', '8000261', {
when, passedStations: true when, passedStations: true
}) })
@ -70,7 +71,7 @@ test('Berlin Jungfernheide to München Hbf', async (t) => {
t.ok(journeys.length > 0, 'no journeys') t.ok(journeys.length > 0, 'no journeys')
for (let journey of journeys) { for (let journey of journeys) {
assertValidStation(t, journey.origin) assertValidStation(t, journey.origin)
if (!await findStation(journey.origin.id)) { if (!(yield findStation(journey.origin.id))) {
console.error('unknown station', journey.origin.id, journey.origin.name) console.error('unknown station', journey.origin.id, journey.origin.name)
} }
if (journey.origin.products) { if (journey.origin.products) {
@ -79,7 +80,7 @@ test('Berlin Jungfernheide to München Hbf', async (t) => {
t.ok(isValidWhen(journey.departure)) t.ok(isValidWhen(journey.departure))
assertValidStation(t, journey.destination) assertValidStation(t, journey.destination)
if (!await findStation(journey.origin.id)) { if (!(yield findStation(journey.origin.id))) {
console.error('unknown station', journey.destination.id, journey.destination.name) console.error('unknown station', journey.destination.id, journey.destination.name)
} }
if (journey.destination.products) { if (journey.destination.products) {
@ -92,14 +93,14 @@ test('Berlin Jungfernheide to München Hbf', async (t) => {
const part = journey.parts[0] const part = journey.parts[0]
assertValidStation(t, part.origin) assertValidStation(t, part.origin)
if (!await findStation(part.origin.id)) { if (!(yield findStation(part.origin.id))) {
console.error('unknown station', part.origin.id, part.origin.name) console.error('unknown station', part.origin.id, part.origin.name)
} }
t.ok(isValidWhen(part.departure)) t.ok(isValidWhen(part.departure))
t.equal(typeof part.departurePlatform, 'string') t.equal(typeof part.departurePlatform, 'string')
assertValidStation(t, part.destination) assertValidStation(t, part.destination)
if (!await findStation(part.destination.id)) { if (!(yield findStation(part.destination.id))) {
console.error('unknown station', part.destination.id, part.destination.name) console.error('unknown station', part.destination.id, part.destination.name)
} }
t.ok(isValidWhen(part.arrival)) t.ok(isValidWhen(part.arrival))
@ -112,10 +113,10 @@ test('Berlin Jungfernheide to München Hbf', async (t) => {
} }
t.end() t.end()
}) }))
test('Berlin Jungfernheide to Torfstraße 17', async (t) => { test('Berlin Jungfernheide to Torfstraße 17', co.wrap(function* (t) {
const journeys = await client.journeys('8011167', { const journeys = yield client.journeys('8011167', {
type: 'address', name: 'Torfstraße 17', type: 'address', name: 'Torfstraße 17',
latitude: 52.5416823, longitude: 13.3491223 latitude: 52.5416823, longitude: 13.3491223
}, {when}) }, {when})
@ -126,7 +127,7 @@ test('Berlin Jungfernheide to Torfstraße 17', async (t) => {
const part = journey.parts[journey.parts.length - 1] const part = journey.parts[journey.parts.length - 1]
assertValidStation(t, part.origin) assertValidStation(t, part.origin)
if (!await findStation(part.origin.id)) { if (!(yield findStation(part.origin.id))) {
console.error('unknown station', part.origin.id, part.origin.name) console.error('unknown station', part.origin.id, part.origin.name)
} }
if (part.origin.products) assertValidProducts(t, part.origin.products) if (part.origin.products) assertValidProducts(t, part.origin.products)
@ -140,10 +141,10 @@ test('Berlin Jungfernheide to Torfstraße 17', async (t) => {
t.ok(isRoughlyEqual(.0001, d.coordinates.longitude, 13.3491223)) t.ok(isRoughlyEqual(.0001, d.coordinates.longitude, 13.3491223))
t.end() t.end()
}) }))
test('Berlin Jungfernheide to ATZE Musiktheater', async (t) => { test('Berlin Jungfernheide to ATZE Musiktheater', co.wrap(function* (t) {
const journeys = await client.journeys('8011167', { const journeys = yield client.journeys('8011167', {
type: 'poi', name: 'ATZE Musiktheater', id: '991598902', type: 'poi', name: 'ATZE Musiktheater', id: '991598902',
latitude: 52.542417, longitude: 13.350437 latitude: 52.542417, longitude: 13.350437
}, {when}) }, {when})
@ -154,7 +155,7 @@ test('Berlin Jungfernheide to ATZE Musiktheater', async (t) => {
const part = journey.parts[journey.parts.length - 1] const part = journey.parts[journey.parts.length - 1]
assertValidStation(t, part.origin) assertValidStation(t, part.origin)
if (!await findStation(part.origin.id)) { if (!(yield findStation(part.origin.id))) {
console.error('unknown station', part.origin.id, part.origin.name) console.error('unknown station', part.origin.id, part.origin.name)
} }
if (part.origin.products) assertValidProducts(t, part.origin.products) if (part.origin.products) assertValidProducts(t, part.origin.products)
@ -168,17 +169,17 @@ test('Berlin Jungfernheide to ATZE Musiktheater', async (t) => {
t.ok(isRoughlyEqual(.0001, d.coordinates.longitude, 13.350402)) t.ok(isRoughlyEqual(.0001, d.coordinates.longitude, 13.350402))
t.end() t.end()
}) }))
test('departures at Berlin Jungfernheide', async (t) => { test('departures at Berlin Jungfernheide', co.wrap(function* (t) {
const deps = await client.departures('8011167', { const deps = yield client.departures('8011167', {
duration: 5, when duration: 5, when
}) })
t.ok(Array.isArray(deps)) t.ok(Array.isArray(deps))
for (let dep of deps) { for (let dep of deps) {
assertValidStation(t, dep.station) assertValidStation(t, dep.station)
if (!await findStation(dep.station.id)) { if (!(yield findStation(dep.station.id))) {
console.error('unknown station', dep.station.id, dep.station.name) console.error('unknown station', dep.station.id, dep.station.name)
} }
if (dep.station.products) assertValidProducts(t, dep.station.products) if (dep.station.products) assertValidProducts(t, dep.station.products)
@ -186,10 +187,10 @@ test('departures at Berlin Jungfernheide', async (t) => {
} }
t.end() t.end()
}) }))
test('nearby Berlin Jungfernheide', async (t) => { test('nearby Berlin Jungfernheide', co.wrap(function* (t) {
const nearby = await client.nearby(52.530273, 13.299433, { const nearby = yield client.nearby(52.530273, 13.299433, {
results: 2, distance: 400 results: 2, distance: 400
}) })
@ -201,10 +202,10 @@ test('nearby Berlin Jungfernheide', async (t) => {
t.ok(nearby[0].distance <= 100) t.ok(nearby[0].distance <= 100)
t.end() t.end()
}) }))
test('locations named Jungfernheide', async (t) => { test('locations named Jungfernheide', co.wrap(function* (t) {
const locations = await client.locations('Jungfernheide', { const locations = yield client.locations('Jungfernheide', {
results: 10 results: 10
}) })
@ -216,4 +217,4 @@ test('locations named Jungfernheide', async (t) => {
t.ok(locations.some(isJungfernheide)) t.ok(locations.some(isJungfernheide))
t.end() t.end()
}) }))

View file

@ -5,6 +5,7 @@ const isRoughlyEqual = require('is-roughly-equal')
const stations = require('vbb-stations-autocomplete') const stations = require('vbb-stations-autocomplete')
const tapePromise = require('tape-promise').default const tapePromise = require('tape-promise').default
const tape = require('tape') const tape = require('tape')
const co = require('co')
const createClient = require('..') const createClient = require('..')
const vbbProfile = require('../p/vbb') const vbbProfile = require('../p/vbb')
@ -30,8 +31,8 @@ const amrumerStr = '900000009101'
const spichernstr = '900000042101' const spichernstr = '900000042101'
const bismarckstr = '900000024201' const bismarckstr = '900000024201'
test('journeys  station to station', async (t) => { test('journeys  station to station', co.wrap(function* (t) {
const journeys = await client.journeys(spichernstr, amrumerStr, { const journeys = yield client.journeys(spichernstr, amrumerStr, {
results: 3, when, passedStations: true results: 3, when, passedStations: true
}) })
@ -71,10 +72,10 @@ test('journeys  station to station', async (t) => {
for (let passed of part.passed) assertValidStopover(t, passed) for (let passed of part.passed) assertValidStopover(t, passed)
} }
t.end() t.end()
}) }))
test('journeys  only subway', async (t) => { test('journeys  only subway', co.wrap(function* (t) {
const journeys = await client.journeys(spichernstr, bismarckstr, { const journeys = yield client.journeys(spichernstr, bismarckstr, {
results: 20, when, results: 20, when,
products: { products: {
suburban: false, suburban: false,
@ -100,11 +101,11 @@ test('journeys  only subway', async (t) => {
} }
} }
t.end() t.end()
}) }))
test('journeys  fails with no product', async (t) => { test('journeys  fails with no product', co.wrap(function* (t) {
try { try {
await client.journeys(spichernstr, bismarckstr, { yield client.journeys(spichernstr, bismarckstr, {
when, when,
products: { products: {
suburban: false, suburban: false,
@ -120,17 +121,17 @@ test('journeys  fails with no product', async (t) => {
t.ok(err, 'error thrown') t.ok(err, 'error thrown')
t.end() t.end()
} }
}) }))
test('journey part details', async (t) => { test('journey part details', co.wrap(function* (t) {
const journeys = await client.journeys(spichernstr, amrumerStr, { const journeys = yield client.journeys(spichernstr, amrumerStr, {
results: 1, when results: 1, when
}) })
const p = journeys[0].parts[0] const p = journeys[0].parts[0]
t.ok(p.id, 'precondition failed') t.ok(p.id, 'precondition failed')
t.ok(p.line.name, 'precondition failed') t.ok(p.line.name, 'precondition failed')
const part = await client.journeyPart(p.id, p.line.name, {when}) const part = yield client.journeyPart(p.id, p.line.name, {when})
t.equal(typeof part.id, 'string') t.equal(typeof part.id, 'string')
t.ok(part.id) t.ok(part.id)
@ -144,12 +145,12 @@ test('journey part details', async (t) => {
for (let passed of part.passed) assertValidStopover(t, passed) for (let passed of part.passed) assertValidStopover(t, passed)
t.end() t.end()
}) }))
test('journeys  station to address', async (t) => { test('journeys  station to address', co.wrap(function* (t) {
const journeys = await client.journeys(spichernstr, { const journeys = yield client.journeys(spichernstr, {
type: 'address', name: 'Torfstraße 17', type: 'address', name: 'Torfstraße 17',
latitude: 52.5416823, longitude: 13.3491223 latitude: 52.5416823, longitude: 13.3491223
}, {results: 1, when}) }, {results: 1, when})
@ -170,12 +171,12 @@ test('journeys  station to address', async (t) => {
assertValidWhen(t, part.arrival) assertValidWhen(t, part.arrival)
t.end() t.end()
}) }))
test('journeys  station to POI', async (t) => { test('journeys  station to POI', co.wrap(function* (t) {
const journeys = await client.journeys(spichernstr, { const journeys = yield client.journeys(spichernstr, {
type: 'poi', name: 'ATZE Musiktheater', id: 9980720, type: 'poi', name: 'ATZE Musiktheater', id: 9980720,
latitude: 52.543333, longitude: 13.351686 latitude: 52.543333, longitude: 13.351686
}, {results: 1, when}) }, {results: 1, when})
@ -196,12 +197,12 @@ test('journeys  station to POI', async (t) => {
assertValidWhen(t, part.arrival) assertValidWhen(t, part.arrival)
t.end() t.end()
}) }))
test('departures', async (t) => { test('departures', co.wrap(function* (t) {
const deps = await client.departures(spichernstr, {duration: 5, when}) const deps = yield client.departures(spichernstr, {duration: 5, when})
t.ok(Array.isArray(deps)) t.ok(Array.isArray(deps))
t.deepEqual(deps, deps.sort((a, b) => t.when > b.when)) t.deepEqual(deps, deps.sort((a, b) => t.when > b.when))
@ -218,22 +219,22 @@ test('departures', async (t) => {
assertValidLine(t, dep.line) assertValidLine(t, dep.line)
} }
t.end() t.end()
}) }))
// todo // todo
test.skip('departures at 7-digit station', async (t) => { test.skip('departures at 7-digit station', co.wrap(function* (t) {
const eisenach = '8010097' // see derhuerst/vbb-hafas#22 const eisenach = '8010097' // see derhuerst/vbb-hafas#22
await client.departures(eisenach, {when}) yield client.departures(eisenach, {when})
t.pass('did not fail') t.pass('did not fail')
t.end() t.end()
}) }))
test('nearby', async (t) => { test('nearby', co.wrap(function* (t) {
// Berliner Str./Bundesallee // Berliner Str./Bundesallee
const nearby = await client.nearby(52.4873452, 13.3310411, {distance: 200}) const nearby = yield client.nearby(52.4873452, 13.3310411, {distance: 200})
t.ok(Array.isArray(nearby)) t.ok(Array.isArray(nearby))
for (let n of nearby) assertValidLocation(t, n, false) for (let n of nearby) assertValidLocation(t, n, false)
@ -249,12 +250,12 @@ test('nearby', async (t) => {
t.ok(nearby[1].distance < 200) t.ok(nearby[1].distance < 200)
t.end() t.end()
}) }))
test('locations', async (t) => { test('locations', co.wrap(function* (t) {
const locations = await client.locations('Alexanderplatz', {results: 10}) const locations = yield client.locations('Alexanderplatz', {results: 10})
t.ok(Array.isArray(locations)) t.ok(Array.isArray(locations))
t.ok(locations.length > 0) t.ok(locations.length > 0)
@ -265,12 +266,12 @@ test('locations', async (t) => {
t.ok(locations.find((s) => s.type === 'address')) t.ok(locations.find((s) => s.type === 'address'))
t.end() t.end()
}) }))
test('radar', async (t) => { test('radar', co.wrap(function* (t) {
const vehicles = await client.radar(52.52411, 13.41002, 52.51942, 13.41709, { const vehicles = yield client.radar(52.52411, 13.41002, 52.51942, 13.41709, {
duration: 5 * 60, when duration: 5 * 60, when
}) })
@ -317,4 +318,4 @@ test('radar', async (t) => {
} }
} }
t.end() t.end()
}) }))