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 execution of the sanitztion step.
12 from nominatim_db.errors import UsageError
13 import nominatim_db.tokenizer.place_sanitizer as sanitizer
14 from nominatim_db.data.place_info import PlaceInfo
17 def test_placeinfo_clone_new_name():
18 place = sanitizer.PlaceName('foo', 'ki', 'su')
20 newplace = place.clone(name='bar')
22 assert place.name == 'foo'
23 assert newplace.name == 'bar'
24 assert newplace.kind == 'ki'
25 assert newplace.suffix == 'su'
28 def test_placeinfo_clone_merge_attr():
29 place = sanitizer.PlaceName('foo', 'ki', 'su')
30 place.set_attr('a1', 'v1')
31 place.set_attr('a2', 'v2')
33 newplace = place.clone(attr={'a2': 'new', 'b2': 'foo'})
35 assert place.get_attr('a2') == 'v2'
36 assert place.get_attr('b2') is None
37 assert newplace.get_attr('a1') == 'v1'
38 assert newplace.get_attr('a2') == 'new'
39 assert newplace.get_attr('b2') == 'foo'
42 def test_placeinfo_has_attr():
43 place = sanitizer.PlaceName('foo', 'ki', 'su')
44 place.set_attr('a1', 'v1')
46 assert place.has_attr('a1')
47 assert not place.has_attr('whatever')
50 def test_sanitizer_default(def_config):
51 san = sanitizer.PlaceSanitizer([{'step': 'split-name-list'}], def_config)
53 name, address = san.process_names(PlaceInfo({'name': {'name:de:de': '1;2;3'},
54 'address': {'street': 'Bald'}}))
57 assert all(isinstance(n, sanitizer.PlaceName) for n in name)
58 assert all(n.kind == 'name' for n in name)
59 assert all(n.suffix == 'de:de' for n in name)
61 assert len(address) == 1
62 assert all(isinstance(n, sanitizer.PlaceName) for n in address)
65 @pytest.mark.parametrize('rules', [None, []])
66 def test_sanitizer_empty_list(def_config, rules):
67 san = sanitizer.PlaceSanitizer(rules, def_config)
69 name, address = san.process_names(PlaceInfo({'name': {'name:de:de': '1;2;3'}}))
72 assert all(isinstance(n, sanitizer.PlaceName) for n in name)
75 def test_sanitizer_missing_step_definition(def_config):
76 with pytest.raises(UsageError):
77 san = sanitizer.PlaceSanitizer([{'id': 'split-name-list'}], def_config)