]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_refresh_address_levels.py
enable flake for Python tests
[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) 2025 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
12 import pytest
13
14 from nominatim_db.tools.refresh import load_address_levels, load_address_levels_from_config
15
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
23 def test_load_ranks_from_project_dir(project_env, temp_db_conn, temp_db_cursor):
24     test_file = project_env.project_dir / 'address-levels.json'
25     test_file.write_text('[{"tags":{"place":{"sea":2}}}]')
26
27     load_address_levels_from_config(temp_db_conn, project_env)
28
29     assert temp_db_cursor.table_rows('address_levels') == 1
30
31
32 def test_load_ranks_from_broken_file(project_env, temp_db_conn):
33     test_file = project_env.project_dir / 'address-levels.json'
34     test_file.write_text('[{"tags":"place":{"sea":2}}}]')
35
36     with pytest.raises(json.decoder.JSONDecodeError):
37         load_address_levels_from_config(temp_db_conn, project_env)
38
39
40 def test_load_ranks_country(temp_db_conn, temp_db_cursor):
41     load_address_levels(temp_db_conn, 'levels',
42                         [{"tags": {"place": {"village": 14}}},
43                          {"countries": ['de'],
44                           "tags": {"place": {"village": 15}}},
45                          {"countries": ['uk', 'us'],
46                           "tags": {"place": {"village": 16}}}
47                          ])
48
49     assert temp_db_cursor.row_set('SELECT * FROM levels') == \
50            set([(None, 'place', 'village', 14, 14),
51                 ('de', 'place', 'village', 15, 15),
52                 ('uk', 'place', 'village', 16, 16),
53                 ('us', 'place', 'village', 16, 16),
54                 ])
55
56
57 def test_load_ranks_default_value(temp_db_conn, temp_db_cursor):
58     load_address_levels(temp_db_conn, 'levels',
59                         [{"tags": {"boundary": {"": 28}}},
60                          {"countries": ['hu'],
61                           "tags": {"boundary": {"": 29}}}
62                          ])
63
64     assert temp_db_cursor.row_set('SELECT * FROM levels') == \
65            set([(None, 'boundary', None, 28, 28),
66                 ('hu', 'boundary', None, 29, 29),
67                 ])
68
69
70 def test_load_ranks_multiple_keys(temp_db_conn, temp_db_cursor):
71     load_address_levels(temp_db_conn, 'levels',
72                         [{"tags": {"place": {"city": 14},
73                                    "boundary": {"administrative2": 4}}
74                           }])
75
76     assert temp_db_cursor.row_set('SELECT * FROM levels') == \
77            set([(None, 'place', 'city', 14, 14),
78                 (None, 'boundary', 'administrative2', 4, 4),
79                 ])
80
81
82 def test_load_ranks_address(temp_db_conn, temp_db_cursor):
83     load_address_levels(temp_db_conn, 'levels',
84                         [{"tags": {"place": {"city": 14,
85                                              "town": [14, 13]}}
86                           }])
87
88     assert temp_db_cursor.row_set('SELECT * FROM levels') == \
89            set([(None, 'place', 'city', 14, 14),
90                 (None, 'place', 'town', 14, 13),
91                 ])