]> git.openstreetmap.org Git - nominatim.git/blob - test/python/test_tools_replication.py
Refactoring loading of external special phrases and importation process by introducin...
[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.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]
48
49
50 ### checking for updates
51
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
54
55
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))
58
59     assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 254
60
61
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)
64
65     monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
66                         "get_state_info", lambda self: None)
67
68     assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == 253
69
70
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)
76
77     monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
78                         "get_state_info",
79                         lambda self: OsmosisState(server_sequence, date))
80
81     assert nominatim.tools.replication.check_for_updates(temp_db_conn, 'https://test.io') == result
82
83
84 ### updating
85
86 @pytest.fixture
87 def update_options(tmpdir):
88     return dict(base_url='https://test.io',
89                    indexed_only=False,
90                    update_interval=3600,
91                    import_file=tmpdir / 'foo.osm',
92                    max_diff_size=1)
93
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, {})
97
98
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)
101
102     assert nominatim.tools.replication.update(temp_db_conn, dict(indexed_only=True)) \
103              == nominatim.tools.replication.UpdateState.MORE_PENDING
104
105
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)
109
110     monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
111                         "apply_diffs",
112                         lambda *args, **kwargs: None)
113
114     sleeptime = []
115     monkeypatch.setattr(time, 'sleep', lambda s: sleeptime.append(s))
116
117     assert nominatim.tools.replication.update(temp_db_conn, update_options) \
118              == nominatim.tools.replication.UpdateState.NO_CHANGES
119
120     assert not sleeptime
121
122
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)
126
127     monkeypatch.setattr(nominatim.tools.replication.ReplicationServer,
128                         "apply_diffs",
129                         lambda *args, **kwargs: None)
130
131     sleeptime = []
132     monkeypatch.setattr(time, 'sleep', lambda s: sleeptime.append(s))
133
134     assert nominatim.tools.replication.update(temp_db_conn, update_options) \
135              == nominatim.tools.replication.UpdateState.NO_CHANGES
136
137     assert len(sleeptime) == 1
138     assert sleeptime[0] < 3600
139     assert sleeptime[0] > 0