Skip to content

Commit

Permalink
DST tests ✅
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Jul 12, 2020
1 parent 1376b18 commit f2d85f9
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 2 deletions.
3 changes: 3 additions & 0 deletions test/fixtures/daylight-saving-time/calendar.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date
sA,1,1,1,1,1,1,1,20191027,20191027
sB,1,1,1,1,1,1,1,20190331,20190331
1 change: 1 addition & 0 deletions test/fixtures/daylight-saving-time/calendar_dates.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
service_id,date,service_id
11 changes: 11 additions & 0 deletions test/fixtures/daylight-saving-time/expected.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
tripId: 'A1',
stops: ['1', '2'],
arrivals: [7140, 10740],
departures: [7260, 10860],
headwayBasedStarts: [],
headwayBasedEnds: [],
headwayBasedHeadways: [],
}
]
3 changes: 3 additions & 0 deletions test/fixtures/daylight-saving-time/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# test case: DST ↔ standard time

Check my [note about GTFS Time values](https://gist.github.com/derhuerst/574edc94981a21ef0ce90713f1cff7f6) for background information.
3 changes: 3 additions & 0 deletions test/fixtures/daylight-saving-time/routes.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
route_id
A
B
5 changes: 5 additions & 0 deletions test/fixtures/daylight-saving-time/stop_times.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trip_id,arrival_time,departure_time,stop_id,stop_sequence
A1,1:59,2:01,1,3
A1,2:59,3:01,2,5
B1,1:59,2:01,2,10
B1,2:59,3:01,1,11
3 changes: 3 additions & 0 deletions test/fixtures/daylight-saving-time/trips.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
route_id,service_id,trip_id
A,sA,A1
B,sB,B1
45 changes: 43 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const {DateTime} = require('luxon')
const test = require('tape')
const {createReadStream} = require('fs')
const {readJSON5Sync} = require('./lib')
const {readJSON5Sync, readFilesFromFixture} = require('./lib')

const readCsv = require('../read-csv')
const formatDate = require('../format-date')
Expand Down Expand Up @@ -122,7 +122,7 @@ test('lib/days-between', (t) => {
require('./read-stop-times')

const stopoversFixtures = readJSON5Sync(require.resolve('./fixtures/stopovers.json5'))
test('compute-stopovers', async (t) => {
test('compute-stopovers: works', async (t) => {
const stopovers = computeStopovers(readFile, 'Europe/Berlin', {
trip: t => t.trip_id === 'b-downtown-on-working-days',
})
Expand All @@ -132,6 +132,47 @@ test('compute-stopovers', async (t) => {
t.deepEqual(res, stopoversFixtures)
})

test('compute-stopovers: handles DST switch properly', async (t) => {
const readFile = readFilesFromFixture('daylight-saving-time')
const stopovers = computeStopovers(readFile, 'Europe/Berlin')

const res = []
for await (const s of stopovers) res.push(s)
t.deepEqual(res, [{
stop_id: '1',
trip_id: 'A1',
service_id: 'sA',
route_id: 'A',
start_of_trip: 1572127200, // 2019-10-27T00:00:00+02:00
arrival: 1572137940, // 2019-10-27T02:59:00+02:00
departure: 1572138060, // 2019-10-27T02:01:00+01:00
}, {
stop_id: '2',
trip_id: 'A1',
service_id: 'sA',
route_id: 'A',
start_of_trip: 1572127200, // 2019-10-27T00:00:00+02:00
arrival: 1572141540, // 2019-10-27T02:59:00+01:00
departure: 1572141660, // 2019-10-27T03:01:00+01:00
}, {
stop_id: '2',
trip_id: 'B1',
service_id: 'sB',
route_id: 'B',
start_of_trip: 1553986800, // 2019-03-31T00:00:00+01:00
arrival: 1553990340, // 2019-03-31T00:59:00+01:00
departure: 1553990460, // 2019-03-31T01:01:00+01:00
}, {
stop_id: '1',
trip_id: 'B1',
service_id: 'sB',
route_id: 'B',
start_of_trip: 1553986800, // 2019-03-31T00:00:00+01:00
arrival: 1553993940, // 2019-03-31T01:59:00+01:00
departure: 1553994060,// 2019-03-31T03:01:00+02:00
}])
})

test('compute-sorted-connections', async (t) => {
const sortedCons = await computeSortedConnections(readFile, 'Europe/Berlin')

Expand Down
7 changes: 7 additions & 0 deletions test/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

const JSON5 = require('json5')
const {readFileSync} = require('fs')
const {join: pJoin} = require('path')
const readCsv = require('../read-csv')

const readJSON5Sync = (path) => {
return JSON5.parse(readFileSync(path, {encoding: 'utf8'}))
}

const readFilesFromFixture = (fixture) => (file) => {
return readCsv(pJoin(__dirname, 'fixtures', fixture, file + '.csv'))
}

module.exports = {
readJSON5Sync,
readFilesFromFixture,
}
28 changes: 28 additions & 0 deletions test/read-stop-times.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const test = require('tape')
const {readFilesFromFixture} = require('./lib')

const readCsv = require('../read-csv')
const readStopTimes = require('../lib/read-stop-times')
Expand Down Expand Up @@ -86,3 +87,30 @@ test('read-stop-times works', async (t) => {
},
])
})

test('read-stop-times: handles DST switch properly', async (t) => {
const readFile = readFilesFromFixture('daylight-saving-time')
const stopTimes = readStopTimes(readFile, noFilter)

const res = []
for await (const st of stopTimes) res.push(st)
t.deepEqual(res, [{
tripId: 'A1',
stops: ['1', '2'],
arrivals: [7140, 10740],
departures: [7260, 10860],
headwayBasedStarts: [],
headwayBasedEnds: [],
headwayBasedHeadways: [],
}, {
tripId: 'B1',
stops: ['2', '1'],
arrivals: [7140, 10740],
departures: [7260, 10860],
headwayBasedStarts: [],
headwayBasedEnds: [],
headwayBasedHeadways: [],
}])

t.end()
})

0 comments on commit f2d85f9

Please sign in to comment.