Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions dataretrieval/codes/states.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"Guam": "gu",
"Northern Mariana Islands": "mp",
"Puerto Rico": "pr",
"US Virgin Islands": "vi",
"Virgin Islands": "vi",
}

fips_codes = {
Expand Down Expand Up @@ -131,7 +131,7 @@
"Guam": "66",
"Northern Mariana Islands": "69",
"Puerto Rico": "72",
"US Virgin Islands": "78",
"Virgin Islands": "78",
}

# Reverse lookups (built once): postal code -> name, FIPS code -> name, and a
Expand All @@ -140,6 +140,9 @@
_name_by_postal = {code: name for name, code in state_codes.items()}
_name_by_fips = {fips: name for name, fips in fips_codes.items()}
_name_by_lower = {name.lower(): name for name in state_codes}
# The table names each state as the Water Data and NGWMN ``state_name`` fields
# do. "US Virgin Islands" is the Census Bureau's name, accepted as input only.
_name_by_lower["us virgin islands"] = "Virgin Islands"


def to_state(
Expand Down
33 changes: 20 additions & 13 deletions tests/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,24 +526,31 @@ class TestTerritories:
"""

@pytest.mark.parametrize(
("value", "name", "postal", "fips"),
("name", "postal", "fips"),
[
("Puerto Rico", "Puerto Rico", "PR", "72"),
("PR", "Puerto Rico", "PR", "72"),
("72", "Puerto Rico", "PR", "72"),
("US:72", "Puerto Rico", "PR", "72"),
("Guam", "Guam", "GU", "66"),
("VI", "US Virgin Islands", "VI", "78"),
("60", "American Samoa", "AS", "60"),
("MP", "Northern Mariana Islands", "MP", "69"),
("Puerto Rico", "PR", "72"),
("Guam", "GU", "66"),
("Virgin Islands", "VI", "78"),
("American Samoa", "AS", "60"),
("Northern Mariana Islands", "MP", "69"),
],
)
def test_every_encoding_resolves(self, value, name, postal, fips):
def test_each_territory_is_in_both_code_tables(self, name, postal, fips):
"""Each code resolves to the others. The input encodings themselves are
:class:`Test_to_state`'s concern."""
from dataretrieval.codes.states import to_state

assert to_state(value, "name") == name
assert to_state(value, "postal") == postal
assert to_state(value, "fips") == fips
for value in (name, postal, fips):
assert to_state(value, "name") == name
assert to_state(value, "postal") == postal
assert to_state(value, "fips") == fips

def test_the_census_name_of_the_virgin_islands_is_accepted(self):
"""The table names it as the services do; the name it had before is
still accepted as input."""
from dataretrieval.codes.states import to_state

assert to_state("US Virgin Islands") == "Virgin Islands"

def test_the_ngwmn_shim_routes_a_territory_to_each_queryable(self):
"""``sites`` filters on ``state_name``, ``providers`` on ``state``."""
Expand Down
Loading