mirror of
https://github.com/osm-search/Nominatim.git
synced 2026-02-16 05:18:00 +00:00
test: replace raw execute() with fixture code where possible
This commit is contained in:
@@ -12,14 +12,16 @@ from nominatim.tools.special_phrases.sp_wiki_loader import SPWikiLoader
|
||||
from nominatim.tools.special_phrases.sp_csv_loader import SPCsvLoader
|
||||
from nominatim.tools.special_phrases.special_phrase import SpecialPhrase
|
||||
|
||||
from cursor import CursorForTesting
|
||||
|
||||
TEST_BASE_DIR = Path(__file__) / '..' / '..'
|
||||
|
||||
def test_fetch_existing_place_classtype_tables(sp_importer, temp_db_cursor):
|
||||
def test_fetch_existing_place_classtype_tables(sp_importer, table_factory):
|
||||
"""
|
||||
Check for the fetch_existing_place_classtype_tables() method.
|
||||
It should return the table just created.
|
||||
"""
|
||||
temp_db_cursor.execute('CREATE TABLE place_classtype_testclasstypetable()')
|
||||
table_factory('place_classtype_testclasstypetable')
|
||||
|
||||
sp_importer._fetch_existing_place_classtype_tables()
|
||||
contained_table = sp_importer.table_phrases_to_delete.pop()
|
||||
@@ -91,7 +93,8 @@ def test_convert_settings_giving_json(sp_importer):
|
||||
|
||||
assert returned == json_file
|
||||
|
||||
def test_create_place_classtype_indexes(temp_db_conn, sp_importer):
|
||||
def test_create_place_classtype_indexes(temp_db_with_extensions, temp_db_conn,
|
||||
table_factory, sp_importer):
|
||||
"""
|
||||
Test that _create_place_classtype_indexes() create the
|
||||
place_id index and centroid index on the right place_class_type table.
|
||||
@@ -100,9 +103,7 @@ def test_create_place_classtype_indexes(temp_db_conn, sp_importer):
|
||||
phrase_type = 'type'
|
||||
table_name = 'place_classtype_{}_{}'.format(phrase_class, phrase_type)
|
||||
|
||||
with temp_db_conn.cursor() as temp_db_cursor:
|
||||
temp_db_cursor.execute("CREATE EXTENSION postgis;")
|
||||
temp_db_cursor.execute('CREATE TABLE {}(place_id BIGINT, centroid GEOMETRY)'.format(table_name))
|
||||
table_factory(table_name, 'place_id BIGINT, centroid GEOMETRY')
|
||||
|
||||
sp_importer._create_place_classtype_indexes('', phrase_class, phrase_type)
|
||||
|
||||
@@ -119,7 +120,7 @@ def test_create_place_classtype_table(temp_db_conn, placex_table, sp_importer):
|
||||
|
||||
assert check_table_exist(temp_db_conn, phrase_class, phrase_type)
|
||||
|
||||
def test_grant_access_to_web_user(temp_db_conn, def_config, sp_importer):
|
||||
def test_grant_access_to_web_user(temp_db_conn, table_factory, def_config, sp_importer):
|
||||
"""
|
||||
Test that _grant_access_to_webuser() give
|
||||
right access to the web user.
|
||||
@@ -128,8 +129,7 @@ def test_grant_access_to_web_user(temp_db_conn, def_config, sp_importer):
|
||||
phrase_type = 'type'
|
||||
table_name = 'place_classtype_{}_{}'.format(phrase_class, phrase_type)
|
||||
|
||||
with temp_db_conn.cursor() as temp_db_cursor:
|
||||
temp_db_cursor.execute('CREATE TABLE {}()'.format(table_name))
|
||||
table_factory(table_name)
|
||||
|
||||
sp_importer._grant_access_to_webuser(phrase_class, phrase_type)
|
||||
|
||||
@@ -154,7 +154,7 @@ def test_create_place_classtype_table_and_indexes(
|
||||
assert check_grant_access(temp_db_conn, def_config.DATABASE_WEBUSER, pair[0], pair[1])
|
||||
|
||||
def test_remove_non_existent_tables_from_db(sp_importer, default_phrases,
|
||||
temp_db_conn):
|
||||
temp_db_conn):
|
||||
"""
|
||||
Check for the remove_non_existent_phrases_from_db() method.
|
||||
|
||||
@@ -165,29 +165,28 @@ def test_remove_non_existent_tables_from_db(sp_importer, default_phrases,
|
||||
place_classtype tables contained in table_phrases_to_delete should
|
||||
be deleted.
|
||||
"""
|
||||
with temp_db_conn.cursor() as temp_db_cursor:
|
||||
sp_importer.table_phrases_to_delete = {
|
||||
'place_classtype_testclasstypetable_to_delete'
|
||||
}
|
||||
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_%';
|
||||
"""
|
||||
query_tables = """
|
||||
SELECT table_name
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema='public'
|
||||
AND table_name like 'place_classtype_%';
|
||||
"""
|
||||
|
||||
sp_importer._remove_non_existent_tables_from_db()
|
||||
sp_importer._remove_non_existent_tables_from_db()
|
||||
|
||||
# Changes are not committed yet. Use temp_db_conn for checking results.
|
||||
with temp_db_conn.cursor(cursor_factory=CursorForTesting) as cur:
|
||||
assert cur.row_set(query_tables) \
|
||||
== {('place_classtype_testclasstypetable_to_keep', )}
|
||||
|
||||
temp_db_cursor.execute(query_tables)
|
||||
tables_result = temp_db_cursor.fetchall()
|
||||
assert (len(tables_result) == 1 and
|
||||
tables_result[0][0] == 'place_classtype_testclasstypetable_to_keep'
|
||||
)
|
||||
|
||||
@pytest.mark.parametrize("should_replace", [(True), (False)])
|
||||
def test_import_phrases(monkeypatch, temp_db_conn, def_config, sp_importer,
|
||||
placex_table, tokenizer_mock, should_replace):
|
||||
placex_table, table_factory, tokenizer_mock, should_replace):
|
||||
"""
|
||||
Check that the main import_phrases() method is well executed.
|
||||
It should create the place_classtype table, the place_id and centroid indexes,
|
||||
@@ -197,11 +196,9 @@ def test_import_phrases(monkeypatch, temp_db_conn, def_config, sp_importer,
|
||||
"""
|
||||
#Add some data to the database before execution in order to test
|
||||
#what is deleted and what is preserved.
|
||||
with temp_db_conn.cursor() as temp_db_cursor:
|
||||
temp_db_cursor.execute("""
|
||||
CREATE TABLE place_classtype_amenity_animal_shelter();
|
||||
CREATE TABLE place_classtype_wrongclass_wrongtype();""")
|
||||
|
||||
table_factory('place_classtype_amenity_animal_shelter')
|
||||
table_factory('place_classtype_wrongclass_wrongtype')
|
||||
|
||||
monkeypatch.setattr('nominatim.tools.special_phrases.sp_wiki_loader.SPWikiLoader._get_wiki_content',
|
||||
mock_get_wiki_content)
|
||||
|
||||
@@ -220,35 +217,10 @@ def test_import_phrases(monkeypatch, temp_db_conn, def_config, sp_importer,
|
||||
if should_replace:
|
||||
assert not check_table_exist(temp_db_conn, 'wrong_class', 'wrong_type')
|
||||
|
||||
#Format (query, should_return_something_bool) use to easily execute all asserts
|
||||
queries_tests = set()
|
||||
|
||||
#Used to check that correct place_classtype table already in the datase before is still there.
|
||||
query_existing_table = """
|
||||
SELECT table_name
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema='public'
|
||||
AND table_name = 'place_classtype_amenity_animal_shelter';
|
||||
"""
|
||||
queries_tests.add((query_existing_table, True))
|
||||
|
||||
#Used to check that wrong place_classtype table was deleted from the database.
|
||||
query_wrong_table = """
|
||||
SELECT table_name
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema='public'
|
||||
AND table_name = 'place_classtype_wrongclass_wrongtype';
|
||||
"""
|
||||
assert temp_db_conn.table_exists('place_classtype_amenity_animal_shelter')
|
||||
if should_replace:
|
||||
queries_tests.add((query_wrong_table, False))
|
||||
assert not temp_db_conn.table_exists('place_classtype_wrongclass_wrongtype')
|
||||
|
||||
with temp_db_conn.cursor() as temp_db_cursor:
|
||||
for query in queries_tests:
|
||||
temp_db_cursor.execute(query[0])
|
||||
if (query[1] == True):
|
||||
assert temp_db_cursor.fetchone()
|
||||
else:
|
||||
assert not temp_db_cursor.fetchone()
|
||||
|
||||
def mock_get_wiki_content(self, lang):
|
||||
"""
|
||||
@@ -270,15 +242,8 @@ def check_table_exist(temp_db_conn, phrase_class, phrase_type):
|
||||
Verify that the place_classtype table exists for the given
|
||||
phrase_class and phrase_type.
|
||||
"""
|
||||
table_name = 'place_classtype_{}_{}'.format(phrase_class, phrase_type)
|
||||
return temp_db_conn.table_exists('place_classtype_{}_{}'.format(phrase_class, phrase_type))
|
||||
|
||||
with temp_db_conn.cursor() as temp_db_cursor:
|
||||
temp_db_cursor.execute("""
|
||||
SELECT *
|
||||
FROM information_schema.tables
|
||||
WHERE table_type='BASE TABLE'
|
||||
AND table_name='{}'""".format(table_name))
|
||||
return temp_db_cursor.fetchone()
|
||||
|
||||
def check_grant_access(temp_db_conn, user, phrase_class, phrase_type):
|
||||
"""
|
||||
@@ -332,7 +297,6 @@ def temp_phplib_dir_with_migration():
|
||||
yield Path(phpdir)
|
||||
|
||||
@pytest.fixture
|
||||
def default_phrases(temp_db_cursor):
|
||||
temp_db_cursor.execute("""
|
||||
CREATE TABLE place_classtype_testclasstypetable_to_delete();
|
||||
CREATE TABLE place_classtype_testclasstypetable_to_keep();""")
|
||||
def default_phrases(table_factory):
|
||||
table_factory('place_classtype_testclasstypetable_to_delete')
|
||||
table_factory('place_classtype_testclasstypetable_to_keep')
|
||||
|
||||
Reference in New Issue
Block a user