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