]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_refresh_address_levels.py
adapt tests to new TIGER CSV format
[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_file
10
11 def test_load_ranks_def_config(temp_db_conn, temp_db_cursor, def_config):
12     load_address_levels_from_file(temp_db_conn, Path(def_config.ADDRESS_LEVEL_CONFIG))
13
14     assert temp_db_cursor.scalar('SELECT count(*) FROM address_levels') > 0
15
16 def test_load_ranks_from_file(temp_db_conn, temp_db_cursor, tmp_path):
17     test_file = tmp_path / 'test_levels.json'
18     test_file.write_text('[{"tags":{"place":{"sea":2}}}]')
19
20     load_address_levels_from_file(temp_db_conn, test_file)
21
22     assert temp_db_cursor.scalar('SELECT count(*) FROM address_levels') > 0
23
24
25 def test_load_ranks_from_broken_file(temp_db_conn, tmp_path):
26     test_file = tmp_path / 'test_levels.json'
27     test_file.write_text('[{"tags":"place":{"sea":2}}}]')
28
29     with pytest.raises(json.decoder.JSONDecodeError):
30         load_address_levels_from_file(temp_db_conn, test_file)
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":
66                             {"place": {"city": 14},
67                              "boundary": {"administrative2" : 4}}
68                          }])
69
70     assert temp_db_cursor.row_set('SELECT * FROM levels') == \
71            set([(None, 'place', 'city', 14, 14),
72                 (None, 'boundary', 'administrative2', 4, 4),
73                ])
74
75
76 def test_load_ranks_address(temp_db_conn, temp_db_cursor):
77     load_address_levels(temp_db_conn, 'levels',
78                         [{"tags":
79                             {"place": {"city": 14,
80                                        "town" : [14, 13]}}
81                          }])
82
83     assert temp_db_cursor.row_set('SELECT * FROM levels') == \
84            set([(None, 'place', 'city', 14, 14),
85                 (None, 'place', 'town', 14, 13),
86                ])