]> git.openstreetmap.org Git - nominatim.git/blob - test/python/tools/test_country_info.py
Added unit tests for loading country info from yaml file
[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):
17     country_info.setup_country_config(def_config)
18
19 @pytest.mark.parametrize("no_partitions", (True, False))
20 def test_setup_country_tables(src_dir, temp_db_with_extensions, dsn, temp_db_cursor,
21                               def_config, no_partitions):
22     country_info.setup_country_tables(dsn, src_dir / 'data', no_partitions)
23
24     assert temp_db_cursor.table_exists('country_name')
25     assert temp_db_cursor.table_rows('country_name') == \
26              temp_db_cursor.scalar('SELECT count(DISTINCT country_code) FROM country_name')
27
28     partitions = temp_db_cursor.row_set("SELECT DISTINCT partition FROM country_name")
29     if no_partitions:
30         assert partitions == {(0, )}
31     else:
32         assert len(partitions) > 10
33
34     assert temp_db_cursor.table_exists('country_osm_grid')
35     assert temp_db_cursor.table_rows('country_osm_grid') > 100
36
37
38 @pytest.mark.parametrize("languages", (None, ' fr,en'))
39 def test_create_country_names(temp_db_with_extensions, temp_db_conn, temp_db_cursor,
40                               table_factory, tokenizer_mock, languages):
41
42     table_factory('country_name', 'country_code varchar(2), name hstore',
43                   content=(('us', '"name"=>"us1","name:af"=>"us2"'),
44                            ('fr', '"name"=>"Fra", "name:en"=>"Fren"')))
45
46     assert temp_db_cursor.scalar("SELECT count(*) FROM country_name") == 2
47
48     tokenizer = tokenizer_mock()
49
50     country_info.create_country_names(temp_db_conn, tokenizer, languages)
51
52     assert len(tokenizer.analyser_cache['countries']) == 2
53
54     result_set = {k: set(v.values()) for k, v in tokenizer.analyser_cache['countries']}
55
56     if languages:
57         assert result_set == {'us' : set(('us', 'us1', 'United States')),
58                               'fr' : set(('fr', 'Fra', 'Fren'))}
59     else:
60         assert result_set == {'us' : set(('us', 'us1', 'us2', 'United States')),
61                               'fr' : set(('fr', 'Fra', 'Fren'))}
62
63 @pytest.mark.parametrize("yaml_file_content", (
64 """
65 de:
66     partition: 3
67     names: 
68         name: 
69             default: Deutschland
70 """,
71 """
72 de:
73     partition: 3
74     languages: de
75     names:
76 """,
77 """
78 de:
79     partition: 3
80     languages: de
81 """
82 ))
83 def test_load(project_env, def_config, yaml_file_content):
84     (project_env.project_dir / 'country_settings.yaml').write_text(yaml_file_content)
85     
86     country_info._COUNTRY_INFO._info = def_config.load_sub_configuration(
87         (project_env.project_dir / 'country_settings.yaml'))
88     
89     for prop in country_info._COUNTRY_INFO._info.values():
90         if 'languages' not in prop:
91             prop['languages'] = []
92             assert country_info._COUNTRY_INFO._info == {'de': {'partition': 3,
93             'languages': [], 'names': {'name': {'default': 'Deutschland'}}}}
94         if 'names' not in prop or prop['names'] is None:
95             prop['names'] = {'name': {}}
96             assert country_info._COUNTRY_INFO._info == {'de': {'partition': 3,
97             'languages': 'de', 'names': {'name': {}}}}