Skip to content

Commit

Permalink
Deal with last admin level in loop
Browse files Browse the repository at this point in the history
Increase coverage
  • Loading branch information
Mike committed Oct 31, 2023
1 parent 4f11e88 commit c268191
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/hdx/location/adminlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,12 @@ def convert_admin_pcode_length(
admin_changes.append(str(admin_no))
new_pcode = "".join(pcode_parts)
break
elif part_length > admin_length and admin_no == self.admin_level:
if pcode_part[0] == "0":
pcode_parts[admin_no] = pcode_part[1:]
admin_changes.append(str(admin_no))
new_pcode = "".join(pcode_parts)
break
if len_new_pcode < total_length:
if admin_length > 2 and pos in self.zeroes[countryiso3]:
pcode_part = f"0{pcode_part}"
Expand Down
44 changes: 44 additions & 0 deletions tests/hdx/location/test_adminlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,35 @@ def test_adminlevel_pcode_formats(self, config, url, formats_url):
None,
True,
)

assert admintwo.get_pcode(
"DZA", "DZ009009", logname="test", fuzzy_match=False
) == (
"DZ009009",
True,
)
assert admintwo.get_pcode(
"DZA", "DZ0090009", logname="test", fuzzy_match=False
) == (
"DZ009009",
True,
)

assert admintwo.get_pcode(
"COL", "CO08849", logname="test", fuzzy_match=False
) == (
"CO08849",
True,
)
# Algorithm removes 0 to make CO80849 and hence fails (it has no
# knowledge that CO80 is an invalid admin 1)
assert admintwo.get_pcode(
"COL", "CO080849", logname="test", fuzzy_match=False
) == (
None,
True,
)

admintwo.set_parent_admins_from_adminlevels([adminone])
# The lookup in admin1 reveals that adding a 0 prefix to the admin1
# is not a valid admin1 (NER000) so the algorithm tries adding
Expand All @@ -400,9 +429,24 @@ def test_adminlevel_pcode_formats(self, config, url, formats_url):
"NER004009",
True,
)
# The lookup in admin1 reveals that removing the 0 prefix from the
# admin1 is not a valid admin1 (CO80849) so the algorithm tries
# removing the 0 prefix at the admin2 level instead and hence succeeds
assert admintwo.get_pcode(
"COL", "CO080849", logname="test", fuzzy_match=False
) == (
"CO08849",
True,
)

admintwo.set_parent_admins([adminone.pcodes])
assert admintwo.get_pcode("NER", "NE00409", logname="test") == (
"NER004009",
True,
)
assert admintwo.get_pcode(
"COL", "CO080849", logname="test", fuzzy_match=False
) == (
"CO08849",
True,
)

0 comments on commit c268191

Please sign in to comment.