mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-03-13 06:14:07 +00:00
postcodes: strip leading country codes
This commit is contained in:
@@ -29,8 +29,9 @@ class _PostcodeMatcher:
|
||||
raise UsageError("Field 'pattern' required for 'postcode' "
|
||||
f"for country '{country_code}'")
|
||||
|
||||
self.pattern = re.compile(config['pattern'].replace('d', '[0-9]')
|
||||
.replace('l', '[A-Z]'))
|
||||
pc_pattern = config['pattern'].replace('d', '[0-9]').replace('l', '[A-Z]')
|
||||
|
||||
self.pattern = re.compile(f'(?:{country_code.upper()}[ -]?)?({pc_pattern})')
|
||||
|
||||
|
||||
def normalize(self, postcode):
|
||||
@@ -39,7 +40,9 @@ class _PostcodeMatcher:
|
||||
"""
|
||||
normalized = postcode.strip().upper()
|
||||
|
||||
return normalized if self.pattern.fullmatch(normalized) else None
|
||||
match = self.pattern.fullmatch(normalized)
|
||||
|
||||
return match.group(1) if match else None
|
||||
|
||||
|
||||
class _PostcodeSanitizer:
|
||||
|
||||
@@ -43,12 +43,14 @@ def test_postcode_no_country_drop(sanitize, country):
|
||||
assert sanitize(country=country, postcode='23231') == []
|
||||
|
||||
|
||||
@pytest.mark.parametrize("postcode", ('12345', ' 34009 '))
|
||||
@pytest.mark.parametrize("postcode", ('12345', ' 12345 ', 'de 12345',
|
||||
'DE12345', 'DE 12345', 'DE-12345'))
|
||||
def test_postcode_pass_good_format(sanitize, postcode):
|
||||
assert sanitize(country='de', postcode=postcode) == [('postcode', postcode.strip())]
|
||||
assert sanitize(country='de', postcode=postcode) == [('postcode', '12345')]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("postcode", ('123456', '', ' ', '.....'))
|
||||
@pytest.mark.parametrize("postcode", ('123456', '', ' ', '.....',
|
||||
'DE 12345', 'DEF12345', 'CH 12345'))
|
||||
@pytest.mark.sanitizer_params(convert_to_address=False)
|
||||
def test_postcode_drop_bad_format(sanitize, postcode):
|
||||
assert sanitize(country='de', postcode=postcode) == []
|
||||
|
||||
Reference in New Issue
Block a user