]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tokenizer/sanitizers/test_derive_names.py
prepare release 5.3.2.post7
[nominatim.git] / test / python / tokenizer / sanitizers / test_derive_names.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2026 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for the sanitizer that creates derived names using regular expressions.
9 """
10 import pytest
11
12 from nominatim_db.errors import UsageError
13 from nominatim_db.data.place_info import PlaceInfo
14 from nominatim_db.tokenizer.place_sanitizer import PlaceSanitizer
15
16
17 @pytest.fixture
18 def mk_sanitizer(def_config):
19     def _f(**kwargs):
20         args = {k.replace('_', '-'): v for k, v in kwargs.items()}
21         return PlaceSanitizer([args | {'step': 'derive-names'}], def_config)
22
23     return _f
24
25
26 def test_no_parameters(mk_sanitizer):
27     with pytest.raises(UsageError, match='name-pattern is missing'):
28         mk_sanitizer()
29
30
31 @pytest.mark.parametrize('prim,out', [('name', 0), ('address', 1)])
32 @pytest.mark.parametrize('keep', [True, False])
33 def test_name_deletion(mk_sanitizer, prim, out, keep):
34     san = mk_sanitizer(name_pattern='A.*B', type=prim, keep_original=keep)
35
36     names = dict(name='AltBoom', alt_name='foo', loc_name='AltB')
37     place = PlaceInfo({'name': names, 'address': names,
38                        'country_code': 'de', 'rank_address': 30})
39
40     san.process_names(place)
41     res = [place.searchable_names, place.searchable_address]
42
43     assert len(res[(out + 1) % 2]) == 3
44     if keep:
45         assert len(res[out]) == 3
46     else:
47         assert {(p.kind, p.name) for p in res[out]} == {('name', 'AltBoom'),
48                                                         ('alt_name', 'foo')}
49
50
51 @pytest.fixture
52 def simple_replace(mk_sanitizer):
53     def _impl(name, pattern, *variants, keep=True):
54         san = mk_sanitizer(name_pattern=pattern, type='name', keep_original=keep,
55                            variants=variants)
56         place = PlaceInfo({'name': {'name': name}, 'country_code': 'de', 'rank_address': 30})
57         san.process_names(place)
58
59         return {p.name for p in place.searchable_names}
60     return _impl
61
62
63 def test_variant_single_name(simple_replace):
64     assert simple_replace('A', 'A', 'The A') == {'A', 'The A'}
65
66
67 def test_replace_single_name(simple_replace):
68     assert simple_replace('A', 'A', 'The A', keep=False) == {'The A'}
69
70
71 def test_variant_with_multiple_names(simple_replace):
72     assert simple_replace('A', 'A', 'A1', 'A2', 'A1') == {'A', 'A1', 'A2'}
73
74
75 def test_variant_with_group_replacement(simple_replace):
76     assert simple_replace('abc X', '(.*) X', r'\1 Y') == {'abc X', 'abc Y'}