E2E/integration tests: validate remarks

This commit is contained in:
Jannis R 2022-04-27 09:09:49 +02:00
parent dda36c2da0
commit 38cc9e9af8
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5

View file

@ -9,6 +9,17 @@ const {assertValidWhen} = require('./util')
const isObj = o => o !== null && 'object' === typeof o && !Array.isArray(o)
const is = val => val !== null && val !== undefined
const createValidateProducts = (cfg) => {
const validateProducts = (val, p, name = 'products') => {
a.ok(isObj(p), name + ' must be an object')
for (let product of cfg.products) {
const msg = `${name}[${product.id}] must be a boolean`
a.strictEqual(typeof p[product.id], 'boolean', msg)
}
}
return validateProducts
}
const createValidateStation = (cfg) => {
const validateStation = (val, s, name = 'station') => {
defaultValidators.station(val, s, name)
@ -16,11 +27,7 @@ const createValidateStation = (cfg) => {
if (!cfg.stationCoordsOptional) {
a.ok(is(s.location), `missing ${name}.location`)
}
a.ok(isObj(s.products), name + '.products must be an object')
for (let product of cfg.products) {
const msg = name + `.products[${product.id}] must be a boolean`
a.strictEqual(typeof s.products[product.id], 'boolean', msg)
}
val.products(val, s.products, name + '.products')
if ('lines' in s) {
a.ok(Array.isArray(s.lines), name + `.lines must be an array`)
@ -98,6 +105,55 @@ const createValidateLine = (cfg) => {
return validateLine
}
const validateRemark = (val, rem, name = 'remark') => {
a.ok(isObj(rem), name + ' must be an object')
a.strictEqual(typeof rem.id, 'string', name + '.id must be a string')
a.ok(rem.id, name + '.id must not be empty')
if (rem.summary !== null) {
a.strictEqual(typeof rem.summary, 'string', name + '.summary must be a string')
a.ok(rem.summary, name + '.summary must not be empty')
}
if (rem.text !== null) {
a.strictEqual(typeof rem.text, 'string', name + '.text must be a string')
a.ok(rem.text, name + '.text must not be empty')
}
if ('validFrom' in rem) {
a.strictEqual(typeof rem.validFrom, 'string', name + '.validFrom must be a string')
a.ok(Number.isInteger(Date.parse(rem.validFrom)), name + '.validFrom must be ISO 8601')
}
if ('validUntil' in rem) {
a.strictEqual(typeof rem.validUntil, 'string', name + '.validUntil must be a string')
a.ok(Number.isInteger(Date.parse(rem.validUntil)), name + '.validUntil must be ISO 8601')
}
if ('modified' in rem) {
a.strictEqual(typeof rem.modified, 'string', name + '.modified must be a string')
a.ok(Number.isInteger(Date.parse(rem.modified)), name + '.modified must be ISO 8601')
}
if ('products' in rem) {
val.products(val, rem.products, name + '.products')
}
if ('edges' in rem) {
a.ok(Array.isArray(rem.edges), name + '.edges must be an array')
for (let i = 0; i < rem.edges.length; i++) {
const e = rem.edges[i]
const n = `${name}.edges[${i}]`
a.ok(isObj(e), n + ' must be an object')
if (e.fromLocation !== null) {
val.location(val, e.fromLocation, n + '.fromLocation')
}
if (e.toLocation !== null) {
val.location(val, e.toLocation, n + '.toLocation')
}
}
}
if ('affectedLines' in rem) {
a.ok(Array.isArray(rem.affectedLines), name + '.affectedLines must be an array')
for (let i = 0; i < rem.affectedLines.length; i++) {
val.line(val, rem.affectedLines[i], `${name}.affectedLines[${i}]`)
}
}
}
const createValidateStopover = (cfg) => {
const validateStopover = (val, s, name = 'stopover') => {
if (
@ -508,6 +564,7 @@ const validateMovements = (val, ms, name = 'movements') => {
}
module.exports = {
products: createValidateProducts,
station: createValidateStation,
stop: () => validateStop,
location: () => validateLocation,
@ -515,6 +572,7 @@ module.exports = {
poi: () => validatePoi,
address: () => validateAddress,
line: createValidateLine,
remark: () => validateRemark,
stopover: createValidateStopover,
ticket: () => validateTicket,
journeyLeg: createValidateJourneyLeg,