1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 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_core.errors import UsageError
13 from nominatim_db.tokenizer.sanitizers.config import SanitizerConfig
15 def test_string_list_default_empty():
16 assert SanitizerConfig().get_string_list('op') == []
19 def test_string_list_default_something():
20 assert SanitizerConfig().get_string_list('op', default=['a', 'b']) == ['a', 'b']
23 def test_string_list_value_string():
24 assert SanitizerConfig({'op': 't'}).get_string_list('op', default=['a', 'b']) == ['t']
27 def test_string_list_value_list():
28 assert SanitizerConfig({'op': ['1', '2']}).get_string_list('op') == ['1', '2']
31 def test_string_list_value_empty():
32 assert SanitizerConfig({'op': ''}).get_string_list('op', default=['a', 'b']) == []
35 def test_string_list_value_dict():
36 with pytest.raises(UsageError):
37 SanitizerConfig({'op': {'1': 'a'}}).get_string_list('op')
40 def test_string_list_value_int_list():
41 with pytest.raises(UsageError):
42 SanitizerConfig({'op': [1, 2]}).get_string_list('op')
45 @pytest.mark.parametrize('inp', ('fg34', 'f\\f', 'morning [glory]', '56.78'))
46 def test_create_split_regex_no_params_unsplit(inp):
47 regex = SanitizerConfig().get_delimiter()
49 assert list(regex.split(inp)) == [inp]
52 @pytest.mark.parametrize('inp,outp', [('here,there', ['here', 'there']),
53 ('ying;;yang', ['ying', 'yang']),
54 (';a; ;c;d,', ['', 'a', '', 'c', 'd', '']),
55 ('1, 3 ,5', ['1', '3', '5'])
57 def test_create_split_regex_no_params_split(inp, outp):
58 regex = SanitizerConfig().get_delimiter()
60 assert list(regex.split(inp)) == outp
63 @pytest.mark.parametrize('delimiter', ['.', '\\', '[]', ' ', '/.*+'])
64 def test_create_split_regex_custom(delimiter):
65 regex = SanitizerConfig({'delimiters': delimiter}).get_delimiter()
67 assert list(regex.split(f'out{delimiter}house')) == ['out', 'house']
68 assert list(regex.split('out,house')) == ['out,house']
71 def test_create_split_regex_empty_delimiter():
72 with pytest.raises(UsageError):
73 regex = SanitizerConfig({'delimiters': ''}).get_delimiter()
76 @pytest.mark.parametrize('inp', ('name', 'name:de', 'na\\me', '.*', ''))
77 def test_create_name_filter_no_param_no_default(inp):
78 filt = SanitizerConfig({'filter-kind': 'place'}).get_filter('name')
83 @pytest.mark.parametrize('inp', ('name', 'name:de', 'na\\me', '.*', ''))
84 def test_create_name_filter_no_param_default_pass_all(inp):
85 filt = SanitizerConfig().get_filter('name', 'PASS_ALL')
90 @pytest.mark.parametrize('inp', ('name', 'name:de', 'na\\me', '.*', ''))
91 def test_create_name_filter_no_param_default_fail_all(inp):
92 filt = SanitizerConfig().get_filter('name', 'FAIL_ALL')
97 def test_create_name_filter_no_param_default_invalid_string():
98 with pytest.raises(ValueError):
99 filt = SanitizerConfig().get_filter('name', 'abc')
102 def test_create_name_filter_no_param_default_empty_list():
103 with pytest.raises(ValueError):
104 filt = SanitizerConfig().get_filter('name', [])
107 @pytest.mark.parametrize('kind', ('de', 'name:de', 'ende'))
108 def test_create_kind_filter_default_positive(kind):
109 filt = SanitizerConfig().get_filter('filter-kind', ['.*de'])
114 @pytest.mark.parametrize('kind', ('de', 'name:de', 'ende'))
115 def test_create_kind_filter_default_negetive(kind):
116 filt = SanitizerConfig().get_filter('filter-kind', ['.*fr'])
118 assert not filt(kind)
121 @pytest.mark.parametrize('kind', ('lang', 'lang:de', 'langxx'))
122 def test_create_kind_filter_custom_regex_positive(kind):
123 filt = SanitizerConfig({'filter-kind': 'lang.*'}
124 ).get_filter('filter-kind', ['.*fr'])
129 @pytest.mark.parametrize('kind', ('de ', '123', '', 'bedece'))
130 def test_create_kind_filter_custom_regex_negative(kind):
131 filt = SanitizerConfig({'filter-kind': '.*de'}).get_filter('filter-kind')
133 assert not filt(kind)
136 @pytest.mark.parametrize('kind', ('name', 'fr', 'name:fr', 'frfr', '34'))
137 def test_create_kind_filter_many_positive(kind):
138 filt = SanitizerConfig({'filter-kind': ['.*fr', 'name', r'\d+']}
139 ).get_filter('filter-kind')
144 @pytest.mark.parametrize('kind', ('name:de', 'fridge', 'a34', '.*', '\\'))
145 def test_create_kind_filter_many_negative(kind):
146 filt = SanitizerConfig({'filter-kind': ['.*fr', 'name', r'\d+']}
147 ).get_filter('filter-kind')
149 assert not filt(kind)