make when timezone- and local-independent

This commit is contained in:
Julius Tens 2017-12-28 22:57:22 +01:00
parent 0de6776815
commit 66d869a3eb
3 changed files with 36 additions and 30 deletions

View file

@ -16,9 +16,11 @@ const {
assertValidLocation, assertValidLocation,
assertValidLine, assertValidLine,
assertValidStopover, assertValidStopover,
when, isValidWhen createWhen, assertValidWhen
} = require('./util.js') } = require('./util.js')
const when = createWhen('Europe/Berlin', 'de-DE')
const assertValidStationProducts = (t, p) => { const assertValidStationProducts = (t, p) => {
t.ok(p) t.ok(p)
t.equal(typeof p.nationalExp, 'boolean') 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) { if (journey.origin.products) {
assertValidProducts(t, journey.origin.products) assertValidProducts(t, journey.origin.products)
} }
t.ok(isValidWhen(journey.departure)) assertValidWhen(t, journey.departure, when)
assertValidStation(t, journey.destination) assertValidStation(t, journey.destination)
assertValidStationProducts(t, journey.origin.products) assertValidStationProducts(t, journey.origin.products)
@ -116,7 +118,7 @@ test('Berlin Jungfernheide to München Hbf', co.wrap(function* (t) {
if (journey.destination.products) { if (journey.destination.products) {
assertValidProducts(t, 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(Array.isArray(journey.legs))
t.ok(journey.legs.length > 0, 'no 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))) { if (!(yield findStation(leg.origin.id))) {
console.error('unknown station', leg.origin.id, leg.origin.name) 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') t.equal(typeof leg.departurePlatform, 'string')
assertValidStation(t, leg.destination) 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))) { if (!(yield findStation(leg.destination.id))) {
console.error('unknown station', leg.destination.id, leg.destination.name) 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') t.equal(typeof leg.arrivalPlatform, 'string')
assertValidLine(t, leg.line) 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) console.error('unknown station', leg.origin.id, leg.origin.name)
} }
if (leg.origin.products) assertValidProducts(t, leg.origin.products) if (leg.origin.products) assertValidProducts(t, leg.origin.products)
t.ok(isValidWhen(leg.departure)) assertValidWhen(t, leg.departure, when)
t.ok(isValidWhen(leg.arrival)) assertValidWhen(t, leg.arrival, when)
const d = leg.destination const d = leg.destination
assertValidAddress(t, d) 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) console.error('unknown station', leg.origin.id, leg.origin.name)
} }
if (leg.origin.products) assertValidProducts(t, leg.origin.products) if (leg.origin.products) assertValidProducts(t, leg.origin.products)
t.ok(isValidWhen(leg.departure)) assertValidWhen(t, leg.departure, when)
t.ok(isValidWhen(leg.arrival)) assertValidWhen(t, leg.arrival, when)
const d = leg.destination const d = leg.destination
assertValidPoi(t, d) 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) console.error('unknown station', dep.station.id, dep.station.name)
} }
if (dep.station.products) assertValidProducts(t, dep.station.products) if (dep.station.products) assertValidProducts(t, dep.station.products)
t.ok(isValidWhen(dep.when)) assertValidWhen(t, dep.when, when)
} }
t.end() t.end()

View file

