diff --git a/src/hdx/location/adminlevel.py b/src/hdx/location/adminlevel.py index d3e1a24..43748c3 100755 --- a/src/hdx/location/adminlevel.py +++ b/src/hdx/location/adminlevel.py @@ -125,7 +125,7 @@ def setup_row( self, countryiso3: str, pcode: str, - adm_name: str, + adm_name: Optional[str], parent: Optional[str], ): """ @@ -134,7 +134,7 @@ 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: @@ -142,14 +142,17 @@ def setup_row( """ 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( diff --git a/tests/hdx/location/test_adminlevel.py b/tests/hdx/location/test_adminlevel.py index 0e6fb33..322258d 100755 --- a/tests/hdx/location/test_adminlevel.py +++ b/tests/hdx/location/test_adminlevel.py @@ -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) @@ -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, ) @@ -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 @@ -687,7 +687,7 @@ 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 @@ -695,7 +695,7 @@ def test_adminlevel_pcode_formats(self, config, url, formats_url): 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 @@ -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, + )