]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_import_special_phrases.py
0adc28502a0d08e91f28bae2152074f6ef204109
[nominatim.git] / test / python / test_tools_import_special_phrases.py
1 """
2     Tests for import special phrases functions
3 """
4 from pathlib import Path
5 import pytest
6 from nominatim.tools.special_phrases import SpecialPhrasesImporter
7
8 TEST_BASE_DIR = Path(__file__) / '..' / '..'
9
10 def test_process_amenity_with_operator(special_phrases_importer, getorcreate_amenityoperator_funcs):
11     special_phrases_importer._process_amenity('', '', '', '', 'near')
12     special_phrases_importer._process_amenity('', '', '', '', 'in')
13
14 def test_process_amenity_without_operator(special_phrases_importer, getorcreate_amenity_funcs):
15     special_phrases_importer._process_amenity('', '', '', '', '')
16
17 def test_create_place_classtype_indexes(temp_db_conn, special_phrases_importer):
18     phrase_class = 'class'
19     phrase_type = 'type'
20     table_name = 'place_classtype_{}_{}'.format(phrase_class, phrase_type)
21     index_prefix = 'idx_place_classtype_{}_{}_'.format(phrase_class, phrase_type)
22
23     with temp_db_conn.cursor() as temp_db_cursor:
24         temp_db_cursor.execute("CREATE EXTENSION postgis;")
25         temp_db_cursor.execute('CREATE TABLE {}(place_id BIGINT, centroid GEOMETRY)'.format(table_name))
26
27     special_phrases_importer._create_place_classtype_indexes('', phrase_class, phrase_type)
28
29     centroid_index_exists = temp_db_conn.index_exists(index_prefix + 'centroid')
30     place_id_index_exists = temp_db_conn.index_exists(index_prefix + 'place_id')
31
32     assert centroid_index_exists and place_id_index_exists
33
34 def test_create_place_classtype_table(temp_db_conn, placex_table, special_phrases_importer):
35     phrase_class = 'class'
36     phrase_type = 'type'
37     special_phrases_importer._create_place_classtype_table('', phrase_class, phrase_type)
38
39     with temp_db_conn.cursor() as temp_db_cursor:
40         temp_db_cursor.execute(f"""
41             SELECT *
42             FROM information_schema.tables
43             WHERE table_type='BASE TABLE'
44             AND table_name='place_classtype_{phrase_class}_{phrase_type}'""")
45         result = temp_db_cursor.fetchone()
46     assert result
47
48 def test_grant_access_to_web_user(temp_db_conn, def_config, special_phrases_importer):
49     phrase_class = 'class'
50     phrase_type = 'type'
51     table_name = 'place_classtype_{}_{}'.format(phrase_class, phrase_type)
52
53     with temp_db_conn.cursor() as temp_db_cursor:
54         temp_db_cursor.execute('CREATE TABLE {}()'.format(table_name))
55
56     special_phrases_importer._grant_access_to_webuser(phrase_class, phrase_type)
57
58     with temp_db_conn.cursor() as temp_db_cursor:
59         temp_db_cursor.execute(f"""
60                 SELECT * FROM information_schema.role_table_grants
61                 WHERE table_name='{table_name}' 
62                 AND grantee='{def_config.DATABASE_WEBUSER}' 
63                 AND privilege_type='SELECT'""")
64         result = temp_db_cursor.fetchone()
65     assert result
66
67 def test_create_place_classtype_table_and_indexes(
68         placex_table, getorcreate_amenity_funcs, 
69         getorcreate_amenityoperator_funcs, special_phrases_importer):
70     pairs = {('class1', 'type1'), ('class2', 'type2')}
71
72     special_phrases_importer._create_place_classtype_table_and_indexes(pairs)
73
74 def test_process_xml_content(special_phrases_importer, getorcreate_amenity_funcs,
75                              getorcreate_amenityoperator_funcs):
76     special_phrases_importer._process_xml_content(get_test_xml_wiki_content(), 'en')
77
78 def mock_get_wiki_content(lang):
79     return get_test_xml_wiki_content()
80
81 def test_import_from_wiki(monkeypatch, special_phrases_importer, placex_table, 
82                           getorcreate_amenity_funcs, getorcreate_amenityoperator_funcs):
83     #mocker.patch.object(special_phrases_importer, '_get_wiki_content', new=mock_get_wiki_content)
84     monkeypatch.setattr('nominatim.tools.special_phrases.SpecialPhrasesImporter._get_wiki_content', mock_get_wiki_content)
85     special_phrases_importer.import_from_wiki(['en'])
86
87 def get_test_xml_wiki_content():
88     xml_test_content_path = (TEST_BASE_DIR / 'testdata' / 'special_phrases_test_content.txt').resolve()
89     with open(xml_test_content_path) as xml_content_reader:
90         return xml_content_reader.read()
91
92 @pytest.fixture
93 def special_phrases_importer(temp_db_conn, def_config, tmp_phplib_dir):
94     return SpecialPhrasesImporter(def_config, tmp_phplib_dir, temp_db_conn)
95
96 @pytest.fixture
97 def make_strandard_name_func(temp_db_cursor):
98     temp_db_cursor.execute(f"""
99         CREATE OR REPLACE FUNCTION make_standard_name(name TEXT) RETURNS TEXT AS $$
100         BEGIN
101         RETURN trim(name); --Basically return only the trimed name for the tests
102         END;
103         $$ LANGUAGE plpgsql IMMUTABLE;""")
104         
105 @pytest.fixture
106 def getorcreate_amenity_funcs(temp_db_cursor, make_strandard_name_func):
107     temp_db_cursor.execute(f"""
108         CREATE OR REPLACE FUNCTION getorcreate_amenity(lookup_word TEXT, normalized_word TEXT,
109                                                     lookup_class text, lookup_type text)
110         RETURNS void as $$
111         BEGIN END;
112         $$ LANGUAGE plpgsql""")
113
114 @pytest.fixture
115 def getorcreate_amenityoperator_funcs(temp_db_cursor, make_strandard_name_func):
116     temp_db_cursor.execute(f"""
117         CREATE OR REPLACE FUNCTION getorcreate_amenityoperator(lookup_word TEXT, normalized_word TEXT,
118                                                     lookup_class text, lookup_type text, op text)
119         RETURNS void as $$
120         BEGIN END;
121         $$ LANGUAGE plpgsql""")