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.
13 from nominatim.tools import country_info
16 def read_config(def_config):
17 country_info.setup_country_config(def_config)
20 @pytest.mark.parametrize("no_partitions", (True, False))
21 def test_setup_country_tables(src_dir, temp_db_with_extensions, dsn, temp_db_cursor,
22 def_config, no_partitions):
23 read_config(def_config)
24 country_info.setup_country_tables(dsn, src_dir / 'data', no_partitions)
26 assert temp_db_cursor.table_exists('country_name')
27 assert temp_db_cursor.table_rows('country_name') == \
28 temp_db_cursor.scalar(
29 'SELECT count(DISTINCT country_code) FROM country_name')
31 partitions = temp_db_cursor.row_set(
32 "SELECT DISTINCT partition FROM country_name")
34 assert partitions == {(0, )}
36 assert len(partitions) > 10
38 assert temp_db_cursor.table_exists('country_osm_grid')
39 assert temp_db_cursor.table_rows('country_osm_grid') > 100
42 @pytest.mark.parametrize("languages", (None, ' fr,en'))
43 def test_create_country_names(temp_db_with_extensions, temp_db_conn, temp_db_cursor,
44 table_factory, tokenizer_mock, languages, def_config):
45 read_config(def_config)
47 table_factory('country_name', 'country_code varchar(2), name hstore',
48 content=(('us', '"name"=>"us1","name:af"=>"us2"'),
49 ('fr', '"name"=>"Fra", "name:en"=>"Fren"')))
51 assert temp_db_cursor.scalar("SELECT count(*) FROM country_name") == 2
53 tokenizer = tokenizer_mock()
55 country_info.create_country_names(temp_db_conn, tokenizer, languages)
57 assert len(tokenizer.analyser_cache['countries']) == 2
59 result_set = {k: set(v.values())
60 for k, v in tokenizer.analyser_cache['countries']}
63 assert result_set == {'us': set(('us', 'us1', 'United States')),
64 'fr': set(('fr', 'Fra', 'Fren'))}
66 assert result_set == {'us': set(('us', 'us1', 'us2', 'United States')),
67 'fr': set(('fr', 'Fra', 'Fren'))}
70 def test_setup_country_config_languages_not_loaded(project_env):
71 (project_env.project_dir / 'country_settings.yaml').write_text("""
78 country_info._COUNTRY_INFO._info = None
79 country_info.setup_country_config(project_env)
80 assert country_info._COUNTRY_INFO._info == {'de': {'partition': 3,
81 'languages': [], 'names': {'name': {'default': 'Deutschland'}}}}
84 def test_setup_country_config_name_not_loaded(project_env):
85 (project_env.project_dir / 'country_settings.yaml').write_text("""
91 country_info._COUNTRY_INFO._info = None
92 country_info.setup_country_config(project_env)
93 assert country_info._COUNTRY_INFO._info == {'de': {'partition': 3,
94 'languages': ['de'], 'names': {'name': {}}}}
97 def test_setup_country_config_names_not_loaded(project_env):
98 (project_env.project_dir / 'country_settings.yaml').write_text("""
103 country_info._COUNTRY_INFO._info = None
104 country_info.setup_country_config(project_env)
105 assert country_info._COUNTRY_INFO._info == {'de': {'partition': 3,
106 'languages': ['de'], 'names': {'name': {}}}}
109 def test_setup_country_config_special_character(project_env):
110 (project_env.project_dir / 'country_settings.yaml').write_text("""
118 country_info._COUNTRY_INFO._info = None
119 country_info.setup_country_config(project_env)
120 assert country_info._COUNTRY_INFO._info == {'bq': {'partition': 250,
121 'languages': ['nl'], 'names': {'name': {'default': '\x85'}}}}