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