]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tokenizer/test_place_sanitizer.py
add a WKB decoder for the Point class
[nominatim.git] / test / python / tokenizer / test_place_sanitizer.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 execution of the sanitztion step.
9 """
10 import pytest
11
12 from nominatim.errors import UsageError
13 import nominatim.tokenizer.place_sanitizer as sanitizer
14 from nominatim.data.place_info import PlaceInfo
15
16
17 def test_placeinfo_clone_new_name():
18     place = sanitizer.PlaceName('foo', 'ki', 'su')
19
20     newplace = place.clone(name='bar')
21
22     assert place.name == 'foo'
23     assert newplace.name == 'bar'
24     assert newplace.kind == 'ki'
25     assert newplace.suffix == 'su'
26
27
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')
32
33     newplace = place.clone(attr={'a2': 'new', 'b2': 'foo'})
34
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'
40
41
42 def test_placeinfo_has_attr():
43     place = sanitizer.PlaceName('foo', 'ki', 'su')
44     place.set_attr('a1', 'v1')
45
46     assert place.has_attr('a1')
47     assert not place.has_attr('whatever')
48
49
50 def test_sanitizer_default(def_config):
51     san = sanitizer.PlaceSanitizer([{'step': 'split-name-list'}], def_config)
52
53     name, address =  san.process_names(PlaceInfo({'name': {'name:de:de': '1;2;3'},
54                                                   'address': {'street': 'Bald'}}))
55
56     assert len(name) == 3
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)
60
61     assert len(address) == 1
62     assert all(isinstance(n, sanitizer.PlaceName) for n in address)
63
64
65 @pytest.mark.parametrize('rules', [None, []])
66 def test_sanitizer_empty_list(def_config, rules):
67     san = sanitizer.PlaceSanitizer(rules, def_config)
68
69     name, address =  san.process_names(PlaceInfo({'name': {'name:de:de': '1;2;3'}}))
70
71     assert len(name) == 1
72     assert all(isinstance(n, sanitizer.PlaceName) for n in name)
73
74
75 def test_sanitizer_missing_step_definition(def_config):
76     with pytest.raises(UsageError):
77         san = sanitizer.PlaceSanitizer([{'id': 'split-name-list'}], def_config)