forked from hans/Nominatim
port replication initialisation to Python
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import itertools
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
@@ -11,6 +12,7 @@ SRC_DIR = Path(__file__) / '..' / '..' / '..'
|
||||
sys.path.insert(0, str(SRC_DIR.resolve()))
|
||||
|
||||
from nominatim.config import Configuration
|
||||
from nominatim.db import connection
|
||||
|
||||
class _TestingCursor(psycopg2.extras.DictCursor):
|
||||
""" Extension to the DictCursor class that provides execution
|
||||
@@ -40,27 +42,42 @@ def temp_db(monkeypatch):
|
||||
exported into NOMINATIM_DATABASE_DSN.
|
||||
"""
|
||||
name = 'test_nominatim_python_unittest'
|
||||
with psycopg2.connect(database='postgres') as conn:
|
||||
conn.set_isolation_level(0)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute('DROP DATABASE IF EXISTS {}'.format(name))
|
||||
cur.execute('CREATE DATABASE {}'.format(name))
|
||||
conn = psycopg2.connect(database='postgres')
|
||||
|
||||
conn.set_isolation_level(0)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute('DROP DATABASE IF EXISTS {}'.format(name))
|
||||
cur.execute('CREATE DATABASE {}'.format(name))
|
||||
|
||||
conn.close()
|
||||
|
||||
monkeypatch.setenv('NOMINATIM_DATABASE_DSN' , 'dbname=' + name)
|
||||
|
||||
yield name
|
||||
|
||||
with psycopg2.connect(database='postgres') as conn:
|
||||
conn.set_isolation_level(0)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute('DROP DATABASE IF EXISTS {}'.format(name))
|
||||
conn = psycopg2.connect(database='postgres')
|
||||
|
||||
conn.set_isolation_level(0)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute('DROP DATABASE IF EXISTS {}'.format(name))
|
||||
|
||||
conn.close()
|
||||
|
||||
@pytest.fixture
|
||||
def temp_db_with_extensions(temp_db):
|
||||
conn = psycopg2.connect(database=temp_db)
|
||||
with conn.cursor() as cur:
|
||||
cur.execute('CREATE EXTENSION hstore; CREATE EXTENSION postgis;')
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
return temp_db
|
||||
|
||||
@pytest.fixture
|
||||
def temp_db_conn(temp_db):
|
||||
""" Connection to the test database.
|
||||
"""
|
||||
conn = psycopg2.connect(database=temp_db)
|
||||
conn = connection.connect('dbname=' + temp_db)
|
||||
yield conn
|
||||
conn.close()
|
||||
|
||||
@@ -80,3 +97,50 @@ def temp_db_cursor(temp_db):
|
||||
@pytest.fixture
|
||||
def def_config():
|
||||
return Configuration(None, SRC_DIR.resolve() / 'settings')
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def status_table(temp_db_conn):
|
||||
""" Create an empty version of the status table.
|
||||
"""
|
||||
with temp_db_conn.cursor() as cur:
|
||||
cur.execute("""CREATE TABLE import_status (
|
||||
lastimportdate timestamp with time zone NOT NULL,
|
||||
sequence_id integer,
|
||||
indexed boolean
|
||||
)""")
|
||||
temp_db_conn.commit()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def place_table(temp_db_with_extensions, temp_db_conn):
|
||||
""" Create an empty version of the place table.
|
||||
"""
|
||||
with temp_db_conn.cursor() as cur:
|
||||
cur.execute("""CREATE TABLE place (
|
||||
osm_id int8 NOT NULL,
|
||||
osm_type char(1) NOT NULL,
|
||||
class text NOT NULL,
|
||||
type text NOT NULL,
|
||||
name hstore,
|
||||
admin_level smallint,
|
||||
address hstore,
|
||||
extratags hstore,
|
||||
geometry Geometry(Geometry,4326) NOT NULL)""")
|
||||
temp_db_conn.commit()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def place_row(place_table, temp_db_cursor):
|
||||
""" A factory for rows in the place table. The table is created as a
|
||||
prerequisite to the fixture.
|
||||
"""
|
||||
idseq = itertools.count(1001)
|
||||
def _insert(osm_type='N', osm_id=None, cls='amenity', typ='cafe', names=None,
|
||||
admin_level=None, address=None, extratags=None, geom=None):
|
||||
temp_db_cursor.execute("INSERT INTO place VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)",
|
||||
(osm_id or next(idseq), osm_type, cls, typ, names,
|
||||
admin_level, address, extratags,
|
||||
geom or 'SRID=4326;POINT(0 0 )'))
|
||||
|
||||
return _insert
|
||||
|
||||
@@ -7,6 +7,7 @@ import pytest
|
||||
import nominatim.cli
|
||||
import nominatim.indexer.indexer
|
||||
import nominatim.tools.refresh
|
||||
import nominatim.tools.replication
|
||||
|
||||
def call_nominatim(*args):
|
||||
return nominatim.cli.nominatim(module_dir='build/module',
|
||||
@@ -56,7 +57,6 @@ def test_cli_help(capsys):
|
||||
(('import', '--continue', 'load-data'), 'setup'),
|
||||
(('freeze',), 'setup'),
|
||||
(('special-phrases',), 'specialphrases'),
|
||||
(('replication',), 'update'),
|
||||
(('add-data', '--tiger-data', 'tiger'), 'setup'),
|
||||
(('add-data', '--file', 'foo.osm'), 'update'),
|
||||
(('check-database',), 'check_import_finished'),
|
||||
@@ -102,7 +102,7 @@ def test_index_command(monkeypatch, temp_db_cursor, params, do_bnds, do_ranks):
|
||||
('importance', ('update.php', '--recompute-importance')),
|
||||
('website', ('setup.php', '--setup-website')),
|
||||
])
|
||||
def test_refresh_legacy_command(mock_run_legacy, command, params):
|
||||
def test_refresh_legacy_command(mock_run_legacy, temp_db, command, params):
|
||||
assert 0 == call_nominatim('refresh', '--' + command)
|
||||
|
||||
assert mock_run_legacy.called == 1
|
||||
@@ -115,7 +115,7 @@ def test_refresh_legacy_command(mock_run_legacy, command, params):
|
||||
('address-levels', 'load_address_levels_from_file'),
|
||||
('functions', 'create_functions'),
|
||||
])
|
||||
def test_refresh_command(monkeypatch, command, func):
|
||||
def test_refresh_command(monkeypatch, temp_db, command, func):
|
||||
func_mock = MockParamCapture()
|
||||
monkeypatch.setattr(nominatim.tools.refresh, func, func_mock)
|
||||
|
||||
@@ -123,12 +123,26 @@ def test_refresh_command(monkeypatch, command, func):
|
||||
|
||||
assert func_mock.called == 1
|
||||
|
||||
def test_refresh_importance_computed_after_wiki_import(mock_run_legacy):
|
||||
|
||||
def test_refresh_importance_computed_after_wiki_import(mock_run_legacy, temp_db):
|
||||
assert 0 == call_nominatim('refresh', '--importance', '--wiki-data')
|
||||
|
||||
assert mock_run_legacy.called == 2
|
||||
assert mock_run_legacy.last_args == ('update.php', '--recompute-importance')
|
||||
|
||||
|
||||
@pytest.mark.parametrize("params,func", [
|
||||
(('--init', '--no-update-functions'), 'init_replication')
|
||||
])
|
||||
def test_replication_command(monkeypatch, temp_db, params, func):
|
||||
func_mock = MockParamCapture()
|
||||
monkeypatch.setattr(nominatim.tools.replication, func, func_mock)
|
||||
|
||||
assert 0 == call_nominatim('replication', *params)
|
||||
|
||||
assert func_mock.called == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("params", [
|
||||
('search', '--query', 'new'),
|
||||
('reverse', '--lat', '0', '--lon', '0'),
|
||||
|
||||
74
test/python/test_db_status.py
Normal file
74
test/python/test_db_status.py
Normal file
@@ -0,0 +1,74 @@
|
||||
"""
|
||||
Tests for status table manipulation.
|
||||
"""
|
||||
import datetime as dt
|
||||
|
||||
import pytest
|
||||
|
||||
import nominatim.db.status
|
||||
|
||||
def test_compute_database_date_place_empty(status_table, place_table, temp_db_conn):
|
||||
with pytest.raises(RuntimeError):
|
||||
nominatim.db.status.compute_database_date(temp_db_conn)
|
||||
|
||||
OSM_NODE_DATA = """\
|
||||
<osm version="0.6" generator="OpenStreetMap server" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/">
|
||||
<node id="45673" visible="true" version="1" changeset="2047" timestamp="2006-01-27T22:09:10Z" user="Foo" uid="111" lat="48.7586670" lon="8.1343060">
|
||||
</node>
|
||||
</osm>
|
||||
"""
|
||||
|
||||
def test_compute_database_date_valid(monkeypatch, status_table, place_row, temp_db_conn):
|
||||
place_row(osm_type='N', osm_id=45673)
|
||||
|
||||
requested_url = []
|
||||
def mock_url(url):
|
||||
requested_url.append(url)
|
||||
return OSM_NODE_DATA
|
||||
|
||||
monkeypatch.setattr(nominatim.db.status, "get_url", mock_url)
|
||||
|
||||
date = nominatim.db.status.compute_database_date(temp_db_conn)
|
||||
|
||||
assert requested_url == ['https://www.openstreetmap.org/api/0.6/node/45673/1']
|
||||
assert date == dt.datetime.fromisoformat('2006-01-27T22:09:10').replace(tzinfo=dt.timezone.utc)
|
||||
|
||||
|
||||
def test_compute_database_broken_api(monkeypatch, status_table, place_row, temp_db_conn):
|
||||
place_row(osm_type='N', osm_id=45673)
|
||||
|
||||
requested_url = []
|
||||
def mock_url(url):
|
||||
requested_url.append(url)
|
||||
return '<osm version="0.6" generator="OpenStre'
|
||||
|
||||
monkeypatch.setattr(nominatim.db.status, "get_url", mock_url)
|
||||
|
||||
with pytest.raises(RuntimeError):
|
||||
date = nominatim.db.status.compute_database_date(temp_db_conn)
|
||||
|
||||
|
||||
def test_set_status_empty_table(status_table, temp_db_conn, temp_db_cursor):
|
||||
date = dt.datetime.fromordinal(1000000).replace(tzinfo=dt.timezone.utc)
|
||||
nominatim.db.status.set_status(temp_db_conn, date=date)
|
||||
|
||||
temp_db_cursor.execute("SELECT * FROM import_status")
|
||||
|
||||
assert temp_db_cursor.rowcount == 1
|
||||
assert temp_db_cursor.fetchone() == [date, None, True]
|
||||
|
||||
|
||||
def test_set_status_filled_table(status_table, temp_db_conn, temp_db_cursor):
|
||||
date = dt.datetime.fromordinal(1000000).replace(tzinfo=dt.timezone.utc)
|
||||
nominatim.db.status.set_status(temp_db_conn, date=date)
|
||||
|
||||
assert 1 == temp_db_cursor.scalar("SELECT count(*) FROM import_status")
|
||||
|
||||
|
||||
date = dt.datetime.fromordinal(1000100).replace(tzinfo=dt.timezone.utc)
|
||||
nominatim.db.status.set_status(temp_db_conn, date=date, seq=456, indexed=False)
|
||||
|
||||
temp_db_cursor.execute("SELECT * FROM import_status")
|
||||
|
||||
assert temp_db_cursor.rowcount == 1
|
||||
assert temp_db_cursor.fetchone() == [date, 456, False]
|
||||
41
test/python/test_tools_replication.py
Normal file
41
test/python/test_tools_replication.py
Normal file
@@ -0,0 +1,41 @@
|
||||
"""
|
||||
Tests for replication functionality.
|
||||
"""
|
||||
import datetime as dt
|
||||
|
||||
import pytest
|
||||
|
||||
import nominatim.tools.replication
|
||||
|
||||
OSM_NODE_DATA = """\
|
||||
<osm version="0.6" generator="OpenStreetMap server" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/">
|
||||
<node id="100" visible="true" version="1" changeset="2047" timestamp="2006-01-27T22:09:10Z" user="Foo" uid="111" lat="48.7586670" lon="8.1343060">
|
||||
</node>
|
||||
</osm>
|
||||
"""
|
||||
|
||||
|
||||
def test_init_replication_bad_base_url(monkeypatch, status_table, place_row, temp_db_conn, temp_db_cursor):
|
||||
place_row(osm_type='N', osm_id=100)
|
||||
|
||||
monkeypatch.setattr(nominatim.db.status, "get_url", lambda u : OSM_NODE_DATA)
|
||||
|
||||
with pytest.raises(RuntimeError, match="Failed to reach replication service"):
|
||||
nominatim.tools.replication.init_replication(temp_db_conn, 'https://test.io')
|
||||
|
||||
|
||||
def test_init_replication_success(monkeypatch, status_table, place_row, temp_db_conn, temp_db_cursor):
|
||||
place_row(osm_type='N', osm_id=100)
|
||||
|
||||
monkeypatch.setattr(nominatim.db.status, "get_url", lambda u : OSM_NODE_DATA)
|
||||
monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
|
||||
"timestamp_to_sequence",
|
||||
lambda self, date: 234)
|
||||
|
||||
nominatim.tools.replication.init_replication(temp_db_conn, 'https://test.io')
|
||||
|
||||
temp_db_cursor.execute("SELECT * FROM import_status")
|
||||
|
||||
expected_date = dt.datetime.fromisoformat('2006-01-27T19:09:10').replace(tzinfo=dt.timezone.utc)
|
||||
assert temp_db_cursor.rowcount == 1
|
||||
assert temp_db_cursor.fetchone() == [expected_date, 234, True]
|
||||
Reference in New Issue
Block a user