]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_country_info.py
move country names into separate include files
[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
16 def read_config(def_config):
17     country_info.setup_country_config(def_config)
18
19
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)
25
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')
30
31     partitions = temp_db_cursor.row_set(
32         "SELECT DISTINCT partition FROM country_name")
33     if no_partitions:
34         assert partitions == {(0, )}
35     else:
36         assert len(partitions) > 10
37
38     assert temp_db_cursor.table_exists('country_osm_grid')
39     assert temp_db_cursor.table_rows('country_osm_grid') > 100
40
41
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)
46
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"')))
50
51     assert temp_db_cursor.scalar("SELECT count(*) FROM country_name") == 2
52
53     tokenizer = tokenizer_mock()
54
55     country_info.create_country_names(temp_db_conn, tokenizer, languages)
56
57     assert len(tokenizer.analyser_cache['countries']) == 2
58
59     result_set = {k: set(v.values())
60                   for k, v in tokenizer.analyser_cache['countries']}
61
62     if languages:
63         assert result_set == {'us': set(('us', 'us1', 'United States')),
64                               'fr': set(('fr', 'Fra', 'Fren'))}
65     else:
66         assert result_set == {'us': set(('us', 'us1', 'us2', 'United States')),
67                               'fr': set(('fr', 'Fra', 'Fren'))}
68
69
70 def test_setup_country_config_languages_not_loaded(project_env):
71     (project_env.project_dir / 'country_settings.yaml').write_text("""
72 de:
73     partition: 3
74     names:
75         name:
76             default: Deutschland
77 """)
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'}}}}
82
83
84 def test_setup_country_config_name_not_loaded(project_env):
85     (project_env.project_dir / 'country_settings.yaml').write_text("""
86 de:
87     partition: 3
88     languages: de
89     names:
90 """)
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': {}}}}
95
96
97 def test_setup_country_config_names_not_loaded(project_env):
98     (project_env.project_dir / 'country_settings.yaml').write_text("""
99 de:
100     partition: 3
101     languages: de
102 """)
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': {}}}}
107
108
109 def test_setup_country_config_special_character(project_env):
110     (project_env.project_dir / 'country_settings.yaml').write_text("""
111 bq:
112     partition: 250
113     languages: nl
114     names: 
115         name: 
116             default: "\\N"
117 """)
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'}}}}