2024-06-09 15:52:20 +03:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2022-01-03 18:23:58 +03:00
|
|
|
#
|
|
|
|
# This file is part of Nominatim. (https://nominatim.org)
|
|
|
|
#
|
2024-06-09 15:52:20 +03:00
|
|
|
# Copyright (C) 2024 by the Nominatim developer community.
|
2022-01-03 18:23:58 +03:00
|
|
|
# For a full list of authors see the git log.
|
2021-03-20 20:55:08 +03:00
|
|
|
"""
|
2021-03-26 03:53:33 +03:00
|
|
|
Tests for import special phrases methods
|
2021-05-10 22:48:11 +03:00
|
|
|
of the class SPImporter.
|
2021-03-20 20:55:08 +03:00
|
|
|
"""
|
2021-03-26 03:53:33 +03:00
|
|
|
from shutil import copyfile
|
2021-03-20 20:55:08 +03:00
|
|
|
import pytest
|
2024-06-09 15:52:20 +03:00
|
|
|
from nominatim_db.tools.special_phrases.sp_importer import SPImporter
|
|
|
|
from nominatim_db.tools.special_phrases.sp_wiki_loader import SPWikiLoader
|
|
|
|
from nominatim_db.tools.special_phrases.special_phrase import SpecialPhrase
|
2024-06-27 22:26:12 +03:00
|
|
|
from nominatim_db.errors import UsageError
|
2021-03-20 20:55:08 +03:00
|
|
|
|
2021-05-19 13:11:04 +03:00
|
|
|
from cursor import CursorForTesting
|
|
|
|
|
2021-05-19 17:03:54 +03:00
|
|
|
@pytest.fixture
|
2022-05-29 15:06:05 +03:00
|
|
|
def sp_importer(temp_db_conn, def_config, monkeypatch):
|
2021-05-19 17:03:54 +03:00
|
|
|
"""
|
|
|
|
Return an instance of SPImporter.
|
|
|
|
"""
|
2022-05-29 15:06:05 +03:00
|
|
|
monkeypatch.setenv('NOMINATIM_LANGUAGES', 'en')
|
|
|
|
loader = SPWikiLoader(def_config)
|
2021-10-22 15:41:14 +03:00
|
|
|
return SPImporter(def_config, temp_db_conn, loader)
|
2021-05-19 17:03:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def xml_wiki_content(src_dir):
|
|
|
|
"""
|
|
|
|
return the content of the static xml test file.
|
|
|
|
"""
|
2021-05-20 00:07:39 +03:00
|
|
|
xml_test_content = src_dir / 'test' / 'testdata' / 'special_phrases_test_content.txt'
|
|
|
|
return xml_test_content.read_text()
|
2021-05-19 17:03:54 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def default_phrases(table_factory):
|
|
|
|
table_factory('place_classtype_testclasstypetable_to_delete')
|
|
|
|
table_factory('place_classtype_testclasstypetable_to_keep')
|
|
|
|
|
2021-03-20 20:55:08 +03:00
|
|
|
|
2021-05-19 13:11:04 +03:00
|
|
|
def test_fetch_existing_place_classtype_tables(sp_importer, table_factory):
|
2021-04-12 15:10:30 +03:00
|
|
|
"""
|
|
|
|
Check for the fetch_existing_place_classtype_tables() method.
|
|
|
|
It should return the table just created.
|
|
|
|
"""
|
2021-05-19 13:11:04 +03:00
|
|
|
table_factory('place_classtype_testclasstypetable')
|
2021-04-12 15:10:30 +03:00
|
|
|
|
2021-05-10 22:48:11 +03:00
|
|
|
sp_importer._fetch_existing_place_classtype_tables()
|
|
|
|
contained_table = sp_importer.table_phrases_to_delete.pop()
|
2021-04-12 15:10:30 +03:00
|
|
|
assert contained_table == 'place_classtype_testclasstypetable'
|
|
|
|
|
2021-05-10 22:48:11 +03:00
|
|
|
def test_check_sanity_class(sp_importer):
|
2021-03-26 03:53:33 +03:00
|
|
|
"""
|
|
|
|
Check for _check_sanity() method.
|
|
|
|
If a wrong class or type is given, an UsageError should raise.
|
|
|
|
If a good class and type are given, nothing special happens.
|
|
|
|
"""
|
|
|
|
|
2021-05-10 22:48:11 +03:00
|
|
|
assert not sp_importer._check_sanity(SpecialPhrase('en', '', 'type', ''))
|
|
|
|
assert not sp_importer._check_sanity(SpecialPhrase('en', 'class', '', ''))
|
|
|
|
|
|
|
|
assert sp_importer._check_sanity(SpecialPhrase('en', 'class', 'type', ''))
|
2021-03-26 03:53:33 +03:00
|
|
|
|
2021-05-10 22:48:11 +03:00
|
|
|
def test_load_white_and_black_lists(sp_importer):
|
2021-03-26 03:53:33 +03:00
|
|
|
"""
|
|
|
|
Test that _load_white_and_black_lists() well return
|
|
|
|
black list and white list and that they are of dict type.
|
|
|
|
"""
|
2021-05-10 22:48:11 +03:00
|
|
|
black_list, white_list = sp_importer._load_white_and_black_lists()
|
2021-03-26 03:53:33 +03:00
|
|
|
|
|
|
|
assert isinstance(black_list, dict) and isinstance(white_list, dict)
|
|
|
|
|
|
|
|
|
2024-07-02 16:15:50 +03:00
|
|
|
def test_create_place_classtype_indexes(temp_db_with_extensions,
|
|
|
|
temp_db_conn, temp_db_cursor,
|
2021-05-19 13:11:04 +03:00
|
|
|
table_factory, sp_importer):
|
2021-03-26 03:53:33 +03:00
|
|
|
"""
|
|
|
|
Test that _create_place_classtype_indexes() create the
|
|
|
|
place_id index and centroid index on the right place_class_type table.
|
|
|
|
"""
|
2021-03-20 20:55:08 +03:00
|
|
|
phrase_class = 'class'
|
|
|
|
phrase_type = 'type'
|
2021-03-23 01:56:24 +03:00
|
|
|
table_name = 'place_classtype_{}_{}'.format(phrase_class, phrase_type)
|
2021-03-20 20:55:08 +03:00
|
|
|
|
2021-05-19 13:11:04 +03:00
|
|
|
table_factory(table_name, 'place_id BIGINT, centroid GEOMETRY')
|
2021-03-20 20:55:08 +03:00
|
|
|
|
2021-05-10 22:48:11 +03:00
|
|
|
sp_importer._create_place_classtype_indexes('', phrase_class, phrase_type)
|
2024-07-02 16:15:50 +03:00
|
|
|
temp_db_conn.commit()
|
2021-03-20 20:55:08 +03:00
|
|
|
|
2024-07-02 16:15:50 +03:00
|
|
|
assert check_placeid_and_centroid_indexes(temp_db_cursor, phrase_class, phrase_type)
|
2021-03-20 20:55:08 +03:00
|
|
|
|
2024-07-02 16:15:50 +03:00
|
|
|
def test_create_place_classtype_table(temp_db_conn, temp_db_cursor, placex_table, sp_importer):
|
2021-03-26 03:53:33 +03:00
|
|
|
"""
|
|
|
|
Test that _create_place_classtype_table() create
|
|
|
|
the right place_classtype table.
|
|
|
|
"""
|
2021-03-20 20:55:08 +03:00
|
|
|
phrase_class = 'class'
|
|
|
|
phrase_type = 'type'
|
2021-05-10 22:48:11 +03:00
|
|
|
sp_importer._create_place_classtype_table('', phrase_class, phrase_type)
|
2024-07-02 16:15:50 +03:00
|
|
|
temp_db_conn.commit()
|
2021-03-20 20:55:08 +03:00
|
|
|
|
2024-07-02 16:15:50 +03:00
|
|
|
assert check_table_exist(temp_db_cursor, phrase_class, phrase_type)
|
2021-03-20 20:55:08 +03:00
|
|
|
|
2024-07-02 16:15:50 +03:00
|
|
|
def test_grant_access_to_web_user(temp_db_conn, temp_db_cursor, table_factory,
|
|
|
|
def_config, sp_importer):
|
2021-03-26 03:53:33 +03:00
|
|
|
"""
|
2021-05-10 22:48:11 +03:00
|
|
|
Test that _grant_access_to_webuser() give
|
2021-03-26 03:53:33 +03:00
|
|
|
right access to the web user.
|
|
|
|
"""
|
2021-03-20 20:55:08 +03:00
|
|
|
phrase_class = 'class'
|
|
|
|
phrase_type = 'type'
|
2021-03-23 01:56:24 +03:00
|
|
|
table_name = 'place_classtype_{}_{}'.format(phrase_class, phrase_type)
|
2021-03-20 20:55:08 +03:00
|
|
|
|
2021-05-19 13:11:04 +03:00
|
|
|
table_factory(table_name)
|
2021-03-20 20:55:08 +03:00
|
|
|
|
2021-05-10 22:48:11 +03:00
|
|
|
sp_importer._grant_access_to_webuser(phrase_class, phrase_type)
|
2024-07-02 16:15:50 +03:00
|
|
|
temp_db_conn.commit()
|
2021-03-20 20:55:08 +03:00
|
|
|
|
2024-07-02 16:15:50 +03:00
|
|
|
assert check_grant_access(temp_db_cursor, def_config.DATABASE_WEBUSER, phrase_class, phrase_type)
|
2021-03-20 20:55:08 +03:00
|
|
|
|
2021-03-24 16:26:52 +03:00
|
|
|
def test_create_place_classtype_table_and_indexes(
|
2024-07-02 16:15:50 +03:00
|
|
|
temp_db_cursor, def_config, placex_table,
|
|
|
|
sp_importer, temp_db_conn):
|
2021-03-26 03:53:33 +03:00
|
|
|
"""
|
|
|
|
Test that _create_place_classtype_table_and_indexes()
|
|
|
|
create the right place_classtype tables and place_id indexes
|
|
|
|
and centroid indexes and grant access to the web user
|
|
|
|
for the given set of pairs.
|
|
|
|
"""
|
|
|
|
pairs = set([('class1', 'type1'), ('class2', 'type2')])
|
2021-03-24 16:26:52 +03:00
|
|
|
|
2022-07-17 11:46:59 +03:00
|
|
|
sp_importer._create_classtype_table_and_indexes(pairs)
|
2024-07-02 16:15:50 +03:00
|
|
|
temp_db_conn.commit()
|
2021-03-24 16:26:52 +03:00
|
|
|
|
2021-03-26 03:53:33 +03:00
|
|
|
for pair in pairs:
|
2024-07-02 16:15:50 +03:00
|
|
|
assert check_table_exist(temp_db_cursor, pair[0], pair[1])
|
|
|
|
assert check_placeid_and_centroid_indexes(temp_db_cursor, pair[0], pair[1])
|
|
|
|
assert check_grant_access(temp_db_cursor, def_config.DATABASE_WEBUSER, pair[0], pair[1])
|
2021-03-24 16:26:52 +03:00
|
|
|
|
2021-05-10 22:48:11 +03:00
|
|
|
def test_remove_non_existent_tables_from_db(sp_importer, default_phrases,
|
2024-07-02 16:15:50 +03:00
|
|
|
temp_db_conn, temp_db_cursor):
|
2021-04-12 15:10:30 +03:00
|
|
|
"""
|
|
|
|
Check for the remove_non_existent_phrases_from_db() method.
|
|
|
|
|
|
|
|
It should removed entries from the word table which are contained
|
|
|
|
in the words_phrases_to_delete set and not those also contained
|
|
|
|
in the words_phrases_still_exist set.
|
|
|
|
|
|
|
|
place_classtype tables contained in table_phrases_to_delete should
|
|
|
|
be deleted.
|
|
|
|
"""
|
2021-05-19 13:11:04 +03:00
|
|
|
sp_importer.table_phrases_to_delete = {
|
|
|
|
'place_classtype_testclasstypetable_to_delete'
|
|
|
|
}
|
|
|
|
|
|
|
|
query_tables = """
|
|
|
|
SELECT table_name
|
|
|
|
FROM information_schema.tables
|
|
|
|
WHERE table_schema='public'
|
|
|
|
AND table_name like 'place_classtype_%';
|
|
|
|
"""
|
2021-04-12 15:10:30 +03:00
|
|
|
|
2021-05-19 13:11:04 +03:00
|
|
|
sp_importer._remove_non_existent_tables_from_db()
|
2024-07-02 16:15:50 +03:00
|
|
|
temp_db_conn.commit()
|
2021-04-12 15:10:30 +03:00
|
|
|
|
2024-07-02 16:15:50 +03:00
|
|
|
assert temp_db_cursor.row_set(query_tables) \
|
2021-05-19 13:11:04 +03:00
|
|
|
== {('place_classtype_testclasstypetable_to_keep', )}
|
2021-04-12 15:10:30 +03:00
|
|
|
|
|
|
|
|
2021-05-17 13:40:50 +03:00
|
|
|
@pytest.mark.parametrize("should_replace", [(True), (False)])
|
2024-07-02 16:15:50 +03:00
|
|
|
def test_import_phrases(monkeypatch, temp_db_cursor, def_config, sp_importer,
|
2021-05-19 17:03:54 +03:00
|
|
|
placex_table, table_factory, tokenizer_mock,
|
|
|
|
xml_wiki_content, should_replace):
|
2021-03-26 03:53:33 +03:00
|
|
|
"""
|
2021-05-11 00:09:00 +03:00
|
|
|
Check that the main import_phrases() method is well executed.
|
2021-03-26 03:53:33 +03:00
|
|
|
It should create the place_classtype table, the place_id and centroid indexes,
|
|
|
|
grand access to the web user and executing the SQL functions for amenities.
|
2021-05-10 22:48:11 +03:00
|
|
|
It should also update the database well by deleting or preserving existing entries
|
2021-04-17 20:23:33 +03:00
|
|
|
of the database.
|
2021-03-26 03:53:33 +03:00
|
|
|
"""
|
2021-04-17 20:23:33 +03:00
|
|
|
#Add some data to the database before execution in order to test
|
|
|
|
#what is deleted and what is preserved.
|
2021-05-19 13:11:04 +03:00
|
|
|
table_factory('place_classtype_amenity_animal_shelter')
|
|
|
|
table_factory('place_classtype_wrongclass_wrongtype')
|
|
|
|
|
2024-06-09 15:52:20 +03:00
|
|
|
monkeypatch.setattr('nominatim_db.tools.special_phrases.sp_wiki_loader._get_wiki_content',
|
2022-05-30 15:12:46 +03:00
|
|
|
lambda lang: xml_wiki_content)
|
2021-05-11 00:09:00 +03:00
|
|
|
|
2021-04-27 22:50:35 +03:00
|
|
|
tokenizer = tokenizer_mock()
|
2021-05-17 13:40:50 +03:00
|
|
|
sp_importer.import_phrases(tokenizer, should_replace)
|
2021-04-27 22:50:35 +03:00
|
|
|
|
|
|
|
assert len(tokenizer.analyser_cache['special_phrases']) == 18
|
2021-03-24 16:26:52 +03:00
|
|
|
|
2021-03-26 03:53:33 +03:00
|
|
|
class_test = 'aerialway'
|
|
|
|
type_test = 'zip_line'
|
|
|
|
|
2024-07-02 16:15:50 +03:00
|
|
|
assert check_table_exist(temp_db_cursor, class_test, type_test)
|
|
|
|
assert check_placeid_and_centroid_indexes(temp_db_cursor, class_test, type_test)
|
|
|
|
assert check_grant_access(temp_db_cursor, def_config.DATABASE_WEBUSER, class_test, type_test)
|
|
|
|
assert check_table_exist(temp_db_cursor, 'amenity', 'animal_shelter')
|
2021-05-17 13:40:50 +03:00
|
|
|
if should_replace:
|
2024-07-02 16:15:50 +03:00
|
|
|
assert not check_table_exist(temp_db_cursor, 'wrong_class', 'wrong_type')
|
2021-04-17 20:23:33 +03:00
|
|
|
|
2024-07-02 16:15:50 +03:00
|
|
|
assert temp_db_cursor.table_exists('place_classtype_amenity_animal_shelter')
|
2021-05-17 13:40:50 +03:00
|
|
|
if should_replace:
|
2024-07-02 16:15:50 +03:00
|
|
|
assert not temp_db_cursor.table_exists('place_classtype_wrongclass_wrongtype')
|
2021-04-17 20:23:33 +03:00
|
|
|
|
2024-07-02 16:15:50 +03:00
|
|
|
def check_table_exist(temp_db_cursor, phrase_class, phrase_type):
|
2021-03-26 03:53:33 +03:00
|
|
|
"""
|
|
|
|
Verify that the place_classtype table exists for the given
|
|
|
|
phrase_class and phrase_type.
|
|
|
|
"""
|
2024-07-02 16:15:50 +03:00
|
|
|
return temp_db_cursor.table_exists('place_classtype_{}_{}'.format(phrase_class, phrase_type))
|
2021-03-26 03:53:33 +03:00
|
|
|
|
|
|
|
|
2024-07-02 16:15:50 +03:00
|
|
|
def check_grant_access(temp_db_cursor, user, phrase_class, phrase_type):
|
2021-03-26 03:53:33 +03:00
|
|
|
"""
|
|
|
|
Check that the web user has been granted right access to the
|
|
|
|
place_classtype table of the given phrase_class and phrase_type.
|
|
|
|
"""
|
|
|
|
table_name = 'place_classtype_{}_{}'.format(phrase_class, phrase_type)
|
|
|
|
|
2024-07-02 16:15:50 +03:00
|
|
|
temp_db_cursor.execute("""
|
|
|
|
SELECT * FROM information_schema.role_table_grants
|
|
|
|
WHERE table_name='{}'
|
|
|
|
AND grantee='{}'
|
|
|
|
AND privilege_type='SELECT'""".format(table_name, user))
|
|
|
|
return temp_db_cursor.fetchone()
|
2021-03-26 03:53:33 +03:00
|
|
|
|
2024-07-02 16:15:50 +03:00
|
|
|
def check_placeid_and_centroid_indexes(temp_db_cursor, phrase_class, phrase_type):
|
2021-03-26 03:53:33 +03:00
|
|
|
"""
|
|
|
|
Check that the place_id index and centroid index exist for the
|
|
|
|
place_classtype table of the given phrase_class and phrase_type.
|
|
|
|
"""
|
2024-07-02 16:15:50 +03:00
|
|
|
table_name = 'place_classtype_{}_{}'.format(phrase_class, phrase_type)
|
2021-03-26 03:53:33 +03:00
|
|
|
index_prefix = 'idx_place_classtype_{}_{}_'.format(phrase_class, phrase_type)
|
|
|
|
|
|
|
|
return (
|
2024-07-02 16:15:50 +03:00
|
|
|
temp_db_cursor.index_exists(table_name, index_prefix + 'centroid')
|
2021-03-26 03:53:33 +03:00
|
|
|
and
|
2024-07-02 16:15:50 +03:00
|
|
|
temp_db_cursor.index_exists(table_name, index_prefix + 'place_id')
|
2021-03-26 03:53:33 +03:00
|
|
|
)
|