2 Tests for import special phrases functions
4 from pathlib import Path
6 from nominatim.tools.special_phrases import SpecialPhrasesImporter
8 TEST_BASE_DIR = Path(__file__) / '..' / '..'
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')
14 def test_process_amenity_without_operator(special_phrases_importer, getorcreate_amenity_funcs):
15 special_phrases_importer._process_amenity('', '', '', '', '')
17 def test_create_place_classtype_indexes(temp_db_conn, special_phrases_importer):
18 phrase_class = 'class'
20 table_name = 'place_classtype_{}_{}'.format(phrase_class, phrase_type)
21 index_prefix = 'idx_place_classtype_{}_{}_'.format(phrase_class, phrase_type)
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))
27 special_phrases_importer._create_place_classtype_indexes('', phrase_class, phrase_type)
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')
32 assert centroid_index_exists and place_id_index_exists
34 def test_create_place_classtype_table(temp_db_conn, placex_table, special_phrases_importer):
35 phrase_class = 'class'
37 special_phrases_importer._create_place_classtype_table('', phrase_class, phrase_type)
39 with temp_db_conn.cursor() as temp_db_cursor:
40 temp_db_cursor.execute(f"""
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()
48 def test_grant_access_to_web_user(temp_db_conn, def_config, special_phrases_importer):
49 phrase_class = 'class'
51 table_name = 'place_classtype_{}_{}'.format(phrase_class, phrase_type)
53 with temp_db_conn.cursor() as temp_db_cursor:
54 temp_db_cursor.execute('CREATE TABLE {}()'.format(table_name))
56 special_phrases_importer._grant_access_to_webuser(phrase_class, phrase_type)
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()
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')}
72 special_phrases_importer._create_place_classtype_table_and_indexes(pairs)
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')
78 def mock_get_wiki_content(lang):
79 return get_test_xml_wiki_content()
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'])
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()
93 def special_phrases_importer(temp_db_conn, def_config, tmp_phplib_dir):
94 return SpecialPhrasesImporter(def_config, tmp_phplib_dir, temp_db_conn)
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 $$
101 RETURN trim(name); --Basically return only the trimed name for the tests
103 $$ LANGUAGE plpgsql IMMUTABLE;""")
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)
112 $$ LANGUAGE plpgsql""")
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)
121 $$ LANGUAGE plpgsql""")