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(
26 dedent(cfg), encoding='utf-8')
33 @pytest.mark.parametrize("no_partitions", (True, False))
34 def test_setup_country_tables(src_dir, temp_db_with_extensions, dsn, temp_db_cursor,
35 loaded_country, no_partitions):
36 country_info.setup_country_tables(dsn, src_dir / 'data', no_partitions)
38 assert temp_db_cursor.table_exists('country_name')
39 assert temp_db_cursor.table_rows('country_name') == \
40 temp_db_cursor.scalar(
41 'SELECT count(DISTINCT country_code) FROM country_name')
43 partitions = temp_db_cursor.row_set(
44 "SELECT DISTINCT partition FROM country_name")
46 assert partitions == {(0, )}
48 assert len(partitions) > 10
50 assert temp_db_cursor.table_exists('country_osm_grid')
51 assert temp_db_cursor.table_rows('country_osm_grid') > 100
54 @pytest.mark.parametrize("languages", (None, ['fr', 'en']))
55 def test_create_country_names(temp_db_with_extensions, temp_db_conn, temp_db_cursor,
56 table_factory, tokenizer_mock, languages, loaded_country):
58 table_factory('country_name', 'country_code varchar(2), name hstore',
59 content=(('us', '"name"=>"us1","name:af"=>"us2"'),
60 ('fr', '"name"=>"Fra", "name:en"=>"Fren"')))
62 assert temp_db_cursor.scalar("SELECT count(*) FROM country_name") == 2
64 tokenizer = tokenizer_mock()
66 country_info.create_country_names(temp_db_conn, tokenizer, languages)
68 assert len(tokenizer.analyser_cache['countries']) == 2
70 result_set = {k: set(v.values())
71 for k, v in tokenizer.analyser_cache['countries']}
74 assert result_set == {'us': set(('us', 'us1')),
75 'fr': set(('fr', 'Fra', 'Fren'))}
77 assert result_set == {'us': set(('us', 'us1', 'us2')),
78 'fr': set(('fr', 'Fra', 'Fren'))}
81 def test_setup_country_names_prefixes(env_with_country_config):
82 config = env_with_country_config("""\
94 default: United States
97 info = country_info._CountryInfo()
100 assert info.get('es')['names'] == {"name": "Espagñe",
102 "name:de": "Spanien"}
103 assert info.get('us')['names'] == {"name": "United States",
104 "name:en": "United States",
106 assert 'names' not in info.get('xx')
109 def test_setup_country_config_languages_not_loaded(env_with_country_config):
110 config = env_with_country_config("""\
117 info = country_info._CountryInfo()
119 assert dict(info.items()) == {'de': {'partition': 3,
121 'names': {'name': 'Deutschland'}}}
124 def test_setup_country_config_name_not_loaded(env_with_country_config):
125 config = env_with_country_config("""\
132 info = country_info._CountryInfo()
135 assert dict(info.items()) == {'de': {'partition': 3,
140 def test_setup_country_config_names_not_loaded(env_with_country_config):
141 config = env_with_country_config("""
147 info = country_info._CountryInfo()
150 assert dict(info.items()) == {'de': {'partition': 3,
155 def test_setup_country_config_special_character(env_with_country_config):
156 config = env_with_country_config("""
165 info = country_info._CountryInfo()
168 assert dict(info.items()) == {'bq': {'partition': 250,
170 'names': {'name': '\x85'}}}