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