]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tokenizer/sanitizers/test_clean_housenumbers.py
sanitizer: move helpers into a configuration class
[nominatim.git] / test / python / tokenizer / sanitizers / test_clean_housenumbers.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for the sanitizer that normalizes housenumbers.
9 """
10 import pytest
11
12 from nominatim.tokenizer.place_sanitizer import PlaceSanitizer
13 from nominatim.indexer.place_info import PlaceInfo
14
15 @pytest.fixture
16 def sanitize(request):
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()})
20
21     def _run(**kwargs):
22         place = PlaceInfo({'address': kwargs})
23         _, address = PlaceSanitizer([sanitizer_args]).process_names(place)
24
25         return sorted([(p.kind, p.name) for p in address])
26
27     return _run
28
29
30 def test_simple_number(sanitize):
31     assert sanitize(housenumber='34') == [('housenumber', '34')]
32
33
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')]
39
40
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')]
45
46
47 @pytest.mark.parametrize('number', ('6523', 'n/a', '4'))
48 def test_convert_to_name_converted(number):
49     sanitizer_args = {'step': 'clean-housenumbers',
50                       'convert-to-name': (r'\d+', 'n/a')}
51
52     place = PlaceInfo({'address': {'housenumber': number}})
53     names, address = PlaceSanitizer([sanitizer_args]).process_names(place)
54
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)
57