Skip to content

Commit 0db2468

Browse files
committed
Improve validation of zip_prefixes and zips_crossing_provinces in region yaml files
1 parent a77efec commit 0db2468

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

test/worldwide/region_data_consistency_test.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,56 @@ class RegionDataConsistencyTest < ActiveSupport::TestCase
5757
end
5858
end
5959

60+
test "If a country has zip_prefixes, then every province has at least one prefix" do
61+
Regions.all.select(&:country?).each do |country|
62+
next unless country.zones&.any? { |zone| zone.zip_prefixes.present? }
63+
64+
zones = country.zones.select do |zone|
65+
zone.zip_prefixes.blank? && # zone has no prefixes
66+
zone.parents.size == 1 # zone is not a shared zone, e.g. a special territory
67+
end
68+
69+
# IT-OT (Olbia-Tempio) no longer exists, and was absorbed into IT-SS (Sassari).
70+
# We need to add proper support for "deprecation" of zones.
71+
# In the mean time, we have removed all prefixes from OT intentionally.
72+
if country.legacy_code == "IT"
73+
zones.reject! { |z| z.legacy_code == "OT" }
74+
end
75+
76+
# IC (Canary Islands) are not defined outside of world.yml
77+
# Ceuta is listed with code EA in world.yml, but may actually be CE. TBD.
78+
if country.legacy_code == "ES"
79+
zones.reject! { |zone| zone.iso_code == "IC" || zone.iso_code == "EA" }
80+
end
81+
82+
assert_empty zones, "#{country.legacy_name} has no prefix(es) for #{zones.map(&:legacy_code).join(", ")}"
83+
end
84+
end
85+
86+
test "The first province listed for each of the zips_crossing_provinces must be where that prefix resolves" do
87+
Regions.all.select(&:country?).each do |country|
88+
country.zips_crossing_provinces&.each do |zip, province_codes|
89+
mapped_province = country.zone(zip:)
90+
91+
assert_equal province_codes.first, mapped_province&.legacy_code, "Expected first province code for zip #{zip}"
92+
end
93+
end
94+
end
95+
96+
test "If a zip has two provinces, then both must be neighbours of each other" do
97+
Regions.all.select(&:country?).each do |country|
98+
country.zips_crossing_provinces&.each do |zip, province_codes|
99+
neighbours = province_codes.map do |zone_code|
100+
country.zone(code: zone_code)&.neighbours
101+
end.flatten.uniq
102+
103+
province_codes.each do |zone_code|
104+
assert_includes neighbours, zone_code, "zip #{zip} missing neighbour #{zone_code}"
105+
end
106+
end
107+
end
108+
end
109+
60110
test "country codes match expected formats" do
61111
Regions.all.select(&:country?).each do |country|
62112
assert_match(/^([A-Z]{2})$/, country.iso_code, "alpha2 for #{country.legacy_name}")

test/worldwide/region_yml_consistency_test.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,39 @@ class RegionYmlConsistencyTest < ActiveSupport::TestCase
365365
end
366366
end
367367

368+
test "all neighbors are uppercase" do
369+
Regions.all.select(&:province?).each do |province|
370+
next if province.neighbors.blank?
371+
372+
assert province.neighbors.all?(&:upcase)
373+
end
374+
end
375+
376+
test "If A is a neighbor for B, then B must be a neighbor for A" do
377+
Regions.all.select(&:country?).each do |country|
378+
next if country.zones.blank?
379+
next unless country.zones.any? { |zone| zone.neighbours.present? }
380+
381+
# Fix `neighboring_zones` data in India and Thailand
382+
next if country.legacy_code == "IN" || country.legacy_code == "TH"
383+
384+
country.zones.each do |zone_a|
385+
# Just because some zones in a country have neighbours defined doesn't mean that they all do.
386+
# For example, AU-TAS is an island, so it shares no land borders with any other zone.
387+
next if zone_a.neighbours.blank?
388+
389+
zone_a.neighbours.each do |zone_code_b|
390+
zone_b = country.zone(code: zone_code_b)
391+
assertion_message =
392+
"Region #{country.legacy_code} zone A #{zone_a.legacy_code} zone B #{zone_b.legacy_code}"
393+
394+
assert_predicate zone_b.neighbours, :present?, assertion_message
395+
assert_includes zone_b.neighbours, zone_a.legacy_code, assertion_message
396+
end
397+
end
398+
end
399+
end
400+
368401
test "week_start_day is a valid value" do
369402
Regions.all.select(&:country?).each do |country|
370403
week_start_day = country.week_start_day

0 commit comments

Comments
 (0)