From 09fc50f5b37eefacef258ab6752c085fe94955da Mon Sep 17 00:00:00 2001 From: Jannis R Date: Sat, 16 Dec 2017 01:23:11 +0100 Subject: [PATCH] comply with FPTF 1.0.1, using validate-fptf --- p/db/modes.js | 2 +- p/vbb/modes.js | 2 +- package.json | 1 + parse/line.js | 10 ++++++++++ test/util.js | 29 ++++++++++++++++------------- test/vbb.js | 1 - 6 files changed, 29 insertions(+), 16 deletions(-) diff --git a/p/db/modes.js b/p/db/modes.js index 7ee4b8f5..be2252c3 100644 --- a/p/db/modes.js +++ b/p/db/modes.js @@ -47,7 +47,7 @@ const m = { bitmask: 64, name: 'Ferry', short: 'F', - mode: 'ferry', + mode: 'watercraft', product: 'ferry' }, subway: { diff --git a/p/vbb/modes.js b/p/vbb/modes.js index 388462b3..3e771793 100644 --- a/p/vbb/modes.js +++ b/p/vbb/modes.js @@ -42,7 +42,7 @@ const m = { category: 4, bitmask: 16, name: 'Fähre', - mode: 'ferry', + mode: 'watercraft', short: 'F', product: 'ferry' }, diff --git a/package.json b/package.json index fe28f309..39244a73 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "tap-spec": "^4.1.1", "tape": "^4.8.0", "tape-promise": "^2.0.1", + "validate-fptf": "^1.0.1", "vbb-parse-line": "^0.2.5", "vbb-stations-autocomplete": "^2.9.0" }, diff --git a/parse/line.js b/parse/line.js index a2619043..19f79680 100644 --- a/parse/line.js +++ b/parse/line.js @@ -1,15 +1,25 @@ 'use strict' +const slugg = require('slugg') + // todo: are p.number and p.line ever different? // todo: operator from p.oprX? const parseLine = (profile, p) => { if (!p) return null // todo: handle this upstream const res = { type: 'line', + id: null, name: p.line || p.name, public: true } + // We don't get a proper line id from the API, so we use the trip nr here. + // todo: find a better way + if (p.prodCtx && p.prodCtx.num) res.id = p.prodCtx.num + // This is terrible, but FPTF demands an ID. Let's pray for VBB to expose an ID. + else if (p.line) res.id = slugg(p.line.trim()) + else if (p.name) res.id = slugg(p.name.trim()) + if (p.cls) res.class = p.cls if (p.prodCtx && p.prodCtx.catCode !== undefined) { res.productCode = +p.prodCtx.catCode diff --git a/test/util.js b/test/util.js index 10c8dc52..90c3f69d 100644 --- a/test/util.js +++ b/test/util.js @@ -1,15 +1,20 @@ 'use strict' +const validateFptf = require('validate-fptf') const isRoughlyEqual = require('is-roughly-equal') const {DateTime} = require('luxon') const isValidWGS84 = require('is-coordinates') +const validateFptfWith = (t, item, allowedTypes, name) => { + try { + validateFptf.recurse(allowedTypes, item, name) + } catch (err) { + t.ifError(err) + } +} + const assertValidStation = (t, s, coordsOptional = false) => { - t.equal(s.type, 'station') - t.equal(typeof s.id, 'string') - t.ok(s.id) - t.equal(typeof s.name, 'string') - t.ok(s.name) + validateFptfWith(t, s, ['station'], 'station') if (!coordsOptional || (s.location !== null && s.location !== undefined)) { t.ok(s.location) @@ -18,18 +23,20 @@ const assertValidStation = (t, s, coordsOptional = false) => { } const assertValidPoi = (t, p) => { + assertValidLocation(t, p, true) + t.equal(typeof p.id, 'string') t.equal(typeof p.name, 'string') if (p.address !== null && p.address !== undefined) { t.equal(typeof p.address, 'string') t.ok(p.address) } - assertValidLocation(t, p, true) } const assertValidAddress = (t, a) => { - t.equal(typeof a.address, 'string') assertValidLocation(t, a, true) + + t.equal(typeof a.address, 'string') } const assertValidLocation = (t, l, coordsOptional = false) => { @@ -59,16 +66,12 @@ const assertValidLocation = (t, l, coordsOptional = false) => { } const validLineModes = [ - 'train', 'bus', 'ferry', 'taxi', 'gondola', 'aircraft', + 'train', 'bus', 'watercraft', 'taxi', 'gondola', 'aircraft', 'car', 'bicycle', 'walking' ] const assertValidLine = (t, l) => { - t.equal(l.type, 'line') - t.equal(typeof l.name, 'string') - t.ok(validLineModes.includes(l.mode), 'invalid mode ' + l.mode) - t.equal(typeof l.product, 'string') - t.equal(l.public, true) + validateFptfWith(t, l, ['line'], 'line') } const isValidDateTime = (w) => { diff --git a/test/vbb.js b/test/vbb.js index 1726fec8..a96faedc 100644 --- a/test/vbb.js +++ b/test/vbb.js @@ -10,7 +10,6 @@ const shorten = require('vbb-short-station-name') const createClient = require('..') const vbbProfile = require('../p/vbb') -const modes = require('../p/vbb/modes') const { assertValidStation: _assertValidStation, assertValidPoi,