]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_refresh_address_levels.py
Merge pull request #2539 from lonvia/clean-up-python-tests
[nominatim.git] / test / python / tools / test_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(project_env, temp_db_conn, temp_db_cursor):
17     test_file = project_env.project_dir / 'address-levels.json'
18     test_file.write_text('[{"tags":{"place":{"sea":2}}}]')
19
20     load_address_levels_from_config(temp_db_conn, project_env)
21
22     assert temp_db_cursor.table_rows('address_levels') == 1
23
24
25 def test_load_ranks_from_broken_file(project_env, temp_db_conn):
26     test_file = project_env.project_dir / 'address-levels.json'
27     test_file.write_text('[{"tags":"place":{"sea":2}}}]')
28
29     with pytest.raises(json.decoder.JSONDecodeError):
30         load_address_levels_from_config(temp_db_conn, project_env)
31
32
33 def test_load_ranks_country(temp_db_conn, temp_db_cursor):
34     load_address_levels(temp_db_conn, 'levels',
35                         [{"tags": {"place": {"village": 14}}},
36                          {"countries": ['de'],
37                           "tags": {"place": {"village": 15}}},
38                          {"countries": ['uk', 'us'],
39                           "tags": {"place": {"village": 16}}}
40                         ])
41
42     assert temp_db_cursor.row_set('SELECT * FROM levels') == \
43            set([(None, 'place', 'village', 14, 14),
44                 ('de', 'place', 'village', 15, 15),
45                 ('uk', 'place', 'village', 16, 16),
46                 ('us', 'place', 'village', 16, 16),
47                ])
48
49
50 def test_load_ranks_default_value(temp_db_conn, temp_db_cursor):
51     load_address_levels(temp_db_conn, 'levels',
52                         [{"tags": {"boundary": {"": 28}}},
53                          {"countries": ['hu'],
54                           "tags": {"boundary": {"": 29}}}
55                         ])
56
57     assert temp_db_cursor.row_set('SELECT * FROM levels') == \
58            set([(None, 'boundary', None, 28, 28),
59                 ('hu', 'boundary', None, 29, 29),
60                ])
61
62
63 def test_load_ranks_multiple_keys(temp_db_conn, temp_db_cursor):
64     load_address_levels(temp_db_conn, 'levels',
65                         [{"tags": {"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": {"place": {"city": 14,
78                                              "town" : [14, 13]}}
79                          }])
80
81     assert temp_db_cursor.row_set('SELECT * FROM levels') == \
82            set([(None, 'place', 'city', 14, 14),
83                 (None, 'place', 'town', 14, 13),
84                ])