2 Tests for replication functionality.
8 from osmium.replication.server import OsmosisState
10 import nominatim.tools.replication
11 import nominatim.db.status as status
12 from nominatim.errors import UsageError
15 <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/">
16 <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">
23 def test_init_replication_bad_base_url(monkeypatch, status_table, place_row, temp_db_conn):
24 place_row(osm_type='N', osm_id=100)
26 monkeypatch.setattr(nominatim.db.status, "get_url", lambda u : OSM_NODE_DATA)
28 with pytest.raises(UsageError, match="Failed to reach replication service"):
29 nominatim.tools.replication.init_replication(temp_db_conn, 'https://test.io')
32 def test_init_replication_success(monkeypatch, status_table, place_row, temp_db_conn, temp_db_cursor):
33 place_row(osm_type='N', osm_id=100)
35 monkeypatch.setattr(nominatim.db.status, "get_url", lambda u : OSM_NODE_DATA)
36 monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
37 "timestamp_to_sequence",
38 lambda self, date: 234)
40 nominatim.tools.replication.init_replication(temp_db_conn, 'https://test.io')
42 expected_date = dt.datetime.strptime('2006-01-27T19:09:10', status.ISODATE_FORMAT)\
43 .replace(tzinfo=dt.timezone.utc)
45 assert temp_db_cursor.row_set("SELECT * FROM import_status") \
46 == {(expected_date, 234, True)}
49 ### checking for updates
51 def test_check_for_updates_empty_status_table(status_table, temp_db_conn):
52 assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 254
55 def test_check_for_updates_seq_not_set(status_table, temp_db_conn):
56 status.set_status(temp_db_conn, dt.datetime.now(dt.timezone.utc))
58 assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 254
61 def test_check_for_updates_no_state(monkeypatch, status_table, temp_db_conn):
62 status.set_status(temp_db_conn, dt.datetime.now(dt.timezone.utc), seq=345)
64 monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
65 "get_state_info", lambda self: None)
67 assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 253
70 @pytest.mark.parametrize("server_sequence,result", [(344, 2), (345, 2), (346, 0)])
71 def test_check_for_updates_no_new_data(monkeypatch, status_table, temp_db_conn,
72 server_sequence, result):
73 date = dt.datetime.now(dt.timezone.utc)
74 status.set_status(temp_db_conn, date, seq=345)
76 monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
78 lambda self: OsmosisState(server_sequence, date))
80 assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == result
86 def update_options(tmpdir):
87 return dict(base_url='https://test.io',
90 import_file=tmpdir / 'foo.osm',
93 def test_update_empty_status_table(status_table, temp_db_conn):
94 with pytest.raises(UsageError):
95 nominatim.tools.replication.update(temp_db_conn, {})
98 def test_update_already_indexed(status_table, temp_db_conn):
99 status.set_status(temp_db_conn, dt.datetime.now(dt.timezone.utc), seq=34, indexed=False)
101 assert nominatim.tools.replication.update(temp_db_conn, dict(indexed_only=True)) \
102 == nominatim.tools.replication.UpdateState.MORE_PENDING
105 def test_update_no_data_no_sleep(monkeypatch, status_table, temp_db_conn, update_options):
106 date = dt.datetime.now(dt.timezone.utc) - dt.timedelta(days=1)
107 status.set_status(temp_db_conn, date, seq=34)
109 monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
111 lambda *args, **kwargs: None)
114 monkeypatch.setattr(time, 'sleep', lambda s: sleeptime.append(s))
116 assert nominatim.tools.replication.update(temp_db_conn, update_options) \
117 == nominatim.tools.replication.UpdateState.NO_CHANGES
122 def test_update_no_data_sleep(monkeypatch, status_table, temp_db_conn, update_options):
123 date = dt.datetime.now(dt.timezone.utc) - dt.timedelta(minutes=30)
124 status.set_status(temp_db_conn, date, seq=34)
126 monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
128 lambda *args, **kwargs: None)
131 monkeypatch.setattr(time, 'sleep', lambda s: sleeptime.append(s))
133 assert nominatim.tools.replication.update(temp_db_conn, update_options) \
134 == nominatim.tools.replication.UpdateState.NO_CHANGES
136 assert len(sleeptime) == 1
137 assert sleeptime[0] < 3600
138 assert sleeptime[0] > 0