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 helper functions.
12 from nominatim.errors import UsageError
13 from nominatim.tokenizer.place_sanitizer import PlaceName
14 import nominatim.tokenizer.sanitizers.helpers as helpers
16 @pytest.mark.parametrize('inp', ('fg34', 'f\\f', 'morning [glory]', '56.78'))
17 def test_create_split_regex_no_params_unsplit(inp):
18 regex = helpers.create_split_regex({})
20 assert list(regex.split(inp)) == [inp]
23 @pytest.mark.parametrize('inp,outp', [('here,there', ['here', 'there']),
24 ('ying;;yang', ['ying', 'yang']),
25 (';a; ;c;d,', ['', 'a', '', 'c', 'd', '']),
26 ('1, 3 ,5', ['1', '3', '5'])
28 def test_create_split_regex_no_params_split(inp, outp):
29 regex = helpers.create_split_regex({})
31 assert list(regex.split(inp)) == outp
34 @pytest.mark.parametrize('delimiter', ['.', '\\', '[]', ' ', '/.*+'])
35 def test_create_split_regex_custom(delimiter):
36 regex = helpers.create_split_regex({'delimiters': delimiter})
38 assert list(regex.split(f'out{delimiter}house')) == ['out', 'house']
39 assert list(regex.split('out,house')) == ['out,house']
42 def test_create_split_regex_empty_delimiter():
43 with pytest.raises(UsageError):
44 regex = helpers.create_split_regex({'delimiters': ''})
47 @pytest.mark.parametrize('inp', ('name', 'name:de', 'na\\me', '.*'))
48 def test_create_kind_filter_no_params(inp):
49 filt = helpers.create_kind_filter({})
51 assert filt(PlaceName('something', inp, ''))
54 @pytest.mark.parametrize('kind', ('de', 'name:de', 'ende'))
55 def test_create_kind_filter_custom_regex_positive(kind):
56 filt = helpers.create_kind_filter({'filter-kind': '.*de'})
58 assert filt(PlaceName('something', kind, ''))
61 @pytest.mark.parametrize('kind', ('de ', '123', '', 'bedece'))
62 def test_create_kind_filter_custom_regex_negative(kind):
63 filt = helpers.create_kind_filter({'filter-kind': '.*de'})
65 assert not filt(PlaceName('something', kind, ''))
68 @pytest.mark.parametrize('kind', ('name', 'fr', 'name:fr', 'frfr', '34'))
69 def test_create_kind_filter_many_positive(kind):
70 filt = helpers.create_kind_filter({'filter-kind': ['.*fr', 'name', r'\d+']})
72 assert filt(PlaceName('something', kind, ''))
75 @pytest.mark.parametrize('kind', ('name:de', 'fridge', 'a34', '.*', '\\'))
76 def test_create_kind_filter_many_negative(kind):
77 filt = helpers.create_kind_filter({'filter-kind': ['.*fr', 'name', r'\d+']})
79 assert not filt(PlaceName('something', kind, ''))