1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for the sanitizer that normalizes housenumbers.
12 from nominatim.tokenizer.place_sanitizer import PlaceSanitizer
13 from nominatim.data.place_info import PlaceInfo
16 def sanitize(request, def_config):
17 sanitizer_args = {'step': 'clean-housenumbers'}
18 for mark in request.node.iter_markers(name="sanitizer_params"):
19 sanitizer_args.update({k.replace('_', '-') : v for k,v in mark.kwargs.items()})
22 place = PlaceInfo({'address': kwargs})
23 _, address = PlaceSanitizer([sanitizer_args], def_config).process_names(place)
25 return sorted([(p.kind, p.name) for p in address])
30 def test_simple_number(sanitize):
31 assert sanitize(housenumber='34') == [('housenumber', '34')]
34 @pytest.mark.parametrize('number', ['1;2;3', '1,2,3', '1; 3 ,2',
35 '2,,3,1', '1;2;3;;', ';3;2;1'])
36 def test_housenumber_lists(sanitize, number):
37 assert sanitize(housenumber=number) == \
38 [('housenumber', '1'), ('housenumber', '2'), ('housenumber', '3')]
41 @pytest.mark.sanitizer_params(filter_kind=('number', 'streetnumber'))
42 def test_filter_kind(sanitize):
43 assert sanitize(housenumber='34', number='4', badnumber='65') == \
44 [('badnumber', '65'), ('housenumber', '34'), ('housenumber', '4')]
47 @pytest.mark.parametrize('number', ('6523', 'n/a', '4'))
48 def test_convert_to_name_converted(def_config, number):
49 sanitizer_args = {'step': 'clean-housenumbers',
50 'convert-to-name': (r'\d+', 'n/a')}
52 place = PlaceInfo({'address': {'housenumber': number}})
53 names, address = PlaceSanitizer([sanitizer_args], def_config).process_names(place)
55 assert ('housenumber', number) in set((p.kind, p.name) for p in names)
56 assert 'housenumber' not in set(p.kind for p in address)
59 @pytest.mark.parametrize('number', ('a54', 'n.a', 'bow'))
60 def test_convert_to_name_unconverted(def_config, number):
61 sanitizer_args = {'step': 'clean-housenumbers',
62 'convert-to-name': (r'\d+', 'n/a')}
64 place = PlaceInfo({'address': {'housenumber': number}})
65 names, address = PlaceSanitizer([sanitizer_args], def_config).process_names(place)
67 assert 'housenumber' not in set(p.kind for p in names)
68 assert ('housenumber', number) in set((p.kind, p.name) for p in address)