]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_replication.py
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / test / python / test_tools_replication.py
1 """
2 Tests for replication functionality.
3 """
4 import datetime as dt
5 import time
6
7 import pytest
8 from osmium.replication.server import OsmosisState
9
10 import nominatim.tools.replication
11 import nominatim.db.status as status
12 from nominatim.errors import UsageError
13
14 OSM_NODE_DATA = """\
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">
17 </node>
18 </osm>
19 """
20
21 ### init replication
22
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)
25
26     monkeypatch.setattr(nominatim.db.status, "get_url", lambda u : OSM_NODE_DATA)
27
28     with pytest.raises(UsageError, match="Failed to reach replication service"):
29         nominatim.tools.replication.init_replication(temp_db_conn, 'https://test.io')
30
31
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)
34
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)
39
40     nominatim.tools.replication.init_replication(temp_db_conn, 'https://test.io')
41
42     temp_db_cursor.execute("SELECT * FROM import_status")
43
44     expected_date = dt.datetime.fromisoformat('2006-01-27T19:09:10').replace(tzinfo=dt.timezone.utc)
45     assert temp_db_cursor.rowcount == 1
46     assert temp_db_cursor.fetchone() == [expected_date, 234, True]
47
48
49 ### checking for updates
50
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
53
54
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))
57
58     assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 254
59
60
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)
63
64     monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
65                         "get_state_info", lambda self: None)
66
67     assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 253
68
69
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)
75
76     monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
77                         "get_state_info",
78                         lambda self: OsmosisState(server_sequence, date))
79
80     assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == result
81
82
83 ### updating
84
85 @pytest.fixture
86 def update_options(tmpdir):
87     return dict(base_url='https://test.io',
88                    indexed_only=False,
89                    update_interval=3600,
90                    import_file=tmpdir / 'foo.osm',
91                    max_diff_size=1)
92
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, {})
96
97
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)
100
101     assert nominatim.tools.replication.update(temp_db_conn, dict(indexed_only=True)) \
102              == nominatim.tools.replication.UpdateState.MORE_PENDING
103
104
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)
108
109     monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
110                         "apply_diffs",
111                         lambda *args, **kwargs: None)
112
113     sleeptime = []
114     monkeypatch.setattr(time, 'sleep', lambda s: sleeptime.append(s))
115
116     assert nominatim.tools.replication.update(temp_db_conn, update_options) \
117              == nominatim.tools.replication.UpdateState.NO_CHANGES
118
119     assert not sleeptime
120
121
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)
125
126     monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
127                         "apply_diffs",
128                         lambda *args, **kwargs: None)
129
130     sleeptime = []
131     monkeypatch.setattr(time, 'sleep', lambda s: sleeptime.append(s))
132
133     assert nominatim.tools.replication.update(temp_db_conn, update_options) \
134              == nominatim.tools.replication.UpdateState.NO_CHANGES
135
136     assert len(sleeptime) == 1
137     assert sleeptime[0] < 3600
138     assert sleeptime[0] > 0