-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
177 lines (155 loc) · 4.94 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// jshint camelcase:false
"use strict";
var test = require("tape");
var CURRENT_YEAR = new Date().getFullYear();
var loadCaltrainSchedule = require("./async");
var isStopAtStation = require("./lib/util").isStopAtStation;
var stations = require("./stations");
var getRouteName = require("./route-name");
loadCaltrainSchedule(function(err, getSchedule) {
if (err) {
throw err;
}
test("stations", function(t) {
stations.forEach(function(station) {
t.ok(station);
t.ok(station.id);
t.ok(station.name);
t.equal(stations.byId(station.id), station);
t.equal(stations.search(station.name), station);
});
t.end();
});
test("route-name", function(t) {
var VALID_ROUTE_NAMES = ["Local", "Limited", "Bullet"];
var routes = getSchedule.store.routes;
Object.keys(routes).forEach(function(key) {
var routeName = getRouteName(routes[key]);
t.assert(
getRouteName.KNOWN_ROUTE_NAMES.indexOf(routeName) !== -1,
"should be valid route name: " + routeName
)
})
t.end();
});
test("caltrain", function(t) {
var getNextStop = getSchedule({
from: "ctsf",
to: "ctsmat",
// Should be a weekend
date: new Date(CURRENT_YEAR, 11, 1, 12),
});
var stop = getNextStop();
t.assert(stop);
t.equal(stop.serviceDate.getFullYear(), CURRENT_YEAR);
t.equal(stop.serviceDate.getMonth(), 11);
t.equal(stop.serviceDate.getDate(), 1);
t.equal(stop.trip.route_id, stop.route.route_id);
t.equal(stop.trip.service_id, stop.service.service_id);
var firstTripStop = stop.tripStops[0];
var lastTripStop = stop.tripStops[stop.tripStops.length - 1];
t.equal(stop.trip.trip_id, firstTripStop.stopTime.trip_id);
t.equal(firstTripStop.stopTime.stop_id, "70012");
t.equal(firstTripStop.date.getFullYear(), CURRENT_YEAR);
t.equal(firstTripStop.date.getMonth(), 11);
t.equal(firstTripStop.date.getDate(), 1);
t.equal(firstTripStop.date.getHours(), 12);
t.equal(firstTripStop.date.getMinutes(), 4);
t.equal(lastTripStop.stopTime.stop_id, "70092");
t.equal(lastTripStop.date.getFullYear(), CURRENT_YEAR);
t.equal(lastTripStop.date.getMonth(), 11);
t.equal(lastTripStop.date.getDate(), 1);
t.equal(lastTripStop.date.getHours(), 12);
t.equal(lastTripStop.date.getMinutes(), 26);
t.assert(isStopAtStation(firstTripStop.stop, stop.fromStation));
t.assert(isStopAtStation(lastTripStop.stop, stop.toStation));
// Assert trip of stop times
stop.tripStops.forEach(function(tripStop) {
t.equal(tripStop.stopTime.trip_id, stop.trip.trip_id);
});
// Assert and station are same
stop.tripStops.forEach(function(tripStop) {
t.assert(isStopAtStation(tripStop.stop, tripStop.station));
});
// Assert order of dates
stop.tripStops.slice(1).forEach(function(tripStop, index) {
var prevTripStop = stop.tripStops[index];
t.ok(tripStop.date >= prevTripStop.date);
});
t.end();
});
test("get multiple route types", function(t) {
var getNextStop = getSchedule({
from: "ctsf",
to: "ctsj",
// Should be a weekday
date: new Date(CURRENT_YEAR, 11, 3, 12),
});
var seenRoutes = new Set();
for (var i = 0; i < 20; i++) {
var stop = getNextStop();
seenRoutes.add(stop.route.route_short_name);
}
// Should have seen local, limited, bullet
t.equal(seenRoutes.size, 3);
t.ok(seenRoutes.has("Local"));
t.ok(seenRoutes.has("Limited"));
t.ok(seenRoutes.has("Bullet"));
t.end();
});
test("sf->sj direction=sb", function(t) {
var getNextStop = getSchedule({
from: "ctsf",
to: "ctsj",
date: new Date(CURRENT_YEAR, 11, 1, 12),
});
for (var i = 0; i < 20; i++) {
var stop = getNextStop();
t.equal(stop.direction, "SB");
}
t.end();
});
test("sj->sf direction=nb", function(t) {
var getNextStop = getSchedule({
from: "ctsj",
to: "ctsf",
date: new Date(CURRENT_YEAR, 11, 1, 12),
});
for (var i = 0; i < 20; i++) {
var stop = getNextStop();
t.equal(stop.direction, "NB");
}
t.end();
});
test("get route from every station to every other station", function(t) {
var limitedStations = [
"ctat",
"ctbr",
"ctta",
"ctco",
];
stations.forEach(function(fromStation) {
if (limitedStations.indexOf(fromStation.id) !== -1) {
return;
}
stations.forEach(function(toStation) {
if (limitedStations.indexOf(toStation.id) !== -1) {
return;
}
if (fromStation === toStation) {
return;
}
else {
var getNextStop = getSchedule({
from: fromStation.id,
to: toStation.id,
date: new Date(CURRENT_YEAR, 11, 1, 12),
});
var stop = getNextStop();
t.assert(stop, "route from " + fromStation.id + " to " + toStation.id);
}
});
});
t.end();
});
});