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, temp_db_cursor):
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 temp_db_cursor.execute("SELECT * FROM import_status")
44 expected_date = dt.datetime.strptime('2006-01-27T19:09:10', status.ISODATE_FORMAT)\
45 .replace(tzinfo=dt.timezone.utc)
46 assert temp_db_cursor.rowcount == 1
47 assert temp_db_cursor.fetchone() == [expected_date, 234, True]
50 ### checking for updates
52 def test_check_for_updates_empty_status_table(status_table, temp_db_conn):
53 assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 254
56 def test_check_for_updates_seq_not_set(status_table, temp_db_conn):
57 status.set_status(temp_db_conn, dt.datetime.now(dt.timezone.utc))
59 assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 254
62 def test_check_for_updates_no_state(monkeypatch, status_table, temp_db_conn):
63 status.set_status(temp_db_conn, dt.datetime.now(dt.timezone.utc), seq=345)
65 monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
66 "get_state_info", lambda self: None)
68 assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 253
71 @pytest.mark.parametrize("server_sequence,result", [(344, 2), (345, 2), (346, 0)])
72 def test_check_for_updates_no_new_data(monkeypatch, status_table, temp_db_conn,
73 server_sequence, result):
74 date = dt.datetime.now(dt.timezone.utc)
75 status.set_status(temp_db_conn, date, seq=345)
77 monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
79 lambda self: OsmosisState(server_sequence, date))
81 assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == result
87 def update_options(tmpdir):
88 return dict(base_url='https://test.io',
91 import_file=tmpdir / 'foo.osm',
94 def test_update_empty_status_table(status_table, temp_db_conn):
95 with pytest.raises(UsageError):
96 nominatim.tools.replication.update(temp_db_conn, {})
99 def test_update_already_indexed(status_table, temp_db_conn):
100 status.set_status(temp_db_conn, dt.datetime.now(dt.timezone.utc), seq=34, indexed=False)
102 assert nominatim.tools.replication.update(temp_db_conn, dict(indexed_only=True)) \
103 == nominatim.tools.replication.UpdateState.MORE_PENDING
106 def test_update_no_data_no_sleep(monkeypatch, status_table, temp_db_conn, update_options):
107 date = dt.datetime.now(dt.timezone.utc) - dt.timedelta(days=1)
108 status.set_status(temp_db_conn, date, seq=34)
110 monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
112 lambda *args, **kwargs: None)
115 monkeypatch.setattr(time, 'sleep', lambda s: sleeptime.append(s))
117 assert nominatim.tools.replication.update(temp_db_conn, update_options) \
118 == nominatim.tools.replication.UpdateState.NO_CHANGES
123 def test_update_no_data_sleep(monkeypatch, status_table, temp_db_conn, update_options):
124 date = dt.datetime.now(dt.timezone.utc) - dt.timedelta(minutes=30)
125 status.set_status(temp_db_conn, date, seq=34)
127 monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
129 lambda *args, **kwargs: None)
132 monkeypatch.setattr(time, 'sleep', lambda s: sleeptime.append(s))
134 assert nominatim.tools.replication.update(temp_db_conn, update_options) \
135 == nominatim.tools.replication.UpdateState.NO_CHANGES
137 assert len(sleeptime) == 1
138 assert sleeptime[0] < 3600
139 assert sleeptime[0] > 0