]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_country_info.py
custom country config loads correctly
[nominatim.git] / test / python / tools / test_country_info.py
1 # SPDX-License-Identifier: GPL-2.0-only
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2022 by the Nominatim developer community.
6 # For a full list of authors see the git log.
7 """
8 Tests for function that handle country properties.
9 """
10
11 import pytest
12
13 from nominatim.tools import country_info
14
15 @pytest.fixture(autouse=True)
16 def read_config(def_config, request):
17     if 'custom_country_config' in request.keywords:
18         return
19     country_info.setup_country_config(def_config)
20
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)
25
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')
29
30     partitions = temp_db_cursor.row_set("SELECT DISTINCT partition FROM country_name")
31     if no_partitions:
32         assert partitions == {(0, )}
33     else:
34         assert len(partitions) > 10
35
36     assert temp_db_cursor.table_exists('country_osm_grid')
37     assert temp_db_cursor.table_rows('country_osm_grid') > 100
38
39
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):
43
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"')))
47
48     assert temp_db_cursor.scalar("SELECT count(*) FROM country_name") == 2
49
50     tokenizer = tokenizer_mock()
51
52     country_info.create_country_names(temp_db_conn, tokenizer, languages)
53
54     assert len(tokenizer.analyser_cache['countries']) == 2
55
56     result_set = {k: set(v.values()) for k, v in tokenizer.analyser_cache['countries']}
57
58     if languages:
59         assert result_set == {'us' : set(('us', 'us1', 'United States')),
60                               'fr' : set(('fr', 'Fra', 'Fren'))}
61     else:
62         assert result_set == {'us' : set(('us', 'us1', 'us2', 'United States')),
63                               'fr' : set(('fr', 'Fra', 'Fren'))}
64
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("""
68 de:
69     partition: 3
70     names:
71         name:
72             default: Deutschland
73 """)
74     country_info.setup_country_config(project_env)
75     assert country_info._COUNTRY_INFO._info == {'de': {'partition': 3, 
76             'languages': [], 'names': {'name': {'default': 'Deutschland'}}}}
77
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("""
81 de:
82     partition: 3
83     languages: de
84     names:
85 """)
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': {}}}}
90
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("""
94 de:
95     partition: 3
96     languages: de
97 """)
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': {}}}}