1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 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.tools import country_info
16 def loaded_country(def_config):
17 country_info.setup_country_config(def_config)
21 def env_with_country_config(project_env):
24 (project_env.project_dir / 'country_settings.yaml').write_text(dedent(cfg))
31 @pytest.mark.parametrize("no_partitions", (True, False))
32 def test_setup_country_tables(src_dir, temp_db_with_extensions, dsn, temp_db_cursor,
33 loaded_country, no_partitions):
34 country_info.setup_country_tables(dsn, src_dir / 'data', no_partitions)
36 assert temp_db_cursor.table_exists('country_name')
37 assert temp_db_cursor.table_rows('country_name') == \
38 temp_db_cursor.scalar(
39 'SELECT count(DISTINCT country_code) FROM country_name')
41 partitions = temp_db_cursor.row_set(
42 "SELECT DISTINCT partition FROM country_name")
44 assert partitions == {(0, )}
46 assert len(partitions) > 10
48 assert temp_db_cursor.table_exists('country_osm_grid')
49 assert temp_db_cursor.table_rows('country_osm_grid') > 100
52 @pytest.mark.parametrize("languages", (None, ' fr,en'))
53 def test_create_country_names(temp_db_with_extensions, temp_db_conn, temp_db_cursor,
54 table_factory, tokenizer_mock, languages, loaded_country):
56 table_factory('country_name', 'country_code varchar(2), name hstore',
57 content=(('us', '"name"=>"us1","name:af"=>"us2"'),
58 ('fr', '"name"=>"Fra", "name:en"=>"Fren"')))
60 assert temp_db_cursor.scalar("SELECT count(*) FROM country_name") == 2
62 tokenizer = tokenizer_mock()
64 country_info.create_country_names(temp_db_conn, tokenizer, languages)
66 assert len(tokenizer.analyser_cache['countries']) == 2
68 result_set = {k: set(v.values())
69 for k, v in tokenizer.analyser_cache['countries']}
72 assert result_set == {'us': set(('us', 'us1')),
73 'fr': set(('fr', 'Fra', 'Fren'))}
75 assert result_set == {'us': set(('us', 'us1', 'us2')),
76 'fr': set(('fr', 'Fra', 'Fren'))}
79 def test_setup_country_names_prefixes(env_with_country_config):
80 config = env_with_country_config("""\
92 default: United States
95 info = country_info._CountryInfo()
98 assert info.get('es')['names'] == {"name": "Espagñe",
100 "name:de": "Spanien"}
101 assert info.get('us')['names'] == {"name": "United States",
102 "name:en": "United States",
104 assert 'names' not in info.get('xx')
107 def test_setup_country_config_languages_not_loaded(env_with_country_config):
108 config = env_with_country_config("""\
115 info = country_info._CountryInfo()
117 assert dict(info.items()) == {'de': {'partition': 3,
119 'names': {'name': 'Deutschland'}}}
122 def test_setup_country_config_name_not_loaded(env_with_country_config):
123 config = env_with_country_config("""\
130 info = country_info._CountryInfo()
133 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,
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'}