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
15 @pytest.fixture(autouse=True)
16 def read_config(def_config, request):
17 if 'custom_country_config' in request.keywords:
19 country_info.setup_country_config(def_config)
21 @pytest.mark.parametrize("no_partitions", (True, False))
22 def test_setup_country_tables(src_dir, temp_db_with_extensions, dsn, temp_db_cursor,
23 def_config, no_partitions):
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('SELECT count(DISTINCT country_code) FROM country_name')
30 partitions = temp_db_cursor.row_set("SELECT DISTINCT partition FROM country_name")
32 assert partitions == {(0, )}
34 assert len(partitions) > 10
36 assert temp_db_cursor.table_exists('country_osm_grid')
37 assert temp_db_cursor.table_rows('country_osm_grid') > 100
40 @pytest.mark.parametrize("languages", (None, ' fr,en'))
41 def test_create_country_names(temp_db_with_extensions, temp_db_conn, temp_db_cursor,
42 table_factory, tokenizer_mock, languages):
44 table_factory('country_name', 'country_code varchar(2), name hstore',
45 content=(('us', '"name"=>"us1","name:af"=>"us2"'),
46 ('fr', '"name"=>"Fra", "name:en"=>"Fren"')))
48 assert temp_db_cursor.scalar("SELECT count(*) FROM country_name") == 2
50 tokenizer = tokenizer_mock()
52 country_info.create_country_names(temp_db_conn, tokenizer, languages)
54 assert len(tokenizer.analyser_cache['countries']) == 2
56 result_set = {k: set(v.values()) for k, v in tokenizer.analyser_cache['countries']}
59 assert result_set == {'us' : set(('us', 'us1', 'United States')),
60 'fr' : set(('fr', 'Fra', 'Fren'))}
62 assert result_set == {'us' : set(('us', 'us1', 'us2', 'United States')),
63 'fr' : set(('fr', 'Fra', 'Fren'))}
65 @pytest.mark.custom_country_config
66 def test_setup_country_config_languages_not_loaded(project_env):
67 (project_env.project_dir / 'country_settings.yaml').write_text("""
74 country_info.setup_country_config(project_env)
75 assert country_info._COUNTRY_INFO._info == {'de': {'partition': 3,
76 'languages': [], 'names': {'name': {'default': 'Deutschland'}}}}
78 @pytest.mark.custom_country_config
79 def test_setup_country_config_name_not_loaded(project_env):
80 (project_env.project_dir / 'country_settings.yaml').write_text("""
86 country_info._COUNTRY_INFO._info = None
87 country_info.setup_country_config(project_env)
88 assert country_info._COUNTRY_INFO._info == {'de': {'partition': 3,
89 'languages': ['de'], 'names': {'name': {}}}}
91 @pytest.mark.custom_country_config
92 def test_setup_country_config_names_not_loaded(project_env):
93 (project_env.project_dir / 'country_settings.yaml').write_text("""
98 country_info._COUNTRY_INFO._info = None
99 country_info.setup_country_config(project_env)
100 assert country_info._COUNTRY_INFO._info == {'de': {'partition': 3,
101 'languages': ['de'], 'names': {'name': {}}}}