1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for replication functionality.
14 from osmium.replication.server import OsmosisState
16 import nominatim_db.tools.replication
17 import nominatim_db.db.status as status
18 from nominatim_db.errors import UsageError
21 <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/">
22 <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">
27 @pytest.fixture(autouse=True)
28 def setup_status_table(status_table):
33 def test_init_replication_bad_base_url(monkeypatch, place_row, temp_db_conn):
34 place_row(osm_type='N', osm_id=100)
36 monkeypatch.setattr(status, "get_url", lambda u: OSM_NODE_DATA)
38 with pytest.raises(UsageError, match="Failed to reach replication service"):
39 nominatim_db.tools.replication.init_replication(temp_db_conn, 'https://test.io')
42 def test_init_replication_success(monkeypatch, place_row, temp_db_conn, temp_db_cursor):
43 place_row(osm_type='N', osm_id=100)
45 monkeypatch.setattr(status, "get_url", lambda u: OSM_NODE_DATA)
46 monkeypatch.setattr(nominatim_db.tools.replication.ReplicationServer,
47 "timestamp_to_sequence",
48 lambda self, date: 234)
50 nominatim_db.tools.replication.init_replication(temp_db_conn, 'https://test.io')
52 expected_date = dt.datetime.strptime('2006-01-27T19:09:10', status.ISODATE_FORMAT)\
53 .replace(tzinfo=dt.timezone.utc)
55 assert temp_db_cursor.row_set("SELECT * FROM import_status") \
56 == {(expected_date, 234, True)}
59 ### checking for updates
61 def test_check_for_updates_empty_status_table(temp_db_conn):
62 assert nominatim_db.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 254
65 def test_check_for_updates_seq_not_set(temp_db_conn):
66 status.set_status(temp_db_conn, dt.datetime.now(dt.timezone.utc))
68 assert nominatim_db.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 254
71 def test_check_for_updates_no_state(monkeypatch, temp_db_conn):
72 status.set_status(temp_db_conn, dt.datetime.now(dt.timezone.utc), seq=345)
74 monkeypatch.setattr(nominatim_db.tools.replication.ReplicationServer,
75 "get_state_info", lambda self: None)
77 assert nominatim_db.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 253
80 @pytest.mark.parametrize("server_sequence,result", [(344, 2), (345, 2), (346, 0)])
81 def test_check_for_updates_no_new_data(monkeypatch, temp_db_conn,
82 server_sequence, result):
83 date = dt.datetime.now(dt.timezone.utc)
84 status.set_status(temp_db_conn, date, seq=345)
86 monkeypatch.setattr(nominatim_db.tools.replication.ReplicationServer,
88 lambda self: OsmosisState(server_sequence, date))
90 assert nominatim_db.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == result
96 def update_options(tmpdir):
97 return dict(base_url='https://test.io',
100 import_file=tmpdir / 'foo.osm',
103 def test_update_empty_status_table(dsn):
104 with pytest.raises(UsageError):
105 nominatim_db.tools.replication.update(dsn, {})
108 def test_update_already_indexed(temp_db_conn, dsn):
109 status.set_status(temp_db_conn, dt.datetime.now(dt.timezone.utc), seq=34, indexed=False)
111 assert nominatim_db.tools.replication.update(dsn, dict(indexed_only=True)) \
112 == nominatim_db.tools.replication.UpdateState.MORE_PENDING
115 def test_update_no_data_no_sleep(monkeypatch, temp_db_conn, dsn, update_options):
116 date = dt.datetime.now(dt.timezone.utc) - dt.timedelta(days=1)
117 status.set_status(temp_db_conn, date, seq=34)
119 monkeypatch.setattr(nominatim_db.tools.replication.ReplicationServer,
121 lambda *args, **kwargs: None)
124 monkeypatch.setattr(time, 'sleep', sleeptime.append)
126 assert nominatim_db.tools.replication.update(dsn, update_options) \
127 == nominatim_db.tools.replication.UpdateState.NO_CHANGES
132 def test_update_no_data_sleep(monkeypatch, temp_db_conn, dsn, update_options):
133 date = dt.datetime.now(dt.timezone.utc) - dt.timedelta(minutes=30)
134 status.set_status(temp_db_conn, date, seq=34)
136 monkeypatch.setattr(nominatim_db.tools.replication.ReplicationServer,
138 lambda *args, **kwargs: None)
141 monkeypatch.setattr(time, 'sleep', sleeptime.append)
143 assert nominatim_db.tools.replication.update(dsn, update_options) \
144 == nominatim_db.tools.replication.UpdateState.NO_CHANGES
146 assert len(sleeptime) == 1
147 assert sleeptime[0] < 3600
148 assert sleeptime[0] > 0