Skip to content

Commit

Permalink
Add test to check that p-code with blank adm name can still be found
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarans committed Jul 22, 2024
1 parent b18c5ee commit 44f7973
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
11 changes: 7 additions & 4 deletions src/hdx/location/adminlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def setup_row(
self,
countryiso3: str,
pcode: str,
adm_name: str,
adm_name: Optional[str],
parent: Optional[str],
):
"""
Expand All @@ -134,22 +134,25 @@ def setup_row(
Args:
countryiso3 (str): Country
pcode (str): P-code
adm_name (str): Administrative name
adm_name (Optional[str]): Administrative name (which can be None)
parent (Optional[str]): Parent p-code
Returns:
None
"""
self.pcode_lengths[countryiso3] = len(pcode)
self.pcodes.append(pcode)
if adm_name is None:
adm_name = ""
self.pcode_to_name[pcode] = adm_name
self.pcode_to_iso3[pcode] = countryiso3
if not adm_name:
return

adm_name = normalise(adm_name)
name_to_pcode = self.name_to_pcode.get(countryiso3, {})
name_to_pcode[adm_name] = pcode
self.name_to_pcode[countryiso3] = name_to_pcode
self.pcode_to_iso3[pcode] = countryiso3
self.pcode_to_iso3[pcode] = countryiso3

if self.use_parent:
name_parent_to_pcode = self.name_parent_to_pcode.get(
Expand Down
32 changes: 20 additions & 12 deletions tests/hdx/location/test_adminlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ def config_parent(self):

@pytest.fixture(scope="function")
def url(self):
return "https://raw.githubusercontent.com/OCHA-DAP/hdx-python-country/main/tests/fixtures/global_pcodes_adm_1_2.csv"
return "https://raw.githubusercontent.com/OCHA-DAP/hdx-python-country/blank_adm_name/tests/fixtures/global_pcodes_adm_1_2.csv"

@pytest.fixture(scope="function")
def formats_url(self):
return "https://raw.githubusercontent.com/OCHA-DAP/hdx-python-country/main/tests/fixtures/global_pcode_lengths.csv"
return "https://raw.githubusercontent.com/OCHA-DAP/hdx-python-country/blank_adm_name/tests/fixtures/global_pcode_lengths.csv"

def test_adminlevel(self, config):
adminone = AdminLevel(config)
Expand Down Expand Up @@ -550,19 +550,19 @@ def test_adminlevel_pcode_formats(self, config, url, formats_url):
"NG015",
True,
)
assert adminone.get_pcode("NER", "NER004", logname="test") == (
"NER004",
assert adminone.get_pcode("NER", "NE004", logname="test") == (
"NE004",
True,
)
assert adminone.get_pcode("NER", "NE04", logname="test") == (
"NER004",
"NE004",
True,
)
assert adminone.get_pcode("NER", "NE004", logname="test") == (
"NER004",
assert adminone.get_pcode("NER", "NER004", logname="test") == (
"NE004",
True,
)
assert adminone.get_pcode("ABC", "NE004", logname="test") == (
assert adminone.get_pcode("ABC", "NER004", logname="test") == (
None,
True,
)
Expand Down Expand Up @@ -642,11 +642,11 @@ def test_adminlevel_pcode_formats(self, config, url, formats_url):
True,
)
assert admintwo.get_pcode("NER", "NER004009", logname="test") == (
"NER004009",
"NE004009",
True,
)
assert admintwo.get_pcode("NER", "NE04009", logname="test") == (
"NER004009",
"NE004009",
True,
)
# Algorithm inserts 0 to make NER000409 and hence fails (it has no
Expand Down Expand Up @@ -687,15 +687,15 @@ def test_adminlevel_pcode_formats(self, config, url, formats_url):
# is not a valid admin1 (NER000) so the algorithm tries adding
# the 0 prefix at the admin2 level instead and hence succeeds
assert admintwo.get_pcode("NER", "NE00409", logname="test") == (
"NER004009",
"NE004009",
True,
)
# we don't use the parent because it could have a pcode length issue
# itself
assert admintwo.get_pcode(
"NER", "NE00409", parent="blah", logname="test"
) == (
"NER004009",
"NE004009",
True,
)
# The lookup in admin1 reveals that removing the 0 prefix from the
Expand All @@ -715,3 +715,11 @@ def test_adminlevel_pcode_formats(self, config, url, formats_url):
"NG015001",
True,
)
assert admintwo.get_pcode("JAM", "JM10001", logname="test") == (
"JM10001",
True,
)
assert admintwo.get_pcode("JAM", "JAM10001", logname="test") == (
"JM10001",
True,
)

0 comments on commit 44f7973

Please sign in to comment.