forked from trainline-eu/stations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_data.rb
535 lines (457 loc) · 19.2 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
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 }
SUGGESTABLE_STATIONS = STATIONS.select { |row| row['is_suggestable'] == 't' }
def has_enabled_carrier(row)
Constants::CARRIERS.any? { |carrier| row["#{carrier}_is_enabled"] == "t" }
end
def has_carrier_id(row)
Constants::CARRIERS.any? { |carrier| !row["#{carrier}_id"].nil? }
end
def has_rail_id(row)
Constants::RAIL_IDS.keys.any? { |rail_id| !row[rail_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_rail_id(row), "Station #{row["name"]} (#{row["id"]}) is useless and should be removed"
end
end
end
def test_number_columns
nb_columns = 39 + (Constants::CARRIERS.size * 2)
STATIONS.each { |row| assert_equal nb_columns, row.size, "Station #{row["name"]} (#{row["id"]}) has a wrong number of columns: #{row["size"]}" }
end
def test_station_name
STATIONS.each do |row|
assert !row["name"].nil?, "Station #{row["name"]} (#{row["id"]}) does not have a name"
disallowed_characters = '(\"|\'|\S\(|\)\S|\,|:|;|\?|\!|_| {2}| $)'
refute_match(/#{disallowed_characters}/, row["name"], "Station #{row["name"]} (#{row["id"]}) has disallowed characters in its name")
disallowed_combinations = Constants::ALLOWED_COMBINATIONS_WITH_DOT.join("|")
if !(Constants::ALLOWED_STATIONS_WITH_DOT.include?(row["id"]) || row["name"] =~ /(#{disallowed_combinations})/)
refute_match(/\./, row["name"], "Station #{row["name"]} (#{row["id"]}) shouldn't have a dot in its name")
end
end
end
def test_rail_ids
STATIONS.each do |row|
Constants::RAIL_IDS.each do |rail_id, expression|
if row[rail_id]
assert_match(/^#{expression}$/, row[rail_id], "Station #{row["name"]} (#{row["id"]}) has not a correct #{rail_id}")
end
end
end
end
def test_enabled_carrier_id
STATIONS.each do |row|
Constants::CARRIERS.each do |carrier|
enabled_column = "#{carrier}_is_enabled"
id_column = "#{carrier}_id"
if row[enabled_column] == "t"
assert !row[id_column].nil?, "Station #{row["name"]} (#{row["id"]}) is enabled for #{carrier} but has no carrier id"
end
end
end
end
def test_unique_ids
id_columns = ["id"] + Constants::RAIL_IDS.keys
id_columns.each do |id_column|
counts = {}
STATIONS.each do |row|
if row[id_column]
counts[row[id_column]] = (counts[row[id_column]] || 0) + 1
end
end
bad_counts = counts.select { |_, count| count != 1 }
assert_equal 0, bad_counts.length, "#{id_column} duplicated: #{bad_counts.map(&:first).join(', ')}"
end
end
def test_boolean_columns
STATIONS.each do |row|
Constants::BOOLEAN_COLUMNS.each do |column|
assert ["t", "f"].include?(row[column]), "Station #{row["name"]} (#{row["id"]}] has an invalid value for #{column}"
end
end
end
def test_coordinates
STATIONS.each do |row|
lon = row["longitude"]
lat = row["latitude"]
if lon
assert !lat.nil?, "Station #{row["name"]} (#{row["id"]}) has longitude but no latitude"
end
if lat
assert !lon.nil?, "Station #{row["name"]} (#{row["id"]}) has latitude but no longitude"
end
if row["is_suggestable"] == "t"
assert !lon.nil? && !lat.nil?, "Station #{row["name"]} (#{row["id"]}) is suggestable but has no coordinates"
end
if lon && lat
# Test coordinates have a correct format (with a dot and not a comma)
refute lat.include?(','), "Station #{row["name"]} (#{row["id"]}) latitude has a bad format"
refute lon.include?(','), "Station #{row["name"]} (#{row["id"]}) longitude has a bad format"
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, "Station #{row["name"]} (#{row["id"]}) has coordinates outside the bounding box"
assert_operator lon, :<, 41, "Station #{row["name"]} (#{row["id"]}) has coordinates outside the bounding box"
assert_operator lat, :>, 35, "Station #{row["name"]} (#{row["id"]}) has coordinates outside the bounding box"
assert_operator lat, :<, 69, "Station #{row["name"]} (#{row["id"]}) has coordinates outside the bounding box"
end
end
end
def test_sorted_by_id
ids = STATIONS.map { |row| row["id"].to_i }
assert ids == ids.sort, "Data is not sorted by the id column"
end
def test_country
country_codes = Constants::COUNTRIES.keys
STATIONS.each do |row|
assert country_codes.include?(row["country"]), "Station #{row["name"]} (#{row["id"]}) has an unknown country"
end
end
def test_timezone
STATIONS.each do |row|
timezone = Constants::COUNTRIES[row["country"]]
assert_equal timezone, row["time_zone"], "Station #{row["name"]} (#{row["id"]}) has an invalid timezone"
end
end
def test_unique_suggestable_name
names = Set.new
SUGGESTABLE_STATIONS.each do |row|
if !Constants::HOMONYM_STATIONS.include?(row["id"])
assert !names.include?(row["name"]), "Station #{row["name"]} (#{row["id"]}) has a name already used"
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_localized_info
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["country_hint"] == 't'),
"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_child_localized_info
STATIONS.each do |row|
parent = STATIONS_BY_ID[row["parent_station_id"]]
if !parent.nil? &&
row["is_suggestable"] == "t" &&
parent["is_suggestable"] == "t"
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"]} (#{parent["id"]}) has"
end
end
end
end
end
def test_localized_info_different_than_name
SUGGESTABLE_STATIONS.each do |row|
Constants::LOCALES.each do |locale|
if !row["info:#{locale}"].nil?
if ["ru", "ko", "zh", "ja"].include?(locale)
refute_match(/[a-zA-Z]/, row["info:#{locale}"], "Station #{row["name"]} (#{row["id"]}) has not a valid “#{locale}” info")
else
refute_match(/(^|-)#{slugify(row["name"])}(-|$)/, slugify(row["info:#{locale}"]), "Station #{row["name"]} (#{row["id"]}) should have a different name and “#{locale}” info")
refute_match(/(^|-)#{slugify(row["info:#{locale}"])}(-|$)/, slugify(row["name"]), "Station #{row["name"]} (#{row["id"]}) should have a different name and “#{locale}” info")
end
end
end
end
end
def test_suggestable_has_carrier
SUGGESTABLE_STATIONS.each do |row|
assert has_enabled_carrier(row) || CHILDREN[row["id"]].any? { |r| has_enabled_carrier(r) },
"Station #{row["name"]} (#{row["id"]}) is suggestable but has no enabled system"
end
end
def test_parent_station
SUGGESTABLE_STATIONS.each do |row|
parent_id = row["parent_station_id"]
if parent_id
parent = STATIONS_BY_ID[parent_id]
assert !parent.nil?, "Station #{row["name"]} (#{row["id"]}) references a nonexistent parent station (#{parent_id})"
refute_equal parent_id, row["id"], "Station #{row["name"]} (#{row["id"]}) references itself as a parent station"
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, "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", "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["is_suggestable"] == "t" && child["slug"].start_with?(parent_station["slug"]) && child["slug"] != parent_station["slug"] }
refute_equal parent_station["is_city"], "f", "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, "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, "Station #{row["name"]} (#{row["id"]}) cannot be the 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
SUGGESTABLE_STATIONS.each do |row|
assert !unique_set.include?(row["slug"]), "Station #{row["name"]} (#{row["id"]}) has a slug already used: #{row["slug"]}"
unique_set << row["slug"]
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["name"]} (#{row["id"]}) has an incorrect slug"
else
suffixes = Constants::HOMONYM_SUFFIXES[row["country"]].join("|")
assert_match(/^#{slugify(row["name"])}-(#{suffixes})$/, row["slug"], "Station #{row["name"]} (#{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["name"]} (#{row["id"]}) has 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["name"]} (#{row["id"]}) cannot be an alias and suggestable"
refute_equal row["same_as"], row["id"], "Station #{row["name"]} (#{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["name"]} (#{row["id"]}) is an alias of a station with a different name"
assert !actual_station.nil?, "Station #{row["name"]} (#{row["id"]}) is an alias of a station that does not exist"
end
end
end
def test_same_as_rail_ids
STATIONS.each do |row|
if row["same_as"]
actual_station = STATIONS_BY_ID[row["same_as"]]
Constants::RAIL_IDS.keys.each do |rail_id|
if row[rail_id]
assert !actual_station[rail_id].nil?, "Actual station #{actual_station["name"]} (#{actual_station["id"]}) has not a #{rail_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["name"]} (#{row["id"]}) and parent station #{parent["id"]} 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
def test_airports_comments_and_children
# Check that no airport station mention "airport" in the comments, and that all children are also airports
SUGGESTABLE_STATIONS.select do |row|
row['is_airport'] == 't'
end.each do |row|
refute has_localized_info?(row, AIRPORT_TRANSLATIONS),
"One or more comments for #{row['name']} (#{row["id"]}) are mentionning an airport which is not needed as this stations is flagged as an airport"
CHILDREN[row['id']].each do |child_row|
assert child_row['is_airport'] == 't',
"#{child_row['name']} (#{child_row['id']}) should be an airport, as a child of airport station #{row['name']} (#{row['id']})"
end
end
# Look for station including an airport translation in their name that should be flagged as airport
SUGGESTABLE_STATIONS.select { |row| row['is_airport'] == 'f' }.each do |row|
refute has_localized_info?(row, AIRPORT_TRANSLATIONS),
"Station #{row['name']} (#{row["id"]}) contains a mention to 'airport' in its name. This stations should be flagged as an airport instead"
end
end
def test_country_hints_and_children
SUGGESTABLE_STATIONS.select do |row|
row['country_hint'] == 't'
end.each do |row|
# Check that the comment does not contain country information
# Check that all children are also hinted
CHILDREN[row['id']].each do |child_row|
assert child_row['country_hint'] == 't',
"#{child_row['name']} (#{child_row['id']}) should have country hint enabled, as a child of country hinted station #{row['name']} (#{row['id']})"
end
end
end
def main_station_hints
# Check that no main station mentions "main station" in the comments
SUGGESTABLE_STATIONS.select do |row|
row['main_station_hint'] == 't'
end.each do |row|
refute has_localized_info?(row, MAIN_STATION_TRANSLATIONS),
"One or more comments for #{row['name']} (#{row["id"]}) are mentionning a main station which is not needed as this stations is flagged with main_station_hint"
end
# Look for station include "main station" in the comments that should be flagged with main_station_hint
SUGGESTABLE_STATIONS.reject do |row|
row['main_station_hint'] == 't' &&
MAIN_STATION_TRANSLATIONS.keys.all? { |locale| row["info:#{locale}"].nil? }
end.each do |row|
refute has_localized_info?(row, MAIN_STATION_TRANSLATIONS),
"One or more comment for #{row['name']} (#{row["id"]}) are mentionning a main station. This stations should be flagged with main_station_hint instead"
end
end
private
AIRPORT_TRANSLATIONS = {
:fr => 'aeroport',
:en => 'airport',
:de => 'flughafen',
:es => 'aeropuerto',
:it => 'aeroporto',
:nl => 'luchthaven',
:da => 'lufthavn',
:ru => 'аэропорт',
# to be completed with other languages
}
MAIN_STATION_TRANSLATIONS = {
:de => "hauptbahnhof",
:en => "main station",
:es => "estación central",
:fr => "gare centrale",
:it => "stazione centrale",
# to be completed with other languages
}
def has_localized_info?(row, translations)
translations.any? do |locale, translation|
slugify(row["info:#{locale}"] || '') =~ /#{translation}/i
end
end
def has_localized_mention?(value, translations)
translations.any? do |locale, translation|
slugify(value || '') =~ /#{translation}/i
end
end
end