]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tokenizer/sanitizers/test_split_name_list.py
Merge pull request #4130 from lonvia/avoid-creating-word-lookup-indexes
[nominatim.git] / test / python / tokenizer / sanitizers / test_split_name_list.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 splits multivalue lists.
9 """
10 import pytest
11
12 from nominatim_db.tokenizer.place_sanitizer import PlaceSanitizer
13 from nominatim_db.data.place_info import PlaceInfo
14
15 from nominatim_db.errors import UsageError
16
17
18 class TestSplitName:
19
20     @pytest.fixture(autouse=True)
21     def setup_country(self, def_config):
22         self.config = def_config
23
24     def run_sanitizer_on(self, **kwargs):
25         place = PlaceInfo({'name': kwargs})
26         PlaceSanitizer([{'step': 'split-name-list'}], self.config).process_names(place)
27
28         return sorted([(p.name, p.kind, p.suffix) for p in place.searchable_names])
29
30     def sanitize_with_delimiter(self, delimiter, name):
31         place = PlaceInfo({'name': {'name': name}})
32         PlaceSanitizer([{'step': 'split-name-list', 'delimiters': delimiter}],
33                        self.config).process_names(place)
34
35         return sorted([p.name for p in place.searchable_names])
36
37     def test_simple(self):
38         assert self.run_sanitizer_on(name='ABC') == [('ABC', 'name', None)]
39         assert self.run_sanitizer_on(name='') == [('', 'name', None)]
40
41     def test_splits(self):
42         assert self.run_sanitizer_on(name='A;B;C') == [('A', 'name', None),
43                                                        ('B', 'name', None),
44                                                        ('C', 'name', None)]
45         assert self.run_sanitizer_on(short_name=' House, boat ') == [('House', 'short_name', None),
46                                                                      ('boat', 'short_name', None)]
47
48     def test_empty_fields(self):
49         assert self.run_sanitizer_on(name='A;;B') == [('A', 'name', None),
50                                                       ('B', 'name', None)]
51         assert self.run_sanitizer_on(name='A; ,B') == [('A', 'name', None),
52                                                        ('B', 'name', None)]
53         assert self.run_sanitizer_on(name=' ;B') == [('B', 'name', None)]
54         assert self.run_sanitizer_on(name='B,') == [('B', 'name', None)]
55
56     def test_custom_delimiters(self):
57         assert self.sanitize_with_delimiter(':', '12:45,3') == ['12', '45,3']
58         assert self.sanitize_with_delimiter('\\', 'a;\\b!#@ \\') == ['a;', 'b!#@']
59         assert self.sanitize_with_delimiter('[]', 'foo[to]be') == ['be', 'foo', 'to']
60         assert self.sanitize_with_delimiter(' ', 'morning  sun') == ['morning', 'sun']
61
62     def test_empty_delimiter_set(self):
63         with pytest.raises(UsageError):
64             self.sanitize_with_delimiter('', 'abc')
65
66
67 def test_no_name_list(def_config):
68     place = PlaceInfo({'address': {'housenumber': '3'}})
69     PlaceSanitizer([{'step': 'split-name-list'}], def_config).process_names(place)
70
71     assert not place.searchable_names
72     assert len(place.searchable_address) == 1