]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_replication.py
e06eda59a71d352ecd88e9b801e9a42dc73976b4
[nominatim.git] / test / python / test_tools_replication.py
1 """
2 Tests for replication functionality.
3 """
4 import datetime as dt
5
6 import pytest
7 from osmium.replication.server import OsmosisState
8
9 import nominatim.tools.replication
10 import nominatim.db.status as status
11
12 OSM_NODE_DATA = """\
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">
15 </node>
16 </osm>
17 """
18
19
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)
22
23     monkeypatch.setattr(nominatim.db.status, "get_url", lambda u : OSM_NODE_DATA)
24
25     with pytest.raises(RuntimeError, match="Failed to reach replication service"):
26         nominatim.tools.replication.init_replication(temp_db_conn, 'https://test.io')
27
28
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)
31
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)
36
37     nominatim.tools.replication.init_replication(temp_db_conn, 'https://test.io')
38
39     temp_db_cursor.execute("SELECT * FROM import_status")
40
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]
44
45
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
48
49
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))
52
53     assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 254
54
55
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),
59                       seq=345)
60
61     monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
62                         "get_state_info", lambda self: None)
63
64     assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 253
65
66
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)
72
73     monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
74                         "get_state_info",
75                         lambda self: OsmosisState(server_sequence, date))
76
77     assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == result