1 # SPDX-License-Identifier: GPL-3.0-or-later
 
   3 # This file is part of Nominatim. (https://nominatim.org)
 
   5 # Copyright (C) 2025 by the Nominatim developer community.
 
   6 # For a full list of authors see the git log.
 
   8 Tests for function that handle country properties.
 
  10 from textwrap import dedent
 
  13 from nominatim_db.data import country_info
 
  17 def loaded_country(def_config):
 
  18     country_info.setup_country_config(def_config)
 
  22 def env_with_country_config(project_env):
 
  25         (project_env.project_dir / 'country_settings.yaml').write_text(dedent(cfg))
 
  32 @pytest.mark.parametrize("no_partitions", (True, False))
 
  33 def test_setup_country_tables(src_dir, temp_db_with_extensions, dsn, temp_db_cursor,
 
  34                               loaded_country, no_partitions):
 
  35     country_info.setup_country_tables(dsn, src_dir / 'data', no_partitions)
 
  37     assert temp_db_cursor.table_exists('country_name')
 
  38     assert temp_db_cursor.table_rows('country_name') == \
 
  39         temp_db_cursor.scalar(
 
  40             'SELECT count(DISTINCT country_code) FROM country_name')
 
  42     partitions = temp_db_cursor.row_set(
 
  43         "SELECT DISTINCT partition FROM country_name")
 
  45         assert partitions == {(0, )}
 
  47         assert len(partitions) > 10
 
  49     assert temp_db_cursor.table_exists('country_osm_grid')
 
  50     assert temp_db_cursor.table_rows('country_osm_grid') > 100
 
  53 @pytest.mark.parametrize("languages", (None, ['fr', 'en']))
 
  54 def test_create_country_names(temp_db_with_extensions, temp_db_conn, temp_db_cursor,
 
  55                               table_factory, tokenizer_mock, languages, loaded_country):
 
  57     table_factory('country_name', 'country_code varchar(2), name hstore',
 
  58                   content=(('us', '"name"=>"us1","name:af"=>"us2"'),
 
  59                            ('fr', '"name"=>"Fra", "name:en"=>"Fren"')))
 
  61     assert temp_db_cursor.scalar("SELECT count(*) FROM country_name") == 2
 
  63     tokenizer = tokenizer_mock()
 
  65     country_info.create_country_names(temp_db_conn, tokenizer, languages)
 
  67     assert len(tokenizer.analyser_cache['countries']) == 2
 
  69     result_set = {k: set(v.values())
 
  70                   for k, v in tokenizer.analyser_cache['countries']}
 
  73         assert result_set == {'us': set(('us', 'us1')),
 
  74                               'fr': set(('fr', 'Fra', 'Fren'))}
 
  76         assert result_set == {'us': set(('us', 'us1', 'us2')),
 
  77                               'fr': set(('fr', 'Fra', 'Fren'))}
 
  80 def test_setup_country_names_prefixes(env_with_country_config):
 
  81     config = env_with_country_config("""\
 
  93                                            default: United States
 
  96     info = country_info._CountryInfo()
 
  99     assert info.get('es')['names'] == {"name": "Espagñe",
 
 101                                        "name:de": "Spanien"}
 
 102     assert info.get('us')['names'] == {"name": "United States",
 
 103                                        "name:en": "United States",
 
 105     assert 'names' not in info.get('xx')
 
 108 def test_setup_country_config_languages_not_loaded(env_with_country_config):
 
 109     config = env_with_country_config("""\
 
 116     info = country_info._CountryInfo()
 
 118     assert dict(info.items()) == {'de': {'partition': 3,
 
 120                                          'names': {'name': 'Deutschland'}}}
 
 123 def test_setup_country_config_name_not_loaded(env_with_country_config):
 
 124     config = env_with_country_config("""\
 
 131     info = country_info._CountryInfo()
 
 134     assert dict(info.items()) == {'de': {'partition': 3,
 
 139 def test_setup_country_config_names_not_loaded(env_with_country_config):
 
 140     config = env_with_country_config("""
 
 146     info = country_info._CountryInfo()
 
 149     assert dict(info.items()) == {'de': {'partition': 3,
 
 154 def test_setup_country_config_special_character(env_with_country_config):
 
 155     config = env_with_country_config("""
 
 164     info = country_info._CountryInfo()
 
 167     assert dict(info.items()) == {'bq': {'partition': 250,
 
 169                                          'names': {'name': '\x85'}}}