2 Tests for replication functionality.
7 from osmium.replication.server import OsmosisState
9 import nominatim.tools.replication
10 import nominatim.db.status as status
13 <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/">
14 <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">
20 def test_init_replication_bad_base_url(monkeypatch, status_table, place_row, temp_db_conn, temp_db_cursor):
21 place_row(osm_type='N', osm_id=100)
23 monkeypatch.setattr(nominatim.db.status, "get_url", lambda u : OSM_NODE_DATA)
25 with pytest.raises(RuntimeError, match="Failed to reach replication service"):
26 nominatim.tools.replication.init_replication(temp_db_conn, 'https://test.io')
29 def test_init_replication_success(monkeypatch, status_table, place_row, temp_db_conn, temp_db_cursor):
30 place_row(osm_type='N', osm_id=100)
32 monkeypatch.setattr(nominatim.db.status, "get_url", lambda u : OSM_NODE_DATA)
33 monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
34 "timestamp_to_sequence",
35 lambda self, date: 234)
37 nominatim.tools.replication.init_replication(temp_db_conn, 'https://test.io')
39 temp_db_cursor.execute("SELECT * FROM import_status")
41 expected_date = dt.datetime.fromisoformat('2006-01-27T19:09:10').replace(tzinfo=dt.timezone.utc)
42 assert temp_db_cursor.rowcount == 1
43 assert temp_db_cursor.fetchone() == [expected_date, 234, True]
46 def test_check_for_updates_empty_status_table(status_table, temp_db_conn):
47 assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 254
50 def test_check_for_updates_seq_not_set(status_table, temp_db_conn):
51 status.set_status(temp_db_conn, dt.datetime.now().replace(tzinfo=dt.timezone.utc))
53 assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 254
56 def test_check_for_updates_no_state(monkeypatch, status_table, temp_db_conn):
57 status.set_status(temp_db_conn,
58 dt.datetime.now().replace(tzinfo=dt.timezone.utc),
61 monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
62 "get_state_info", lambda self: None)
64 assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 253
67 @pytest.mark.parametrize("server_sequence,result", [(344, 1), (345, 1), (346, 0)])
68 def test_check_for_updates_no_new_data(monkeypatch, status_table, temp_db_conn,
69 server_sequence, result):
70 date = dt.datetime.now().replace(tzinfo=dt.timezone.utc)
71 status.set_status(temp_db_conn, date, seq=345)
73 monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
75 lambda self: OsmosisState(server_sequence, date))
77 assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == result