Skip to content

Commit

Permalink
Improve validation of zip_prefixes and zips_crossing_provinces in reg…
Browse files Browse the repository at this point in the history
…ion yaml files
  • Loading branch information
rochlefebvre committed Feb 24, 2025
1 parent a77efec commit 0db2468
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
50 changes: 50 additions & 0 deletions test/worldwide/region_data_consistency_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,56 @@ class RegionDataConsistencyTest < ActiveSupport::TestCase
end
end

test "If a country has zip_prefixes, then every province has at least one prefix" do
Regions.all.select(&:country?).each do |country|
next unless country.zones&.any? { |zone| zone.zip_prefixes.present? }

zones = country.zones.select do |zone|
zone.zip_prefixes.blank? && # zone has no prefixes
zone.parents.size == 1 # zone is not a shared zone, e.g. a special territory
end

# IT-OT (Olbia-Tempio) no longer exists, and was absorbed into IT-SS (Sassari).
# We need to add proper support for "deprecation" of zones.
# In the mean time, we have removed all prefixes from OT intentionally.
if country.legacy_code == "IT"
zones.reject! { |z| z.legacy_code == "OT" }
end

# IC (Canary Islands) are not defined outside of world.yml
# Ceuta is listed with code EA in world.yml, but may actually be CE. TBD.
if country.legacy_code == "ES"
zones.reject! { |zone| zone.iso_code == "IC" || zone.iso_code == "EA" }
end

assert_empty zones, "#{country.legacy_name} has no prefix(es) for #{zones.map(&:legacy_code).join(", ")}"
end
end

test "The first province listed for each of the zips_crossing_provinces must be where that prefix resolves" do
Regions.all.select(&:country?).each do |country|
country.zips_crossing_provinces&.each do |zip, province_codes|
mapped_province = country.zone(zip:)

assert_equal province_codes.first, mapped_province&.legacy_code, "Expected first province code for zip #{zip}"
end
end
end

test "If a zip has two provinces, then both must be neighbours of each other" do
Regions.all.select(&:country?).each do |country|
country.zips_crossing_provinces&.each do |zip, province_codes|
neighbours = province_codes.map do |zone_code|
country.zone(code: zone_code)&.neighbours
end.flatten.uniq

province_codes.each do |zone_code|
assert_includes neighbours, zone_code, "zip #{zip} missing neighbour #{zone_code}"
end
end
end
end

test "country codes match expected formats" do
Regions.all.select(&:country?).each do |country|
assert_match(/^([A-Z]{2})$/, country.iso_code, "alpha2 for #{country.legacy_name}")
Expand Down
33 changes: 33 additions & 0 deletions test/worldwide/region_yml_consistency_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,39 @@ class RegionYmlConsistencyTest < ActiveSupport::TestCase
end
end

test "all neighbors are uppercase" do
Regions.all.select(&:province?).each do |province|
next if province.neighbors.blank?

assert province.neighbors.all?(&:upcase)
end
end

test "If A is a neighbor for B, then B must be a neighbor for A" do
Regions.all.select(&:country?).each do |country|
next if country.zones.blank?
next unless country.zones.any? { |zone| zone.neighbours.present? }

# Fix `neighboring_zones` data in India and Thailand
next if country.legacy_code == "IN" || country.legacy_code == "TH"

country.zones.each do |zone_a|
# Just because some zones in a country have neighbours defined doesn't mean that they all do.
# For example, AU-TAS is an island, so it shares no land borders with any other zone.
next if zone_a.neighbours.blank?

zone_a.neighbours.each do |zone_code_b|
zone_b = country.zone(code: zone_code_b)
assertion_message =
"Region #{country.legacy_code} zone A #{zone_a.legacy_code} zone B #{zone_b.legacy_code}"

assert_predicate zone_b.neighbours, :present?, assertion_message
assert_includes zone_b.neighbours, zone_a.legacy_code, assertion_message
end
end
end
end

test "week_start_day is a valid value" do
Regions.all.select(&:country?).each do |country|
week_start_day = country.week_start_day
Expand Down

0 comments on commit 0db2468

Please sign in to comment.