mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-02-15 02:47:59 +00:00
support arbitrary prefixes in country name list
This means we can now get rid of the last special cases for names.
This commit is contained in:
@@ -7,20 +7,30 @@
|
||||
"""
|
||||
Tests for function that handle country properties.
|
||||
"""
|
||||
|
||||
from textwrap import dedent
|
||||
import pytest
|
||||
|
||||
from nominatim.tools import country_info
|
||||
|
||||
|
||||
def read_config(def_config):
|
||||
@pytest.fixture
|
||||
def loaded_country(def_config):
|
||||
country_info.setup_country_config(def_config)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def env_with_country_config(project_env):
|
||||
|
||||
def _mk_config(cfg):
|
||||
(project_env.project_dir / 'country_settings.yaml').write_text(dedent(cfg))
|
||||
|
||||
return project_env
|
||||
|
||||
return _mk_config
|
||||
|
||||
|
||||
@pytest.mark.parametrize("no_partitions", (True, False))
|
||||
def test_setup_country_tables(src_dir, temp_db_with_extensions, dsn, temp_db_cursor,
|
||||
def_config, no_partitions):
|
||||
read_config(def_config)
|
||||
loaded_country, no_partitions):
|
||||
country_info.setup_country_tables(dsn, src_dir / 'data', no_partitions)
|
||||
|
||||
assert temp_db_cursor.table_exists('country_name')
|
||||
@@ -41,8 +51,7 @@ def test_setup_country_tables(src_dir, temp_db_with_extensions, dsn, temp_db_cur
|
||||
|
||||
@pytest.mark.parametrize("languages", (None, ' fr,en'))
|
||||
def test_create_country_names(temp_db_with_extensions, temp_db_conn, temp_db_cursor,
|
||||
table_factory, tokenizer_mock, languages, def_config):
|
||||
read_config(def_config)
|
||||
table_factory, tokenizer_mock, languages, loaded_country):
|
||||
|
||||
table_factory('country_name', 'country_code varchar(2), name hstore',
|
||||
content=(('us', '"name"=>"us1","name:af"=>"us2"'),
|
||||
@@ -60,62 +69,103 @@ def test_create_country_names(temp_db_with_extensions, temp_db_conn, temp_db_cur
|
||||
for k, v in tokenizer.analyser_cache['countries']}
|
||||
|
||||
if languages:
|
||||
assert result_set == {'us': set(('us', 'us1', 'United States')),
|
||||
assert result_set == {'us': set(('us', 'us1')),
|
||||
'fr': set(('fr', 'Fra', 'Fren'))}
|
||||
else:
|
||||
assert result_set == {'us': set(('us', 'us1', 'us2', 'United States')),
|
||||
assert result_set == {'us': set(('us', 'us1', 'us2')),
|
||||
'fr': set(('fr', 'Fra', 'Fren'))}
|
||||
|
||||
|
||||
def test_setup_country_config_languages_not_loaded(project_env):
|
||||
(project_env.project_dir / 'country_settings.yaml').write_text("""
|
||||
de:
|
||||
partition: 3
|
||||
names:
|
||||
name:
|
||||
default: Deutschland
|
||||
""")
|
||||
country_info._COUNTRY_INFO._info = None
|
||||
country_info.setup_country_config(project_env)
|
||||
assert country_info._COUNTRY_INFO._info == {'de': {'partition': 3,
|
||||
'languages': [], 'names': {'name': {'default': 'Deutschland'}}}}
|
||||
def test_setup_country_names_prefixes(env_with_country_config):
|
||||
config = env_with_country_config("""\
|
||||
es:
|
||||
names:
|
||||
name:
|
||||
en: Spain
|
||||
de: Spanien
|
||||
default: Espagñe
|
||||
us:
|
||||
names:
|
||||
short_name:
|
||||
default: USA
|
||||
name:
|
||||
default: United States
|
||||
en: United States
|
||||
""")
|
||||
info = country_info._CountryInfo()
|
||||
info.load(config)
|
||||
|
||||
assert info.get('es')['names'] == {"name": "Espagñe",
|
||||
"name:en": "Spain",
|
||||
"name:de": "Spanien"}
|
||||
assert info.get('us')['names'] == {"name": "United States",
|
||||
"name:en": "United States",
|
||||
"short_name": "USA"}
|
||||
assert 'names' not in info.get('xx')
|
||||
|
||||
|
||||
def test_setup_country_config_name_not_loaded(project_env):
|
||||
(project_env.project_dir / 'country_settings.yaml').write_text("""
|
||||
de:
|
||||
partition: 3
|
||||
languages: de
|
||||
names:
|
||||
""")
|
||||
country_info._COUNTRY_INFO._info = None
|
||||
country_info.setup_country_config(project_env)
|
||||
assert country_info._COUNTRY_INFO._info == {'de': {'partition': 3,
|
||||
'languages': ['de'], 'names': {'name': {}}}}
|
||||
def test_setup_country_config_languages_not_loaded(env_with_country_config):
|
||||
config = env_with_country_config("""\
|
||||
de:
|
||||
partition: 3
|
||||
names:
|
||||
name:
|
||||
default: Deutschland
|
||||
""")
|
||||
info = country_info._CountryInfo()
|
||||
info.load(config)
|
||||
assert dict(info.items()) == {'de': {'partition': 3,
|
||||
'languages': [],
|
||||
'names': {'name': 'Deutschland'}}}
|
||||
|
||||
|
||||
def test_setup_country_config_names_not_loaded(project_env):
|
||||
(project_env.project_dir / 'country_settings.yaml').write_text("""
|
||||
de:
|
||||
partition: 3
|
||||
languages: de
|
||||
""")
|
||||
country_info._COUNTRY_INFO._info = None
|
||||
country_info.setup_country_config(project_env)
|
||||
assert country_info._COUNTRY_INFO._info == {'de': {'partition': 3,
|
||||
'languages': ['de'], 'names': {'name': {}}}}
|
||||
def test_setup_country_config_name_not_loaded(env_with_country_config):
|
||||
config = env_with_country_config("""\
|
||||
de:
|
||||
partition: 3
|
||||
languages: de
|
||||
names:
|
||||
""")
|
||||
|
||||
info = country_info._CountryInfo()
|
||||
info.load(config)
|
||||
|
||||
assert dict(info.items()) == {'de': {'partition': 3,
|
||||
'languages': ['de'],
|
||||
'names': {}
|
||||
}}
|
||||
|
||||
|
||||
def test_setup_country_config_special_character(project_env):
|
||||
(project_env.project_dir / 'country_settings.yaml').write_text("""
|
||||
bq:
|
||||
partition: 250
|
||||
languages: nl
|
||||
names:
|
||||
name:
|
||||
default: "\\N"
|
||||
""")
|
||||
country_info._COUNTRY_INFO._info = None
|
||||
country_info.setup_country_config(project_env)
|
||||
assert country_info._COUNTRY_INFO._info == {'bq': {'partition': 250,
|
||||
'languages': ['nl'], 'names': {'name': {'default': '\x85'}}}}
|
||||
def test_setup_country_config_names_not_loaded(env_with_country_config):
|
||||
config = env_with_country_config("""
|
||||
de:
|
||||
partition: 3
|
||||
languages: de
|
||||
""")
|
||||
|
||||
info = country_info._CountryInfo()
|
||||
info.load(config)
|
||||
|
||||
assert dict(info.items()) == {'de': {'partition': 3,
|
||||
'languages': ['de'],
|
||||
'names': {}
|
||||
}}
|
||||
|
||||
|
||||
def test_setup_country_config_special_character(env_with_country_config):
|
||||
config = env_with_country_config("""
|
||||
bq:
|
||||
partition: 250
|
||||
languages: nl
|
||||
names:
|
||||
name:
|
||||
default: "\\N"
|
||||
""")
|
||||
|
||||
info = country_info._CountryInfo()
|
||||
info.load(config)
|
||||
|
||||
assert dict(info.items()) == {'bq': {'partition': 250,
|
||||
'languages': ['nl'],
|
||||
'names': {'name': '\x85'}
|
||||
}}
|
||||
|
||||
Reference in New Issue
Block a user