forked from hans/Nominatim
port code to psycopg3
This commit is contained in:
@@ -8,10 +8,11 @@
|
||||
Tests for functions to import a new database.
|
||||
"""
|
||||
from pathlib import Path
|
||||
from contextlib import closing
|
||||
|
||||
import pytest
|
||||
import psycopg2
|
||||
import pytest_asyncio
|
||||
import psycopg
|
||||
from psycopg import sql as pysql
|
||||
|
||||
from nominatim_db.tools import database_import
|
||||
from nominatim_db.errors import UsageError
|
||||
@@ -21,10 +22,7 @@ class TestDatabaseSetup:
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup_nonexistant_db(self):
|
||||
conn = psycopg2.connect(database='postgres')
|
||||
|
||||
try:
|
||||
conn.set_isolation_level(0)
|
||||
with psycopg.connect(dbname='postgres', autocommit=True) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(f'DROP DATABASE IF EXISTS {self.DBNAME}')
|
||||
|
||||
@@ -32,22 +30,17 @@ class TestDatabaseSetup:
|
||||
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(f'DROP DATABASE IF EXISTS {self.DBNAME}')
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def cursor(self):
|
||||
conn = psycopg2.connect(database=self.DBNAME)
|
||||
|
||||
try:
|
||||
with psycopg.connect(dbname=self.DBNAME) as conn:
|
||||
with conn.cursor() as cur:
|
||||
yield cur
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def conn(self):
|
||||
return closing(psycopg2.connect(database=self.DBNAME))
|
||||
return psycopg.connect(dbname=self.DBNAME)
|
||||
|
||||
|
||||
def test_setup_skeleton(self):
|
||||
@@ -178,18 +171,19 @@ def test_truncate_database_tables(temp_db_conn, temp_db_cursor, table_factory, w
|
||||
|
||||
|
||||
@pytest.mark.parametrize("threads", (1, 5))
|
||||
def test_load_data(dsn, place_row, placex_table, osmline_table,
|
||||
@pytest.mark.asyncio
|
||||
async def test_load_data(dsn, place_row, placex_table, osmline_table,
|
||||
temp_db_cursor, threads):
|
||||
for func in ('precompute_words', 'getorcreate_housenumber_id', 'make_standard_name'):
|
||||
temp_db_cursor.execute(f"""CREATE FUNCTION {func} (src TEXT)
|
||||
RETURNS TEXT AS $$ SELECT 'a'::TEXT $$ LANGUAGE SQL
|
||||
""")
|
||||
temp_db_cursor.execute(pysql.SQL("""CREATE FUNCTION {} (src TEXT)
|
||||
RETURNS TEXT AS $$ SELECT 'a'::TEXT $$ LANGUAGE SQL
|
||||
""").format(pysql.Identifier(func)))
|
||||
for oid in range(100, 130):
|
||||
place_row(osm_id=oid)
|
||||
place_row(osm_type='W', osm_id=342, cls='place', typ='houses',
|
||||
geom='SRID=4326;LINESTRING(0 0, 10 10)')
|
||||
|
||||
database_import.load_data(dsn, threads)
|
||||
await database_import.load_data(dsn, threads)
|
||||
|
||||
assert temp_db_cursor.table_rows('placex') == 30
|
||||
assert temp_db_cursor.table_rows('location_property_osmline') == 1
|
||||
@@ -241,11 +235,12 @@ class TestSetupSQL:
|
||||
|
||||
|
||||
@pytest.mark.parametrize("drop", [True, False])
|
||||
def test_create_search_indices(self, temp_db_conn, temp_db_cursor, drop):
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_search_indices(self, temp_db_conn, temp_db_cursor, drop):
|
||||
self.write_sql('indices.sql',
|
||||
"""CREATE FUNCTION test() RETURNS bool
|
||||
AS $$ SELECT {{drop}} $$ LANGUAGE SQL""")
|
||||
|
||||
database_import.create_search_indices(temp_db_conn, self.config, drop)
|
||||
await database_import.create_search_indices(temp_db_conn, self.config, drop)
|
||||
|
||||
temp_db_cursor.scalar('SELECT test()') == drop
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
Tests for migration functions
|
||||
"""
|
||||
import pytest
|
||||
import psycopg2.extras
|
||||
|
||||
from nominatim_db.tools import migration
|
||||
from nominatim_db.errors import UsageError
|
||||
@@ -44,7 +43,6 @@ def test_no_migration_old_versions(temp_db_with_extensions, table_factory, def_c
|
||||
def test_set_up_migration_for_36(temp_db_with_extensions, temp_db_cursor,
|
||||
table_factory, def_config, monkeypatch,
|
||||
postprocess_mock):
|
||||
psycopg2.extras.register_hstore(temp_db_cursor)
|
||||
# don't actually run any migration, except the property table creation
|
||||
monkeypatch.setattr(migration, '_MIGRATION_FUNCTIONS',
|
||||
[((3, 5, 0, 99), migration.add_nominatim_property_table)])
|
||||
|
||||
@@ -47,7 +47,7 @@ class MockPostcodeTable:
|
||||
country_code, postcode,
|
||||
geometry)
|
||||
VALUES (nextval('seq_place'), 1, %s, %s,
|
||||
'SRID=4326;POINT(%s %s)')""",
|
||||
ST_SetSRID(ST_MakePoint(%s, %s), 4326))""",
|
||||
(country, postcode, x, y))
|
||||
self.conn.commit()
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import tarfile
|
||||
from textwrap import dedent
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
|
||||
from nominatim_db.db.connection import execute_scalar
|
||||
from nominatim_db.tools import tiger_data, freeze
|
||||
@@ -76,82 +77,91 @@ def csv_factory(tmp_path):
|
||||
|
||||
|
||||
@pytest.mark.parametrize("threads", (1, 5))
|
||||
def test_add_tiger_data(def_config, src_dir, tiger_table, tokenizer_mock, threads):
|
||||
tiger_data.add_tiger_data(str(src_dir / 'test' / 'testdb' / 'tiger'),
|
||||
def_config, threads, tokenizer_mock())
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_tiger_data(def_config, src_dir, tiger_table, tokenizer_mock, threads):
|
||||
await tiger_data.add_tiger_data(str(src_dir / 'test' / 'testdb' / 'tiger'),
|
||||
def_config, threads, tokenizer_mock())
|
||||
|
||||
assert tiger_table.count() == 6213
|
||||
|
||||
|
||||
def test_add_tiger_data_database_frozen(def_config, temp_db_conn, tiger_table, tokenizer_mock,
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_tiger_data_database_frozen(def_config, temp_db_conn, tiger_table, tokenizer_mock,
|
||||
tmp_path):
|
||||
freeze.drop_update_tables(temp_db_conn)
|
||||
|
||||
with pytest.raises(UsageError) as excinfo:
|
||||
tiger_data.add_tiger_data(str(tmp_path), def_config, 1, tokenizer_mock())
|
||||
await tiger_data.add_tiger_data(str(tmp_path), def_config, 1, tokenizer_mock())
|
||||
|
||||
assert "database frozen" in str(excinfo.value)
|
||||
|
||||
assert tiger_table.count() == 0
|
||||
|
||||
def test_add_tiger_data_no_files(def_config, tiger_table, tokenizer_mock,
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_tiger_data_no_files(def_config, tiger_table, tokenizer_mock,
|
||||
tmp_path):
|
||||
tiger_data.add_tiger_data(str(tmp_path), def_config, 1, tokenizer_mock())
|
||||
await tiger_data.add_tiger_data(str(tmp_path), def_config, 1, tokenizer_mock())
|
||||
|
||||
assert tiger_table.count() == 0
|
||||
|
||||
|
||||
def test_add_tiger_data_bad_file(def_config, tiger_table, tokenizer_mock,
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_tiger_data_bad_file(def_config, tiger_table, tokenizer_mock,
|
||||
tmp_path):
|
||||
sqlfile = tmp_path / '1010.csv'
|
||||
sqlfile.write_text("""Random text""")
|
||||
|
||||
tiger_data.add_tiger_data(str(tmp_path), def_config, 1, tokenizer_mock())
|
||||
await tiger_data.add_tiger_data(str(tmp_path), def_config, 1, tokenizer_mock())
|
||||
|
||||
assert tiger_table.count() == 0
|
||||
|
||||
|
||||
def test_add_tiger_data_hnr_nan(def_config, tiger_table, tokenizer_mock,
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_tiger_data_hnr_nan(def_config, tiger_table, tokenizer_mock,
|
||||
csv_factory, tmp_path):
|
||||
csv_factory('file1', hnr_from=99)
|
||||
csv_factory('file2', hnr_from='L12')
|
||||
csv_factory('file3', hnr_to='12.4')
|
||||
|
||||
tiger_data.add_tiger_data(str(tmp_path), def_config, 1, tokenizer_mock())
|
||||
await tiger_data.add_tiger_data(str(tmp_path), def_config, 1, tokenizer_mock())
|
||||
|
||||
assert tiger_table.count() == 1
|
||||
assert tiger_table.row()['start'] == 99
|
||||
assert tiger_table.row().start == 99
|
||||
|
||||
|
||||
@pytest.mark.parametrize("threads", (1, 5))
|
||||
def test_add_tiger_data_tarfile(def_config, tiger_table, tokenizer_mock,
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_tiger_data_tarfile(def_config, tiger_table, tokenizer_mock,
|
||||
tmp_path, src_dir, threads):
|
||||
tar = tarfile.open(str(tmp_path / 'sample.tar.gz'), "w:gz")
|
||||
tar.add(str(src_dir / 'test' / 'testdb' / 'tiger' / '01001.csv'))
|
||||
tar.close()
|
||||
|
||||
tiger_data.add_tiger_data(str(tmp_path / 'sample.tar.gz'), def_config, threads,
|
||||
tokenizer_mock())
|
||||
await tiger_data.add_tiger_data(str(tmp_path / 'sample.tar.gz'), def_config, threads,
|
||||
tokenizer_mock())
|
||||
|
||||
assert tiger_table.count() == 6213
|
||||
|
||||
|
||||
def test_add_tiger_data_bad_tarfile(def_config, tiger_table, tokenizer_mock,
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_tiger_data_bad_tarfile(def_config, tiger_table, tokenizer_mock,
|
||||
tmp_path):
|
||||
tarfile = tmp_path / 'sample.tar.gz'
|
||||
tarfile.write_text("""Random text""")
|
||||
|
||||
with pytest.raises(UsageError):
|
||||
tiger_data.add_tiger_data(str(tarfile), def_config, 1, tokenizer_mock())
|
||||
await tiger_data.add_tiger_data(str(tarfile), def_config, 1, tokenizer_mock())
|
||||
|
||||
|
||||
def test_add_tiger_data_empty_tarfile(def_config, tiger_table, tokenizer_mock,
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_tiger_data_empty_tarfile(def_config, tiger_table, tokenizer_mock,
|
||||
tmp_path):
|
||||
tar = tarfile.open(str(tmp_path / 'sample.tar.gz'), "w:gz")
|
||||
tar.add(__file__)
|
||||
tar.close()
|
||||
|
||||
tiger_data.add_tiger_data(str(tmp_path / 'sample.tar.gz'), def_config, 1,
|
||||
tokenizer_mock())
|
||||
await tiger_data.add_tiger_data(str(tmp_path / 'sample.tar.gz'), def_config, 1,
|
||||
tokenizer_mock())
|
||||
|
||||
assert tiger_table.count() == 0
|
||||
|
||||
Reference in New Issue
Block a user