From 66d869a3eba3d3fd303e66f808a5246f19ec7dd6 Mon Sep 17 00:00:00 2001 From: Julius Tens Date: Thu, 28 Dec 2017 22:57:22 +0100 Subject: [PATCH] make when timezone- and local-independent --- test/db.js | 22 ++++++++++++---------- test/util.js | 22 ++++++++++++---------- test/vbb.js | 22 ++++++++++++---------- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/test/db.js b/test/db.js index 317b3c65..36a0620b 100644 --- a/test/db.js +++ b/test/db.js @@ -16,9 +16,11 @@ const { assertValidLocation, assertValidLine, assertValidStopover, - when, isValidWhen + createWhen, assertValidWhen } = require('./util.js') +const when = createWhen('Europe/Berlin', 'de-DE') + const assertValidStationProducts = (t, p) => { t.ok(p) t.equal(typeof p.nationalExp, 'boolean') @@ -106,7 +108,7 @@ test('Berlin Jungfernheide to München Hbf', co.wrap(function* (t) { if (journey.origin.products) { assertValidProducts(t, journey.origin.products) } - t.ok(isValidWhen(journey.departure)) + assertValidWhen(t, journey.departure, when) assertValidStation(t, journey.destination) assertValidStationProducts(t, journey.origin.products) @@ -116,7 +118,7 @@ test('Berlin Jungfernheide to München Hbf', co.wrap(function* (t) { if (journey.destination.products) { assertValidProducts(t, journey.destination.products) } - t.ok(isValidWhen(journey.arrival)) + assertValidWhen(t, journey.arrival, when) t.ok(Array.isArray(journey.legs)) t.ok(journey.legs.length > 0, 'no legs') @@ -127,7 +129,7 @@ test('Berlin Jungfernheide to München Hbf', co.wrap(function* (t) { if (!(yield findStation(leg.origin.id))) { console.error('unknown station', leg.origin.id, leg.origin.name) } - t.ok(isValidWhen(leg.departure)) + assertValidWhen(t, leg.departure, when) t.equal(typeof leg.departurePlatform, 'string') assertValidStation(t, leg.destination) @@ -135,7 +137,7 @@ test('Berlin Jungfernheide to München Hbf', co.wrap(function* (t) { if (!(yield findStation(leg.destination.id))) { console.error('unknown station', leg.destination.id, leg.destination.name) } - t.ok(isValidWhen(leg.arrival)) + assertValidWhen(t, leg.arrival, when) t.equal(typeof leg.arrivalPlatform, 'string') assertValidLine(t, leg.line) @@ -166,8 +168,8 @@ test('Berlin Jungfernheide to Torfstraße 17', co.wrap(function* (t) { console.error('unknown station', leg.origin.id, leg.origin.name) } if (leg.origin.products) assertValidProducts(t, leg.origin.products) - t.ok(isValidWhen(leg.departure)) - t.ok(isValidWhen(leg.arrival)) + assertValidWhen(t, leg.departure, when) + assertValidWhen(t, leg.arrival, when) const d = leg.destination assertValidAddress(t, d) @@ -195,8 +197,8 @@ test('Berlin Jungfernheide to ATZE Musiktheater', co.wrap(function* (t) { console.error('unknown station', leg.origin.id, leg.origin.name) } if (leg.origin.products) assertValidProducts(t, leg.origin.products) - t.ok(isValidWhen(leg.departure)) - t.ok(isValidWhen(leg.arrival)) + assertValidWhen(t, leg.departure, when) + assertValidWhen(t, leg.arrival, when) const d = leg.destination assertValidPoi(t, d) @@ -220,7 +222,7 @@ test('departures at Berlin Jungfernheide', co.wrap(function* (t) { console.error('unknown station', dep.station.id, dep.station.name) } if (dep.station.products) assertValidProducts(t, dep.station.products) - t.ok(isValidWhen(dep.when)) + assertValidWhen(t, dep.when, when) } t.end() diff --git a/test/util.js b/test/util.js index 6a374bac..fbd0d8c5 100644 --- a/test/util.js +++ b/test/util.js @@ -98,18 +98,20 @@ const hour = 60 * 60 * 1000 const week = 7 * 24 * hour // next Monday 10 am -const when = DateTime.fromMillis(Date.now(), { - zone: 'Europe/Berlin', - locale: 'de-DE' -}).startOf('week').plus({weeks: 1, hours: 10}).toJSDate() -const isValidWhen = (w) => { - const ts = +new Date(w) +const createWhen = (timezone, locale) => { + return DateTime.fromMillis(Date.now(), { + zone: timezone, + locale, + }).startOf('week').plus({weeks: 1, hours: 10}).toJSDate() +} +const isValidWhen = (actual, expected) => { + const ts = +new Date(actual) if (Number.isNaN(ts)) return false - return isRoughlyEqual(12 * hour, +when, ts) + return isRoughlyEqual(12 * hour, +expected, ts) } -const assertValidWhen = (t, w) => { - t.ok(isValidWhen(w), 'invalid when') +const assertValidWhen = (t, actual, expected) => { + t.ok(isValidWhen(actual, expected), 'invalid when') } const assertValidTicket = (t, ti) => { @@ -151,6 +153,6 @@ module.exports = { assertValidLine, isValidDateTime, assertValidStopover, - hour, when, isValidWhen, assertValidWhen, + hour, createWhen, isValidWhen, assertValidWhen, assertValidTicket } diff --git a/test/vbb.js b/test/vbb.js index cc9eec9d..733792ee 100644 --- a/test/vbb.js +++ b/test/vbb.js @@ -17,11 +17,13 @@ const { assertValidLocation, assertValidLine: _assertValidLine, assertValidStopover, - hour, when, + hour, createWhen, assertValidWhen, assertValidTicket } = require('./util') +const when = createWhen('Europe/Berlin', 'de-DE') + const assertValidStation = (t, s, coordsOptional = false) => { _assertValidStation(t, s, coordsOptional) t.equal(s.name, shorten(s.name)) @@ -70,12 +72,12 @@ test('journeys – station to station', co.wrap(function* (t) { assertValidStationProducts(t, journey.origin.products) t.ok(journey.origin.name.indexOf('(Berlin)') === -1) t.strictEqual(journey.origin.id, spichernstr) - assertValidWhen(t, journey.departure) + assertValidWhen(t, journey.departure, when) assertValidStation(t, journey.destination) assertValidStationProducts(t, journey.destination.products) t.strictEqual(journey.destination.id, amrumerStr) - assertValidWhen(t, journey.arrival) + assertValidWhen(t, journey.arrival, when) t.ok(Array.isArray(journey.legs)) t.strictEqual(journey.legs.length, 1) @@ -87,12 +89,12 @@ test('journeys – station to station', co.wrap(function* (t) { assertValidStationProducts(t, leg.origin.products) t.ok(leg.origin.name.indexOf('(Berlin)') === -1) t.strictEqual(leg.origin.id, spichernstr) - assertValidWhen(t, leg.departure) + assertValidWhen(t, leg.departure, when) assertValidStation(t, leg.destination) assertValidStationProducts(t, leg.destination.products) t.strictEqual(leg.destination.id, amrumerStr) - assertValidWhen(t, leg.arrival) + assertValidWhen(t, leg.arrival, when) assertValidLine(t, leg.line) t.ok(findStation(leg.direction)) @@ -198,14 +200,14 @@ test('journeys – station to address', co.wrap(function* (t) { assertValidStation(t, leg.origin) assertValidStationProducts(t, leg.origin.products) - assertValidWhen(t, leg.departure) + assertValidWhen(t, leg.departure, when) const dest = leg.destination assertValidAddress(t, dest) t.strictEqual(dest.address, 'Torfstraße 17') t.ok(isRoughlyEqual(.0001, dest.latitude, 52.5416823)) t.ok(isRoughlyEqual(.0001, dest.longitude, 13.3491223)) - assertValidWhen(t, leg.arrival) + assertValidWhen(t, leg.arrival, when) t.end() })) @@ -225,14 +227,14 @@ test('journeys – station to POI', co.wrap(function* (t) { assertValidStation(t, leg.origin) assertValidStationProducts(t, leg.origin.products) - assertValidWhen(t, leg.departure) + assertValidWhen(t, leg.departure, when) const dest = leg.destination assertValidPoi(t, dest) t.strictEqual(dest.name, 'ATZE Musiktheater') t.ok(isRoughlyEqual(.0001, dest.latitude, 52.543333)) t.ok(isRoughlyEqual(.0001, dest.longitude, 13.351686)) - assertValidWhen(t, leg.arrival) + assertValidWhen(t, leg.arrival, when) t.end() })) @@ -253,7 +255,7 @@ test('departures', co.wrap(function* (t) { assertValidStationProducts(t, dep.station.products) t.strictEqual(dep.station.id, spichernstr) - assertValidWhen(t, dep.when) + assertValidWhen(t, dep.when, when) t.ok(findStation(dep.direction)) assertValidLine(t, dep.line) }