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 sanitizer configuration helper functions.
12 from nominatim.errors import UsageError
13 from nominatim.tokenizer.place_sanitizer import PlaceName
14 from nominatim.tokenizer.sanitizers.config import SanitizerConfig
16 def test_string_list_default_empty():
17 assert SanitizerConfig().get_string_list('op') == []
20 def test_string_list_default_none():
21 assert SanitizerConfig().get_string_list('op', default=None) is None
24 def test_string_list_default_something():
25 assert SanitizerConfig().get_string_list('op', default=['a', 'b']) == ['a', 'b']
28 def test_string_list_value_string():
29 assert SanitizerConfig({'op': 't'}).get_string_list('op', default=['a', 'b']) == ['t']
32 def test_string_list_value_list():
33 assert SanitizerConfig({'op': ['1', '2']}).get_string_list('op') == ['1', '2']
36 def test_string_list_value_empty():
37 assert SanitizerConfig({'op': ''}).get_string_list('op', default=['a', 'b']) == []
40 def test_string_list_value_dict():
41 with pytest.raises(UsageError):
42 SanitizerConfig({'op': {'1': 'a'}}).get_string_list('op')
45 def test_string_list_value_int_list():
46 with pytest.raises(UsageError):
47 SanitizerConfig({'op': [1, 2]}).get_string_list('op')
50 @pytest.mark.parametrize('inp', ('fg34', 'f\\f', 'morning [glory]', '56.78'))
51 def test_create_split_regex_no_params_unsplit(inp):
52 regex = SanitizerConfig().get_delimiter()
54 assert list(regex.split(inp)) == [inp]
57 @pytest.mark.parametrize('inp,outp', [('here,there', ['here', 'there']),
58 ('ying;;yang', ['ying', 'yang']),
59 (';a; ;c;d,', ['', 'a', '', 'c', 'd', '']),
60 ('1, 3 ,5', ['1', '3', '5'])
62 def test_create_split_regex_no_params_split(inp, outp):
63 regex = SanitizerConfig().get_delimiter()
65 assert list(regex.split(inp)) == outp
68 @pytest.mark.parametrize('delimiter', ['.', '\\', '[]', ' ', '/.*+'])
69 def test_create_split_regex_custom(delimiter):
70 regex = SanitizerConfig({'delimiters': delimiter}).get_delimiter()
72 assert list(regex.split(f'out{delimiter}house')) == ['out', 'house']
73 assert list(regex.split('out,house')) == ['out,house']
76 def test_create_split_regex_empty_delimiter():
77 with pytest.raises(UsageError):
78 regex = SanitizerConfig({'delimiters': ''}).get_delimiter()
81 @pytest.mark.parametrize('inp', ('name', 'name:de', 'na\\me', '.*'))
82 def test_create_kind_filter_no_params(inp):
83 filt = SanitizerConfig().get_filter_kind()
85 assert filt(PlaceName('something', inp, ''))
88 @pytest.mark.parametrize('kind', ('de', 'name:de', 'ende'))
89 def test_create_kind_filter_custom_regex_positive(kind):
90 filt = SanitizerConfig({'filter-kind': '.*de'}).get_filter_kind()
92 assert filt(PlaceName('something', kind, ''))
95 @pytest.mark.parametrize('kind', ('de ', '123', '', 'bedece'))
96 def test_create_kind_filter_custom_regex_negative(kind):
97 filt = SanitizerConfig({'filter-kind': '.*de'}).get_filter_kind()
99 assert not filt(PlaceName('something', kind, ''))
102 @pytest.mark.parametrize('kind', ('name', 'fr', 'name:fr', 'frfr', '34'))
103 def test_create_kind_filter_many_positive(kind):
104 filt = SanitizerConfig({'filter-kind': ['.*fr', 'name', r'\d+']}).get_filter_kind()
106 assert filt(PlaceName('something', kind, ''))
109 @pytest.mark.parametrize('kind', ('name:de', 'fridge', 'a34', '.*', '\\'))
110 def test_create_kind_filter_many_negative(kind):
111 filt = SanitizerConfig({'filter-kind': ['.*fr', 'name', r'\d+']}).get_filter_kind()
113 assert not filt(PlaceName('something', kind, ''))