]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tokenizer/test_place_sanitizer.py
better error reporting when API script does not exist
[nominatim.git] / test / python / tokenizer / test_place_sanitizer.py
1 """
2 Tests for execution of the sanitztion step.
3 """
4 import pytest
5
6 from nominatim.errors import UsageError
7 import nominatim.tokenizer.place_sanitizer as sanitizer
8 from nominatim.indexer.place_info import PlaceInfo
9
10
11 def test_placeinfo_clone_new_name():
12     place = sanitizer.PlaceName('foo', 'ki', 'su')
13
14     newplace = place.clone(name='bar')
15
16     assert place.name == 'foo'
17     assert newplace.name == 'bar'
18     assert newplace.kind == 'ki'
19     assert newplace.suffix == 'su'
20
21
22 def test_placeinfo_clone_merge_attr():
23     place = sanitizer.PlaceName('foo', 'ki', 'su')
24     place.set_attr('a1', 'v1')
25     place.set_attr('a2', 'v2')
26
27     newplace = place.clone(attr={'a2': 'new', 'b2': 'foo'})
28
29     assert place.get_attr('a2') == 'v2'
30     assert place.get_attr('b2') is None
31     assert newplace.get_attr('a1') == 'v1'
32     assert newplace.get_attr('a2') == 'new'
33     assert newplace.get_attr('b2') == 'foo'
34
35
36 def test_placeinfo_has_attr():
37     place = sanitizer.PlaceName('foo', 'ki', 'su')
38     place.set_attr('a1', 'v1')
39
40     assert place.has_attr('a1')
41     assert not place.has_attr('whatever')
42
43
44 def test_sanitizer_default():
45     san = sanitizer.PlaceSanitizer([{'step': 'split-name-list'}])
46
47     name, address =  san.process_names(PlaceInfo({'name': {'name:de:de': '1;2;3'},
48                                                   'address': {'street': 'Bald'}}))
49
50     assert len(name) == 3
51     assert all(isinstance(n, sanitizer.PlaceName) for n in name)
52     assert all(n.kind == 'name'  for n in name)
53     assert all(n.suffix == 'de:de'  for n in name)
54
55     assert len(address) == 1
56     assert all(isinstance(n, sanitizer.PlaceName) for n in address)
57
58
59 @pytest.mark.parametrize('rules', [None, []])
60 def test_sanitizer_empty_list(rules):
61     san = sanitizer.PlaceSanitizer(rules)
62
63     name, address =  san.process_names(PlaceInfo({'name': {'name:de:de': '1;2;3'}}))
64
65     assert len(name) == 1
66     assert all(isinstance(n, sanitizer.PlaceName) for n in name)
67
68
69 def test_sanitizer_missing_step_definition():
70     with pytest.raises(UsageError):
71         san = sanitizer.PlaceSanitizer([{'id': 'split-name-list'}])