mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 23:29:35 +02:00
tests: isValidWhen -> assertValidWhen
This commit is contained in:
parent
196681513c
commit
06e759966c
2 changed files with 13 additions and 13 deletions
|
@ -2,9 +2,11 @@
|
||||||
|
|
||||||
const isRoughlyEqual = require('is-roughly-equal')
|
const isRoughlyEqual = require('is-roughly-equal')
|
||||||
const {DateTime} = require('luxon')
|
const {DateTime} = require('luxon')
|
||||||
|
const a = require('assert')
|
||||||
|
|
||||||
const hour = 60 * 60 * 1000
|
const hour = 60 * 60 * 1000
|
||||||
const week = 7 * 24 * hour
|
const day = 24 * hour
|
||||||
|
const week = 7 * day
|
||||||
|
|
||||||
// next Monday 10 am
|
// next Monday 10 am
|
||||||
const createWhen = (timezone, locale) => {
|
const createWhen = (timezone, locale) => {
|
||||||
|
@ -14,13 +16,13 @@ const createWhen = (timezone, locale) => {
|
||||||
}).startOf('week').plus({weeks: 1, hours: 10}).toJSDate()
|
}).startOf('week').plus({weeks: 1, hours: 10}).toJSDate()
|
||||||
}
|
}
|
||||||
|
|
||||||
const isValidWhen = (actual, expected) => {
|
const assertValidWhen = (actual, expected, name) => {
|
||||||
const ts = +new Date(actual)
|
const ts = +new Date(actual)
|
||||||
if (Number.isNaN(ts)) return false
|
a.ok(!Number.isNaN(ts), name + ' is not parsable by Date')
|
||||||
// the timestamps might be from long-distance trains
|
// the timestamps might be from long-distance trains
|
||||||
return isRoughlyEqual(14 * hour, +expected, ts)
|
a.ok(isRoughlyEqual(day, +expected, ts), name + ' is out of range')
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
hour, createWhen, isValidWhen
|
hour, createWhen, assertValidWhen
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ const a = require('assert')
|
||||||
const {defaultValidators} = require('validate-fptf')
|
const {defaultValidators} = require('validate-fptf')
|
||||||
const anyOf = require('validate-fptf/lib/any-of')
|
const anyOf = require('validate-fptf/lib/any-of')
|
||||||
|
|
||||||
const {isValidWhen} = require('./util')
|
const {assertValidWhen} = require('./util')
|
||||||
|
|
||||||
const isObj = o => o !== null && 'object' === typeof o && !Array.isArray(o)
|
const isObj = o => o !== null && 'object' === typeof o && !Array.isArray(o)
|
||||||
const is = val => val !== null && val !== undefined
|
const is = val => val !== null && val !== undefined
|
||||||
|
@ -80,11 +80,11 @@ const createValidateStopover = (cfg) => {
|
||||||
const validateStopover = (val, s, name = 'stopover') => {
|
const validateStopover = (val, s, name = 'stopover') => {
|
||||||
if (is(s.arrival)) {
|
if (is(s.arrival)) {
|
||||||
val.date(val, s.arrival, name + '.arrival')
|
val.date(val, s.arrival, name + '.arrival')
|
||||||
a.ok(isValidWhen(s.arrival, cfg.when), name + '.arrival is invalid')
|
assertValidWhen(s.arrival, cfg.when, name)
|
||||||
}
|
}
|
||||||
if (is(s.departure)) {
|
if (is(s.departure)) {
|
||||||
val.date(val, s.departure, name + '.departure')
|
val.date(val, s.departure, name + '.departure')
|
||||||
a.ok(isValidWhen(s.departure, cfg.when), name + '.departure is invalid')
|
assertValidWhen(s.departure, cfg.when, name)
|
||||||
}
|
}
|
||||||
if (!is(s.arrival) && !is(s.departure)) {
|
if (!is(s.arrival) && !is(s.departure)) {
|
||||||
a.fail(name + ' contains neither arrival nor departure')
|
a.fail(name + ' contains neither arrival nor departure')
|
||||||
|
@ -153,12 +153,10 @@ const createValidateJourneyLeg = (cfg) => {
|
||||||
defaultValidators.journeyLeg(val, withFakeScheduleAndOperator, name)
|
defaultValidators.journeyLeg(val, withFakeScheduleAndOperator, name)
|
||||||
|
|
||||||
if (leg.arrival !== null) {
|
if (leg.arrival !== null) {
|
||||||
const msg = name + '.arrival is invalid'
|
assertValidWhen(leg.arrival, cfg.when, name + '.arrival')
|
||||||
a.ok(isValidWhen(leg.arrival, cfg.when), msg)
|
|
||||||
}
|
}
|
||||||
if (leg.departure !== null) {
|
if (leg.departure !== null) {
|
||||||
const msg = name + '.departure is invalid'
|
assertValidWhen(leg.departure, cfg.when, name + '.departure')
|
||||||
a.ok(isValidWhen(leg.departure, cfg.when), msg)
|
|
||||||
}
|
}
|
||||||
// todo: leg.arrivalPlatform !== null
|
// todo: leg.arrivalPlatform !== null
|
||||||
if (is(leg.arrivalPlatform)) {
|
if (is(leg.arrivalPlatform)) {
|
||||||
|
@ -226,7 +224,7 @@ const createValidateDeparture = (cfg) => {
|
||||||
|
|
||||||
val.station(val, dep.station, name + '.station')
|
val.station(val, dep.station, name + '.station')
|
||||||
|
|
||||||
a.ok(isValidWhen(dep.when, cfg.when), name + '.when is invalid')
|
assertValidWhen(dep.when, cfg.when, name)
|
||||||
if (dep.delay !== null) {
|
if (dep.delay !== null) {
|
||||||
const msg = name + '.delay must be a number'
|
const msg = name + '.delay must be a number'
|
||||||
a.strictEqual(typeof dep.delay, 'number', msg)
|
a.strictEqual(typeof dep.delay, 'number', msg)
|
||||||
|
|
Loading…
Add table
Reference in a new issue