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