From aced74078aef5a7ce6c30376ab6e4cfd7299aa59 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sun, 7 Jan 2018 19:01:22 +0100 Subject: [PATCH] =?UTF-8?q?fix=20=C3=96BB=20tests=20:white=5Fcheck=5Fmark:?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- test/oebb.js | 21 +++++++++++++++-- test/validate-line-without-mode.js | 38 ++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 test/validate-line-without-mode.js diff --git a/package.json b/package.json index fe3c998a..d69cae39 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "tap-spec": "^4.1.1", "tape": "^4.8.0", "tape-promise": "^2.0.1", - "validate-fptf": "^1.0.2", + "validate-fptf": "^1.2.0", "vbb-stations-autocomplete": "^2.11.0" }, "scripts": { diff --git a/test/oebb.js b/test/oebb.js index a9630c53..261cb548 100644 --- a/test/oebb.js +++ b/test/oebb.js @@ -6,6 +6,9 @@ const tapePromise = require('tape-promise').default const tape = require('tape') const co = require('co') const isRoughlyEqual = require('is-roughly-equal') +const validateFptf = require('validate-fptf') + +const validateLineWithoutMode = require('./validate-line-without-mode') const createClient = require('..') const oebbProfile = require('../p/oebb') @@ -15,7 +18,6 @@ const { assertValidPoi, assertValidAddress, assertValidLocation, - assertValidLine, assertValidStopover, hour, createWhen, assertValidWhen } = require('./util.js') @@ -91,6 +93,20 @@ const assertValidPrice = (t, p) => { } } +// todo: fix this upstream +// see https://github.com/derhuerst/hafas-client/blob/c6e558be217667f1bcdac4a605898eb75ea80374/p/oebb/products.js#L71 +const assertValidLine = (t, l) => { // with optional mode + const validators = Object.assign({}, validateFptf.defaultValidators, { + line: validateLineWithoutMode + }) + const recurse = validateFptf.createRecurse(validators) + try { + recurse(['line'], l, 'line') + } catch (err) { + t.ifError(err) + } +} + const test = tapePromise(tape) const client = createClient(oebbProfile) @@ -285,10 +301,11 @@ test('departures at Salzburg Hbf', co.wrap(function* (t) { test('nearby Salzburg Hbf', co.wrap(function* (t) { const salzburgHbfPosition = { + type: 'location', longitude: 13.045604, latitude: 47.812851 } - const nearby = yield client.nearby(salzburgHbfPosition.latitude, salzburgHbfPosition.longitude, { + const nearby = yield client.nearby(salzburgHbfPosition, { results: 2, distance: 400 }) diff --git a/test/validate-line-without-mode.js b/test/validate-line-without-mode.js new file mode 100644 index 00000000..71068e32 --- /dev/null +++ b/test/validate-line-without-mode.js @@ -0,0 +1,38 @@ +'use strict' + +const a = require('assert') +const is = require('@sindresorhus/is') + +const validateItem = require('validate-fptf/lib/item') +const validateReference = require('validate-fptf/lib/reference') + +// todo: this is copied code, DRY this up! +// see https://github.com/public-transport/validate-fptf/blob/373b4847ec9668c4a9ec9b0dbd50f8a70ffbe127/line.js +const validateLineWithoutMode = (validate, line, name) => { + validateItem(line, name) + + a.strictEqual(line.type, 'line', name + '.type must be `line`') + + validateReference(line.id, name + '.id') + + a.strictEqual(typeof line.name, 'string', name + '.name must be a string') + a.ok(line.name.length > 0, name + '.name can\'t be empty') + + // skipping line validation here + // see https://github.com/derhuerst/hafas-client/issues/8#issuecomment-355839965 + if (is.undefined(line.mode) || is.null(line.mode)) { + console.error(`ÖBB: Missing \`mode\` for line ${line.name} (at ${name}).`) + } + + if (!is.undefined(line.subMode)) { + a.fail(name + '.subMode is reserved an should not be used for now') + } + + // todo: routes + + if (!is.null(line.operator) && !is.undefined(line.operator)) { + validate(['operator'], line.operator, name + '.operator') + } +} + +module.exports = validateLineWithoutMode