]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_refresh_address_levels.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / test_tools_refresh_address_levels.py
1 """
2 Tests for function for importing address ranks.
3 """
4 import json
5 import pytest
6 from pathlib import Path
7
8 from nominatim.tools.refresh import load_address_levels, load_address_levels_from_file
9
10 def test_load_ranks_def_config(temp_db_conn, temp_db_cursor, def_config):
11     load_address_levels_from_file(temp_db_conn, Path(def_config.ADDRESS_LEVEL_CONFIG))
12
13     assert temp_db_cursor.scalar('SELECT count(*) FROM address_levels') > 0
14
15 def test_load_ranks_from_file(temp_db_conn, temp_db_cursor, tmp_path):
16     test_file = tmp_path / 'test_levels.json'
17     test_file.write_text('[{"tags":{"place":{"sea":2}}}]')
18
19     load_address_levels_from_file(temp_db_conn, test_file)
20
21     assert temp_db_cursor.scalar('SELECT count(*) FROM address_levels') > 0
22
23
24 def test_load_ranks_from_broken_file(temp_db_conn, tmp_path):
25     test_file = tmp_path / 'test_levels.json'
26     test_file.write_text('[{"tags":"place":{"sea":2}}}]')
27
28     with pytest.raises(json.decoder.JSONDecodeError):
29         load_address_levels_from_file(temp_db_conn, test_file)
30
31
32 def test_load_ranks_country(temp_db_conn, temp_db_cursor):
33     load_address_levels(temp_db_conn, 'levels',
34                         [{"tags": {"place": {"village": 14}}},
35                          {"countries": ['de'],
36                           "tags": {"place": {"village": 15}}},
37                          {"countries": ['uk', 'us' ],
38                           "tags": {"place": {"village": 16}}}
39                         ])
40
41     assert temp_db_cursor.row_set('SELECT * FROM levels') == \
42            set([(None, 'place', 'village', 14, 14),
43                 ('de', 'place', 'village', 15, 15),
44                 ('uk', 'place', 'village', 16, 16),
45                 ('us', 'place', 'village', 16, 16),
46                ])
47
48
49 def test_load_ranks_default_value(temp_db_conn, temp_db_cursor):
50     load_address_levels(temp_db_conn, 'levels',
51                         [{"tags": {"boundary": {"": 28}}},
52                          {"countries": ['hu'],
53                           "tags": {"boundary": {"": 29}}}
54                         ])
55
56     assert temp_db_cursor.row_set('SELECT * FROM levels') == \
57            set([(None, 'boundary', None, 28, 28),
58                 ('hu', 'boundary', None, 29, 29),
59                ])
60
61
62 def test_load_ranks_multiple_keys(temp_db_conn, temp_db_cursor):
63     load_address_levels(temp_db_conn, 'levels',
64                         [{"tags":
65                             {"place": {"city": 14},
66                              "boundary": {"administrative2" : 4}}
67                          }])
68
69     assert temp_db_cursor.row_set('SELECT * FROM levels') == \
70            set([(None, 'place', 'city', 14, 14),
71                 (None, 'boundary', 'administrative2', 4, 4),
72                ])
73
74
75 def test_load_ranks_address(temp_db_conn, temp_db_cursor):
76     load_address_levels(temp_db_conn, 'levels',
77                         [{"tags":
78                             {"place": {"city": 14,
79                                        "town" : [14, 13]}}
80                          }])
81
82     assert temp_db_cursor.row_set('SELECT * FROM levels') == \
83            set([(None, 'place', 'city', 14, 14),
84                 (None, 'place', 'town', 14, 13),
85                ])