@ -98,18 +98,20 @@ const hour = 60 * 60 * 1000
const week = 7 * 24 * hour const week = 7 * 24 * hour
// next Monday 10 am // next Monday 10 am
const when = DateTime.fromMillis(Date.now(), { const createWhen = (timezone, locale) => {
zone: 'Europe/Berlin', return DateTime.fromMillis(Date.now(), {
locale: 'de-DE' zone: timezone,
}).startOf('week').plus({weeks: 1, hours: 10}).toJSDate() locale,
const isValidWhen = (w) => { }).startOf('week').plus({weeks: 1, hours: 10}).toJSDate()
const ts = +new Date(w) }
const isValidWhen = (actual, expected) => {
const ts = +new Date(actual)
if (Number.isNaN(ts)) return false if (Number.isNaN(ts)) return false
return isRoughlyEqual(12 * hour, +when, ts) return isRoughlyEqual(12 * hour, +expected, ts)
} }
const assertValidWhen = (t, w) => { const assertValidWhen = (t, actual, expected) => {
t.ok(isValidWhen(w), 'invalid when') t.ok(isValidWhen(actual, expected), 'invalid when')
} }
const assertValidTicket = (t, ti) => { const assertValidTicket = (t, ti) => {
@ -151,6 +153,6 @@ module.exports = {
assertValidLine, assertValidLine,
isValidDateTime, isValidDateTime,
assertValidStopover, assertValidStopover,
hour, when, isValidWhen, assertValidWhen, hour, createWhen, isValidWhen, assertValidWhen,
assertValidTicket assertValidTicket
} }

View file

@ -17,11 +17,13 @@ const {
assertValidLocation, assertValidLocation,
assertValidLine: _assertValidLine, assertValidLine: _assertValidLine,
assertValidStopover, assertValidStopover,
hour, when, hour, createWhen,
assertValidWhen, assertValidWhen,
assertValidTicket assertValidTicket
} = require('./util') } = require('./util')
const when = createWhen('Europe/Berlin', 'de-DE')
const assertValidStation = (t, s, coordsOptional = false) => { const assertValidStation = (t, s, coordsOptional = false) => {
_assertValidStation(t, s, coordsOptional) _assertValidStation(t, s, coordsOptional)
t.equal(s.name, shorten(s.name)) 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) assertValidStationProducts(t, journey.origin.products)
t.ok(journey.origin.name.indexOf('(Berlin)') === -1) t.ok(journey.origin.name.indexOf('(Berlin)') === -1)
t.strictEqual(journey.origin.id, spichernstr) t.strictEqual(journey.origin.id, spichernstr)
assertValidWhen(t, journey.departure) assertValidWhen(t, journey.departure, when)
assertValidStation(t, journey.destination) assertValidStation(t, journey.destination)
assertValidStationProducts(t, journey.destination.products) assertValidStationProducts(t, journey.destination.products)
t.strictEqual(journey.destination.id, amrumerStr) t.strictEqual(journey.destination.id, amrumerStr)
assertValidWhen(t, journey.arrival) assertValidWhen(t, journey.arrival, when)
t.ok(Array.isArray(journey.legs)) t.ok(Array.isArray(journey.legs))
t.strictEqual(journey.legs.length, 1) t.strictEqual(journey.legs.length, 1)
@ -87,12 +89,12 @@ test('journeys  station to station', co.wrap(function* (t) {
assertValidStationProducts(t, leg.origin.products) assertValidStationProducts(t, leg.origin.products)
t.ok(leg.origin.name.indexOf('(Berlin)') === -1) t.ok(leg.origin.name.indexOf('(Berlin)') === -1)
t.strictEqual(leg.origin.id, spichernstr) t.strictEqual(leg.origin.id, spichernstr)
assertValidWhen(t, leg.departure) assertValidWhen(t, leg.departure, when)
assertValidStation(t, leg.destination) assertValidStation(t, leg.destination)
assertValidStationProducts(t, leg.destination.products) assertValidStationProducts(t, leg.destination.products)
t.strictEqual(leg.destination.id, amrumerStr) t.strictEqual(leg.destination.id, amrumerStr)
assertValidWhen(t, leg.arrival) assertValidWhen(t, leg.arrival, when)
assertValidLine(t, leg.line) assertValidLine(t, leg.line)
t.ok(findStation(leg.direction)) t.ok(findStation(leg.direction))
@ -198,14 +200,14 @@ test('journeys  station to address', co.wrap(function* (t) {
assertValidStation(t, leg.origin) assertValidStation(t, leg.origin)
assertValidStationProducts(t, leg.origin.products) assertValidStationProducts(t, leg.origin.products)
assertValidWhen(t, leg.departure) assertValidWhen(t, leg.departure, when)
const dest = leg.destination const dest = leg.destination
assertValidAddress(t, dest) assertValidAddress(t, dest)
t.strictEqual(dest.address, 'Torfstraße 17') t.strictEqual(dest.address, 'Torfstraße 17')
t.ok(isRoughlyEqual(.0001, dest.latitude, 52.5416823)) t.ok(isRoughlyEqual(.0001, dest.latitude, 52.5416823))
t.ok(isRoughlyEqual(.0001, dest.longitude, 13.3491223)) t.ok(isRoughlyEqual(.0001, dest.longitude, 13.3491223))
assertValidWhen(t, leg.arrival) assertValidWhen(t, leg.arrival, when)
t.end() t.end()
})) }))
@ -225,14 +227,14 @@ test('journeys  station to POI', co.wrap(function* (t) {
assertValidStation(t, leg.origin) assertValidStation(t, leg.origin)
assertValidStationProducts(t, leg.origin.products) assertValidStationProducts(t, leg.origin.products)
assertValidWhen(t, leg.departure) assertValidWhen(t, leg.departure, when)
const dest = leg.destination const dest = leg.destination
assertValidPoi(t, dest) assertValidPoi(t, dest)
t.strictEqual(dest.name, 'ATZE Musiktheater') t.strictEqual(dest.name, 'ATZE Musiktheater')
t.ok(isRoughlyEqual(.0001, dest.latitude, 52.543333)) t.ok(isRoughlyEqual(.0001, dest.latitude, 52.543333))
t.ok(isRoughlyEqual(.0001, dest.longitude, 13.351686)) t.ok(isRoughlyEqual(.0001, dest.longitude, 13.351686))
assertValidWhen(t, leg.arrival) assertValidWhen(t, leg.arrival, when)
t.end() t.end()
})) }))
@ -253,7 +255,7 @@ test('departures', co.wrap(function* (t) {
assertValidStationProducts(t, dep.station.products) assertValidStationProducts(t, dep.station.products)
t.strictEqual(dep.station.id, spichernstr) t.strictEqual(dep.station.id, spichernstr)
assertValidWhen(t, dep.when) assertValidWhen(t, dep.when, when)
t.ok(findStation(dep.direction)) t.ok(findStation(dep.direction))
assertValidLine(t, dep.line) assertValidLine(t, dep.line)
} }