]> git.openstreetmap.org Git - nominatim.git/blob - test/python/data/test_country_info.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / data / test_country_info.py
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 #
3 # This file is part of Nominatim. (https://nominatim.org)
4 #
5 # Copyright (C) 2025 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 from textwrap import dedent
11 import pytest
12
13 from nominatim_db.data import country_info
14
15
16 @pytest.fixture
17 def loaded_country(def_config):
18     country_info.setup_country_config(def_config)
19
20
21 @pytest.fixture
22 def env_with_country_config(project_env):
23
24     def _mk_config(cfg):
25         (project_env.project_dir / 'country_settings.yaml').write_text(dedent(cfg))
26
27         return project_env
28
29     return _mk_config
30
31
32 @pytest.mark.parametrize("no_partitions", (True, False))
33 def test_setup_country_tables(src_dir, temp_db_with_extensions, dsn, temp_db_cursor,
34                               loaded_country, no_partitions):
35     country_info.setup_country_tables(dsn, src_dir / 'data', no_partitions)
36
37     assert temp_db_cursor.table_exists('country_name')
38     assert temp_db_cursor.table_rows('country_name') == \
39         temp_db_cursor.scalar(
40             'SELECT count(DISTINCT country_code) FROM country_name')
41
42     partitions = temp_db_cursor.row_set(
43         "SELECT DISTINCT partition FROM country_name")
44     if no_partitions:
45         assert partitions == {(0, )}
46     else:
47         assert len(partitions) > 10
48
49     assert temp_db_cursor.table_exists('country_osm_grid')
50     assert temp_db_cursor.table_rows('country_osm_grid') > 100
51
52
53 @pytest.mark.parametrize("languages", (None, ['fr', 'en']))
54 def test_create_country_names(temp_db_with_extensions, temp_db_conn, temp_db_cursor,
55                               table_factory, tokenizer_mock, languages, loaded_country):
56
57     table_factory('country_name', 'country_code varchar(2), name hstore',
58                   content=(('us', '"name"=>"us1","name:af"=>"us2"'),
59                            ('fr', '"name"=>"Fra", "name:en"=>"Fren"')))
60
61     assert temp_db_cursor.scalar("SELECT count(*) FROM country_name") == 2
62
63     tokenizer = tokenizer_mock()
64
65     country_info.create_country_names(temp_db_conn, tokenizer, languages)
66
67     assert len(tokenizer.analyser_cache['countries']) == 2
68
69     result_set = {k: set(v.values())
70                   for k, v in tokenizer.analyser_cache['countries']}
71
72     if languages:
73         assert result_set == {'us': set(('us', 'us1')),
74                               'fr': set(('fr', 'Fra', 'Fren'))}
75     else:
76         assert result_set == {'us': set(('us', 'us1', 'us2')),
77                               'fr': set(('fr', 'Fra', 'Fren'))}
78
79
80 def test_setup_country_names_prefixes(env_with_country_config):
81     config = env_with_country_config("""\
82                                      es:
83                                        names:
84                                          name:
85                                            en: Spain
86                                            de: Spanien
87                                            default: Espagñe
88                                      us:
89                                        names:
90                                          short_name:
91                                            default: USA
92                                          name:
93                                            default: United States
94                                            en: United States
95                                      """)
96     info = country_info._CountryInfo()
97     info.load(config)
98
99     assert info.get('es')['names'] == {"name": "Espagñe",
100                                        "name:en": "Spain",
101                                        "name:de": "Spanien"}
102     assert info.get('us')['names'] == {"name": "United States",
103                                        "name:en": "United States",
104                                        "short_name": "USA"}
105     assert 'names' not in info.get('xx')
106
107
108 def test_setup_country_config_languages_not_loaded(env_with_country_config):
109     config = env_with_country_config("""\
110                                      de:
111                                          partition: 3
112                                          names:
113                                              name:
114                                                  default: Deutschland
115                                      """)
116     info = country_info._CountryInfo()
117     info.load(config)
118     assert dict(info.items()) == {'de': {'partition': 3,
119                                          'languages': [],
120                                          'names': {'name': 'Deutschland'}}}
121
122
123 def test_setup_country_config_name_not_loaded(env_with_country_config):
124     config = env_with_country_config("""\
125                                      de:
126                                          partition: 3
127                                          languages: de
128                                          names:
129                                      """)
130
131     info = country_info._CountryInfo()
132     info.load(config)
133
134     assert dict(info.items()) == {'de': {'partition': 3,
135                                          'languages': ['de'],
136                                          'names': {}}}
137
138
139 def test_setup_country_config_names_not_loaded(env_with_country_config):
140     config = env_with_country_config("""
141                                      de:
142                                          partition: 3
143                                          languages: de
144                                      """)
145
146     info = country_info._CountryInfo()
147     info.load(config)
148
149     assert dict(info.items()) == {'de': {'partition': 3,
150                                          'languages': ['de'],
151                                          'names': {}}}
152
153
154 def test_setup_country_config_special_character(env_with_country_config):
155     config = env_with_country_config("""
156                                      bq:
157                                          partition: 250
158                                          languages: nl
159                                          names:
160                                              name:
161                                                  default: "\\N"
162                                      """)
163
164     info = country_info._CountryInfo()
165     info.load(config)
166
167     assert dict(info.items()) == {'bq': {'partition': 250,
168                                          'languages': ['nl'],
169                                          'names': {'name': '\x85'}}}