1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2024 by the Nominatim developer community.
 
   6 # For a full list of authors see the git log.
 
   8 Tests for function for importing address ranks.
 
  11 from pathlib import Path
 
  15 from nominatim_db.tools.refresh import load_address_levels, load_address_levels_from_config
 
  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)
 
  20     assert temp_db_cursor.table_rows('address_levels') > 0
 
  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}}}]')
 
  26     load_address_levels_from_config(temp_db_conn, project_env)
 
  28     assert temp_db_cursor.table_rows('address_levels') == 1
 
  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}}}]')
 
  35     with pytest.raises(json.decoder.JSONDecodeError):
 
  36         load_address_levels_from_config(temp_db_conn, project_env)
 
  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}}},
 
  43                           "tags": {"place": {"village": 15}}},
 
  44                          {"countries": ['uk', 'us'],
 
  45                           "tags": {"place": {"village": 16}}}
 
  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),
 
  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}}},
 
  60                           "tags": {"boundary": {"": 29}}}
 
  63     assert temp_db_cursor.row_set('SELECT * FROM levels') == \
 
  64            set([(None, 'boundary', None, 28, 28),
 
  65                 ('hu', 'boundary', None, 29, 29),
 
  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}}
 
  75     assert temp_db_cursor.row_set('SELECT * FROM levels') == \
 
  76            set([(None, 'place', 'city', 14, 14),
 
  77                 (None, 'boundary', 'administrative2', 4, 4),
 
  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,
 
  87     assert temp_db_cursor.row_set('SELECT * FROM levels') == \
 
  88            set([(None, 'place', 'city', 14, 14),
 
  89                 (None, 'place', 'town', 14, 13),