forked from trainline-eu/stations
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_data.rb
481 lines (409 loc) · 16 KB
/
test_data.rb
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
require "csv"
require "minitest/autorun"
require "set"
require "stringex"
require_relative "lib/constants"
STATIONS = CSV.read("stations.csv", Constants::CSV_PARAMETERS)
STATIONS_BY_ID = STATIONS.inject({}) { |hash, station| hash[station["id"]] = station; hash }
ALIASES = {}
CHILDREN = {}
CHILDREN_ENABLED_COUNT = Hash.new(0)
SLUG_COUNT = {}
STATIONS.each { |row| ALIASES[row["id"]] = []}
STATIONS.each { |row| CHILDREN[row["id"]] = [] }
STATIONS.each { |row| SLUG_COUNT["#{row["slug"]}_#{row["country"]}"] = 0 }
def has_enabled_carrier(row)
row["atoc_is_enabled"] == "t" ||
row["benerail_is_enabled"] == "t" ||
row["busbud_is_enabled"] == "t" ||
row["db_is_enabled"] == "t" ||
row["hkx_is_enabled"] == "t" ||
row["idtgv_is_enabled"] == "t" ||
row["ntv_is_enabled"] == "t" ||
row["ouigo_is_enabled"] == "t" ||
row["renfe_is_enabled"] == "t" ||
row["sncf_is_enabled"] == "t" ||
row["trenitalia_is_enabled"] == "t"
end
def has_carrier_id(row)
!row["atoc_id"].nil? ||
!row["benerail_id"].nil? ||
!row["busbud_id"].nil? ||
!row["db_id"].nil? ||
!row["hkx_id"].nil? ||
!row["idtgv_id"].nil? ||
!row["ntv_id"].nil? ||
!row["ouigo_id"].nil? ||
!row["renfe_id"].nil? ||
!row["sncf_id"].nil? ||
!row["trenitalia_id"].nil?
end
def has_any_id(row)
has_carrier_id(row) ||
!row["uic"].nil? ||
!row["uic8_sncf"].nil? ||
!row["sncf_tvs_id"].nil? ||
!row["trenitalia_rtvt_id"].nil?
end
STATIONS.each do |row|
if row["same_as"]
ALIASES[row["same_as"]] << row
end
if row["parent_station_id"]
CHILDREN[row["parent_station_id"]] << row
if has_enabled_carrier(row) == "t"
CHILDREN_ENABLED_COUNT[row["parent_station_id"]] += 1
end
end
SLUG_COUNT["#{row["slug"]}_#{row["country"]}"] += 1
end
def slugify(name)
name.gsub(/[\/\.]/,"-").to_ascii.to_url
end
def fold(name)
name.to_ascii
.downcase
.gsub(/[^a-z]/, " ")
.gsub(/\s+/, " ")
.strip
end
class StationsTest < Minitest::Test
def test_is_station_useful
STATIONS.each do |row|
if CHILDREN[row["id"]].empty?
assert has_any_id(row), "Station #{row["name"]} (#{row["id"]}) is useless and should be removed"
end
end
end
def test_number_columns
nb_columns = 57
STATIONS.each { |row| assert_equal nb_columns, row.size, "Wrong number of columns #{row["size"]} for station #{row["id"]}" }
end
def test_station_has_name
STATIONS.each do |row|
assert row["name"], "Station #{row["name"]} (#{row["id"]}) does not have a name"
end
end
def test_enabled_and_id_columns
Constants::CARRIER_IDS.each do |carrier, id_column_size|
enabled_column = "#{carrier}_is_enabled"
id_column = "#{carrier}_id"
unique_set = Set.new
STATIONS.each do |row|
assert ["t", "f"].include?(row[enabled_column])
id = row[id_column]
if row[enabled_column] == "t"
assert !id.nil?, "Missing #{id_column} for station #{row["id"]}"
end
if !id.nil?
if id_column_size.is_a?(Array)
assert id_column_size.include?(row[id_column].size), "Invalid #{id_column}: #{row[id_column]} for station #{row["id"]}"
else
assert_equal id_column_size, row[id_column].size, "Invalid #{id_column}: #{row[id_column]} for station #{row["id"]}"
end
assert !unique_set.include?(row[id_column]), "Duplicated #{id_column} #{row[id_column]} for station #{row["id"]}"
unique_set << row[id_column]
end
end
end
end
def validate_id_unicity(column_name)
counts = {}
STATIONS.each do |row|
if row[column_name]
counts[row[column_name]] = (counts[row[column_name]] || 0) + 1
end
end
bad_counts = counts.select { |_, count| count != 1 }
assert_equal 0, bad_counts.length, "#{column_name} duplicated: #{bad_counts.map(&:first).join(', ')}"
end
def test_id_unicity
uniq_size = STATIONS.map { |row| row["id"] }.uniq.size
assert_equal STATIONS.size, uniq_size
end
def test_uic_unicity
validate_id_unicity("uic")
end
def test_sncf_tvs_id_unicity
validate_id_unicity("sncf_tvs_id")
end
def test_trenitalia_rtvt_id_unicity
validate_id_unicity("trenitalia_rtvt_id")
end
def test_coordinates
STATIONS.each do |row|
lon = row["longitude"]
lat = row["latitude"]
if lon
assert !lat.nil?, "Longitude of station #{row["id"]} set, but not latitude"
end
if lat
assert !lon.nil?, "Latitude of station #{row["id"]} set, but not longitude"
end
if row["is_suggestable"] == "t"
assert !lon.nil? && !lat.nil?, "Station #{row["id"]} is suggestable but has no coordinates"
end
if lon && lat
lon = lon.to_f
lat = lat.to_f
# Very rough bounding box of Europe
# Mostly tests if lon and lat are not switched
assert_operator lon, :>, -10, "Coordinates of station #{row["id"]} not within the bounding box"
assert_operator lon, :<, 41, "Coordinates of station #{row["id"]} not within the bounding box"
assert_operator lat, :>, 35, "Coordinates of station #{row["id"]} not within the bounding box"
assert_operator lat, :<, 69, "Coordinates of station #{row["id"]} not within the bounding box"
end
end
end
def test_sorted_by_id
ids = STATIONS.map { |row| row["id"].to_i }
assert ids == ids.sort, "The data is not sorted by the id column"
end
def test_is_suggestable
STATIONS.each do |row|
assert ["t", "f"].include?(row["is_suggestable"]), "Invalid value for is_suggestable for station #{row["id"]}"
end
end
def test_is_city
STATIONS.each do |row|
assert ["t", "f"].include?(row["is_city"]), "Invalid value for is_city for station #{row["id"]}"
end
end
def test_is_main_station
STATIONS.each do |row|
assert ["t", "f"].include?(row["is_main_station"]), "Invalid value for is_main_station for station #{row["id"]}"
end
end
def test_country
country_codes = Constants::COUNTRIES.keys
STATIONS.each do |row|
assert country_codes.include?(row["country"]), "Invalid value for country for station #{row["id"]}"
end
end
def test_timezone
STATIONS.each do |row|
timezone = Constants::COUNTRIES[row["country"]]
assert_equal timezone, row["time_zone"], "Invalid timezone for station #{row["id"]}"
end
end
def test_unique_suggestable_name
names = Set.new
STATIONS.each do |row|
if row["is_suggestable"] == "t" && !Constants::HOMONYM_STATIONS.include?(row["id"])
assert !names.include?(row["name"]), "Duplicate name '#{row["name"]}'"
names << row["name"]
end
end
end
def test_homonym_suggestable
Constants::HOMONYM_STATIONS.each do |homonym_id|
homonym_station = STATIONS_BY_ID[homonym_id]
assert_equal homonym_station["is_suggestable"], "t", "Homonym station #{homonym_station["name"]} (#{homonym_station["id"]}) is not suggestable"
end
end
def test_homonym_information
Constants::HOMONYM_STATIONS.each do |homonym_id|
homonym_station = STATIONS_BY_ID[homonym_id]
Constants::SUGGESTABLE_LOCALES.each do |locale|
assert !homonym_station["info:#{locale}"].nil?, "Homonym station #{homonym_station["name"]} (#{homonym_station["id"]}) must have an info in “#{locale}”"
end
end
end
def test_homonym_exists
stations = STATIONS.map do |station|
{
"id" => station["id"],
"folded_name" => fold(station["name"] || ""),
"suggestable?" => (station["is_suggestable"] == "t")
}
end
Constants::HOMONYM_STATIONS.each do |homonym_id|
homonym_station = STATIONS_BY_ID[homonym_id]
folded_homonym_name = fold(homonym_station["name"] || "")
has_homonym = stations.any? do |station|
station["id"] != homonym_station["id"] &&
station["folded_name"] == folded_homonym_name &&
station["suggestable?"]
end
assert has_homonym,
"Station #{homonym_station["name"]} (#{homonym_station["id"]}) does not have a suggestable homonym station"
end
end
def test_info_different_than_name
STATIONS.each do |row|
if row["is_suggestable"] == "t"
Constants::LOCALES.each do |locale|
if !row["info:#{locale}"].nil?
refute_equal row["name"], row["info:#{locale}"], "Name and “#{locale}” information should be different: '#{row["name"]}'"
if !["ru", "ko", "zh", "ja"].include?(locale)
refute_equal fold(row["name"]), fold(row["info:#{locale}"]), "Name and “#{locale}” information should be different: '#{row["name"]}'"
end
end
end
end
end
end
def test_suggestable_has_carrier
STATIONS.each do |row|
if row["is_suggestable"] == "t"
assert has_enabled_carrier(row) || CHILDREN[row["id"]].any? { |r| has_enabled_carrier(r) },
"Station #{row["id"]} is suggestable but has no enabled system"
end
end
end
def test_parent_station
STATIONS.each do |row|
parent_id = row["parent_station_id"]
if parent_id && row["is_suggestable"] == "t"
parent = STATIONS_BY_ID[parent_id]
assert !parent.nil?, "Station #{row["id"]} references a not existing parent station (#{parent_id})"
refute_equal parent_id, row["id"], "Station #{row["id"]} references itself as a parent station"
Constants::LOCALES.each do |locale|
if !parent["info:#{locale}"].nil?
assert !row["info:#{locale}"].nil?, "Station #{row["name"]} (#{row["id"]}) has no \“#{locale}\” info while its parent (#{parent["name"]}) has"
end
end
end
end
end
def test_parent_have_multiple_children
CHILDREN_ENABLED_COUNT.each do |parent_id, count|
parent_station = STATIONS_BY_ID[parent_id]
if parent_station["is_suggestable"] == "t"
assert count >= 2, "The parent station #{parent_station["name"]} (#{parent_station["id"]}) is suggestable and has only #{count} child"
end
end
end
def test_parent_should_be_city
CHILDREN.each do |parent_id, children_list|
parent_station = STATIONS_BY_ID[parent_id]
if children_list.size >= 1
parent_station = STATIONS_BY_ID[parent_id]
if !has_carrier_id(parent_station)
refute_equal parent_station["is_city"], "f", "The parent station #{parent_station["name"]} (#{parent_station["id"]}) has no carrier id and should be flagged as city"
end
end
if children_list.size >= 2 &&
parent_station["parent_station_id"].nil? &&
children_list.all? { |child| child["slug"].start_with?(parent_station["slug"]) && child["slug"] != parent_station["slug"] }
refute_equal parent_station["is_city"], "f", "The parent station #{parent_station["name"]} (#{parent_station["id"]}) should be a city"
end
end
end
def test_city_is_not_main_station
STATIONS.each do |row|
if row["is_city"] == "t"
assert_equal "f", row["is_main_station"], "The city #{row["name"]} (#{row["id"]}) cannot be a main station at the same time"
end
end
end
def test_parent_has_main_sation
CHILDREN.each do |parent_id, children_list|
parent_station = STATIONS_BY_ID[parent_id]
if children_list.size >= 2 &&
parent_station["is_suggestable"] == "t" &&
parent_station["is_main_station"] == "f" &&
parent_station["parent_station_id"].nil?
main_station_count = children_list.select { |child| child["is_main_station"] == "t" }.size
assert_equal 1, main_station_count, "The parent station #{parent_station["name"]} (#{parent_station["id"]}) should have one and only one main station"
end
end
end
def test_main_station_must_be_parent_or_child
STATIONS.each do |row|
if row["is_main_station"] == "t"
is_parent_or_child = CHILDREN[row["id"]].size > 0 || row["parent_station_id"]
assert is_parent_or_child, "The station #{row["name"]} (#{row["id"]}) cannot be main station as it is not a parent or child station"
end
end
end
def test_slugify
assert_equal slugify("Figueras/Figueres Vilafant Esp."), "figueras-figueres-vilafant-esp"
end
def test_unique_slugs
unique_set = Set.new
STATIONS.each do |row|
if row["is_suggestable"] == "t"
assert !unique_set.include?(row["slug"]), "Duplicated slug '#{row["slug"]}' for station #{row["id"]}"
unique_set << row["slug"]
end
end
end
def test_correct_slugs
STATIONS.each do |row|
if !Constants::HOMONYM_STATIONS.include?(row["id"])
assert_equal slugify(row["name"]), row["slug"], "Station #{row["id"]} (#{row["name"]}) has an incorrect slug"
else
suffixes = Constants::HOMONYM_SUFFIXES[row["country"]].join("|")
assert_match(/\A#{slugify(row["name"])}-(#{suffixes})+\z/, row["slug"], "Station #{row["id"]} has an incorrect slug")
end
end
end
def test_uic8_sncf
STATIONS.each do |row|
uic8_sncf = row["uic8_sncf"]
uic = row["uic"]
if !uic8_sncf.nil? && !Constants::UIC8_WHITELIST_IDS.include?(row["id"])
assert uic == uic8_sncf[0...-1], "Station #{row["id"]} have an incoherent uic8_sncf code"
end
end
end
def test_station_should_be_same_as
STATIONS.each do |row|
if row["is_suggestable"] == "f" &&
CHILDREN[row["id"]].empty? &&
row["parent_station_id"].nil? &&
ALIASES[row["id"]].empty? &&
row["same_as"].nil?
assert_equal 1, SLUG_COUNT["#{row["slug"]}_#{row["country"]}"],
"Station #{row["name"]} (#{row["id"]}) can be an alias of a station with the same name"
end
end
end
def test_same_as_is_valid
STATIONS.each do |row|
if row["same_as"]
assert_equal "f", row["is_suggestable"], "Station #{row["id"]} is an alias, yet it is suggestable"
refute_equal row["same_as"], row["id"], "Station #{row["id"]} references itself as an alias station"
actual_station = STATIONS_BY_ID[row["same_as"]]
assert row["slug"].start_with?(actual_station["slug"]), "Station #{row["id"]} is an alias of a station with a different name"
assert !actual_station.nil?, "Station #{row["id"]} is an alias of a station that does not exist"
end
end
end
def test_same_as_carrier_ids
STATIONS.each do |row|
if row["same_as"]
actual_station = STATIONS_BY_ID[row["same_as"]]
carrier_ids = Constants::CARRIER_IDS.keys.map { |carrier| "#{carrier}_id" }
carrier_ids += ["uic", "uic8_sncf", "sncf_tvs_id", "trenitalia_rtvt_id"]
carrier_ids.each do |carrier_id|
if row[carrier_id]
assert !actual_station[carrier_id].nil?, "Actual station #{actual_station["name"]} (#{actual_station["id"]}) has not a #{carrier_id} while its alias #{row["id"]} has one"
end
end
end
end
end
def test_sncf_self_service_machine
STATIONS.each do |row|
parent_id = row["parent_station_id"]
if parent_id
parent = STATIONS_BY_ID[parent_id]
if row["name"] == parent["name"]
assert_equal row["sncf_self_service_machine"], parent["sncf_self_service_machine"], "Child station #{row["id"]} and parent station #{parent["id"]} named #{row["name"]} should have the same SNCF self-service machine information"
end
end
if row["sncf_self_service_machine"] == "t"
assert_equal "FR", row["country"], "Station #{row["name"]} (#{row["id"]}) has a SNCF self-service machine but is not located in France"
assert !row["sncf_id"].nil?, "Station #{row["name"]} (#{row["id"]}) has a SNCF self-service machine but is without SNCF id"
end
end
end
def test_sncf_virtual_station
Constants::VIRTUAL_STATIONS.each do |id|
station = STATIONS_BY_ID[id]
assert_equal "t", station["sncf_is_enabled"], "Virtual station #{station["name"]} (#{station["id"]}) should be enabled for SNCF"
end
end
end