-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathtest_db.py
More file actions
482 lines (338 loc) · 15.5 KB
/
Copy pathtest_db.py
File metadata and controls
482 lines (338 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# -*- coding: utf-8 -*-
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import os
from datetime import datetime
from urllib.parse import urljoin
import pytest
import requests
import responses
from bugbug import db
from bugbug.db import LastModifiedNotAvailable
@pytest.fixture
def mock_db(tmp_path):
def register_db(db_format, db_compression):
db_name = f"prova.{db_format}"
if db_compression is not None:
db_name += f".{db_compression}"
db_path = tmp_path / db_name
db.register(db_path, "https://alink", 1)
return db_path
return register_db
@pytest.mark.parametrize("db_format", ["json", "pickle"])
@pytest.mark.parametrize("db_compression", [None, "gz", "zstd"])
def test_write_read_size(mock_db, db_format, db_compression):
db_path = mock_db(db_format, db_compression)
db.write(db_path, range(1, 8))
assert list(db.read(db_path)) == [1, 2, 3, 4, 5, 6, 7]
assert db.size(db_path) == 7
@pytest.mark.parametrize("db_format", ["json", "pickle"])
@pytest.mark.parametrize("db_compression", [None, "gz", "zstd"])
def test_read_empty(mock_db, db_format, db_compression):
db_path = mock_db(db_format, db_compression)
assert list(db.read(db_path)) == []
assert db.size(db_path) == 0
db.write(db_path, [])
assert list(db.read(db_path)) == []
assert db.size(db_path) == 0
@pytest.mark.parametrize("db_format", ["json", "pickle"])
@pytest.mark.parametrize("db_compression", [None, "gz", "zstd"])
def test_read_while_appending(mock_db, db_format, db_compression):
db_path = mock_db(db_format, db_compression)
assert list(db.read(db_path)) == []
assert db.size(db_path) == 0
def values1():
yield from db.read(db_path)
db.append(db_path, values1())
assert list(db.read(db_path)) == []
assert db.size(db_path) == 0
def values2():
yield from db.read(db_path)
yield from (1, 2, 3)
db.append(db_path, values2())
assert list(db.read(db_path)) == [1, 2, 3]
assert db.size(db_path) == 3
def values3():
yield from db.read(db_path)
yield from (4, 5, 6, 7)
db.append(db_path, values3())
assert list(db.read(db_path)) == [1, 2, 3, 1, 2, 3, 4, 5, 6, 7]
assert db.size(db_path) == 10
@pytest.mark.parametrize("db_format", ["json", "pickle"])
@pytest.mark.parametrize("db_compression", [None, "gz", "zstd"])
def test_append(mock_db, db_format, db_compression):
db_path = mock_db(db_format, db_compression)
db.write(db_path, range(1, 4))
assert list(db.read(db_path)) == [1, 2, 3]
db.append(db_path, range(4, 8))
assert list(db.read(db_path)) == [1, 2, 3, 4, 5, 6, 7]
@pytest.mark.parametrize("db_format", ["json", "pickle"])
@pytest.mark.parametrize("db_compression", [None, "gz", "zstd"])
def test_delete(mock_db, db_format, db_compression):
db_path = mock_db(db_format, db_compression)
db.write(db_path, range(1, 9))
assert list(db.read(db_path)) == [1, 2, 3, 4, 5, 6, 7, 8]
db.delete(db_path, lambda x: x == 4)
assert list(db.read(db_path)) == [1, 2, 3, 5, 6, 7, 8]
def test_delete_not_existent(mock_db):
db_path = mock_db("json", None)
assert not os.path.exists(db_path)
db.delete(db_path, lambda x: x == 4)
assert not os.path.exists(db_path)
def test_unregistered_db(tmp_path):
db_path = tmp_path / "prova.json"
with pytest.raises(AssertionError):
list(db.read(db_path))
with pytest.raises(AssertionError):
db.write(db_path, range(7))
with pytest.raises(AssertionError):
db.append(db_path, range(7))
@pytest.mark.parametrize(
"db_name", ["prova", "prova.", "prova.gz", "prova.unknown.gz", "prova.json.unknown"]
)
def test_bad_format_compression(tmp_path, db_name):
db_path = tmp_path / db_name
db.register(db_path, "https://alink", 1)
with pytest.raises(AssertionError):
db.write(db_path, range(7))
with pytest.raises(AssertionError):
db.append(db_path, range(7))
def test_register_db(tmp_path):
db_path = tmp_path / "prova.json"
db.register(db_path, "https://alink", 1)
assert os.path.exists(db_path.with_suffix(db_path.suffix + ".version"))
def test_exists_db(tmp_path):
db_path = tmp_path / "prova.json"
db.register(db_path, "https://alink", 1)
assert not db.exists(db_path)
db.write(db_path, range(7))
assert db.exists(db_path)
def test_download(tmp_path, mock_zst):
url = "https://community-tc.services.mozilla.com/api/index/v1/task/project.bugbug.data_commits.latest/artifacts/public/prova.json.zst"
url_version = "https://community-tc.services.mozilla.com/api/index/v1/task/project.bugbug.data_commits.latest/artifacts/public/prova.json.version"
db_path = tmp_path / "prova.json"
db.register(db_path, url, 1)
responses.add(responses.GET, url_version, status=200, body="1")
responses.add(
responses.HEAD,
url,
status=200,
headers={
"ETag": "123",
"Accept-Encoding": "zstd",
"Last-Modified": "2019-04-16",
},
)
tmp_zst_path = tmp_path / "prova_tmp.zst"
mock_zst(tmp_zst_path)
with open(tmp_zst_path, "rb") as content:
responses.add(responses.GET, url, status=200, body=content.read())
assert db.download(db_path)
assert db.download(db_path)
assert db.last_modified(db_path) == datetime(2019, 4, 16)
assert os.path.exists(db_path)
assert not os.path.exists(db_path.with_suffix(db_path.suffix + ".zst"))
assert os.path.exists(db_path.with_suffix(db_path.suffix + ".zst.etag"))
def test_download_missing(tmp_path, mock_zst):
url = "https://community-tc.services.mozilla.com/api/index/v1/task/project.bugbug.data_commits.latest/artifacts/public/prova.json.zst"
url_version = "https://community-tc.services.mozilla.com/api/index/v1/task/project.bugbug.data_commits.latest/artifacts/public/prova.json.version"
db_path = tmp_path / "prova.json"
db.register(db_path, url, 1)
responses.add(
responses.HEAD,
url,
status=404,
headers={"ETag": "123", "Accept-Encoding": "zstd"},
)
responses.add(
responses.GET, url, status=404, body=requests.exceptions.HTTPError("HTTP error")
)
responses.add(responses.GET, url_version, status=404)
assert not db.download(db_path)
assert not os.path.exists(db_path)
with pytest.raises(LastModifiedNotAvailable):
db.last_modified(db_path)
def test_download_different_schema(tmp_path, mock_zst):
url = "https://community-tc.services.mozilla.com/api/index/v1/task/project.bugbug.data_commits.latest/artifacts/public/prova.json.zst"
url_version = "https://community-tc.services.mozilla.com/api/index/v1/task/project.bugbug.data_commits.latest/artifacts/public/prova.json.version"
db_path = tmp_path / "prova.json"
db.register(db_path, url, 2)
responses.add(responses.GET, url_version, status=200, body="1")
responses.add(
responses.HEAD,
url,
status=200,
headers={
"ETag": "123",
"Accept-Encoding": "zstd",
"Last-Modified": "2019-04-16",
},
)
tmp_zst_path = tmp_path / "prova_tmp.zst"
mock_zst(tmp_zst_path)
with open(tmp_zst_path, "rb") as content:
responses.add(responses.GET, url, status=200, body=content.read())
assert not db.download(db_path)
with pytest.raises(db.LastModifiedNotAvailable):
db.last_modified(db_path)
assert not os.path.exists(db_path)
assert not os.path.exists(db_path.with_suffix(db_path.suffix + ".zst"))
assert not os.path.exists(db_path.with_suffix(db_path.suffix + ".zst.etag"))
def test_download_same_schema_new_db(tmp_path, mock_zst):
url = "https://community-tc.services.mozilla.com/api/index/v1/task/project.bugbug.data_commits.latest/artifacts/public/prova.json.zst"
url_version = "https://community-tc.services.mozilla.com/api/index/v1/task/project.bugbug.data_commits.latest/artifacts/public/prova.json.version"
db_path = tmp_path / "prova.json"
db.register(db_path, url, 1)
responses.add(responses.GET, url_version, status=200, body="1")
responses.add(
responses.HEAD,
url,
status=200,
headers={
"ETag": "123",
"Accept-Encoding": "zstd",
},
)
responses.add(
responses.HEAD,
url,
status=200,
headers={
"ETag": "456",
"Accept-Encoding": "zstd",
},
)
tmp_zst_path1 = tmp_path / "prova_tmp.zst"
mock_zst(tmp_zst_path1, b"0")
with open(tmp_zst_path1, "rb") as content:
responses.add(responses.GET, url, status=200, body=content.read())
tmp_zst_path2 = tmp_path / "prova_tmp2.zst"
mock_zst(tmp_zst_path2, b"1")
with open(tmp_zst_path2, "rb") as content:
responses.add(responses.GET, url, status=200, body=content.read())
assert db.download(db_path)
assert os.path.exists(db_path)
assert not os.path.exists(db_path.with_suffix(db_path.suffix + ".zst"))
assert os.path.exists(db_path.with_suffix(db_path.suffix + ".zst.etag"))
with open(db_path, "r") as f:
assert f.read() == "0"
assert db.download(db_path)
assert os.path.exists(db_path)
assert not os.path.exists(db_path.with_suffix(db_path.suffix + ".zst"))
assert os.path.exists(db_path.with_suffix(db_path.suffix + ".zst.etag"))
with open(db_path, "r") as f:
assert f.read() == "1"
def test_download_support_file(tmp_path, mock_zst):
url = "https://community-tc.services.mozilla.com/api/index/v1/task/project.bugbug.data_commits.latest/artifacts/public/prova.json.zst"
url_version = "https://community-tc.services.mozilla.com/api/index/v1/task/project.bugbug.data_commits.latest/artifacts/public/prova.json.version"
support_filename = "support.zst"
url_support = urljoin(url, support_filename)
db_path = tmp_path / "prova.json"
db.register(db_path, url, 1, support_files=[support_filename])
responses.add(responses.GET, url_version, status=200, body="1")
responses.add(
responses.HEAD,
url_support,
status=200,
headers={"ETag": "123", "Accept-Encoding": "zstd"},
)
tmp_zst_path = tmp_path / "prova_tmp.zst"
mock_zst(tmp_zst_path)
with open(tmp_zst_path, "rb") as content:
responses.add(responses.GET, url_support, status=200, body=content.read())
assert db.download_support_file(db_path, support_filename)
assert not os.path.exists(os.path.join(os.path.dirname(db_path), support_filename))
assert os.path.exists(
os.path.join(os.path.dirname(db_path), os.path.splitext(support_filename)[0])
)
assert os.path.exists(
os.path.join(
os.path.dirname(db_path),
os.path.splitext(support_filename)[0] + ".zst.etag",
)
)
def test_download_with_support_files_too(tmp_path, mock_zst):
url = "https://community-tc.services.mozilla.com/api/index/v1/task/project.bugbug.data_commits.latest/artifacts/public/prova.json.zst"
url_version = "https://community-tc.services.mozilla.com/api/index/v1/task/project.bugbug.data_commits.latest/artifacts/public/prova.json.version"
support_filename = "support.zst"
url_support = urljoin(url, support_filename)
db_path = tmp_path / "prova.json"
db.register(db_path, url, 1, support_files=[support_filename])
responses.add(responses.GET, url_version, status=200, body="1")
responses.add(
responses.HEAD,
url,
status=200,
headers={"ETag": "123", "Accept-Encoding": "zstd"},
)
responses.add(
responses.HEAD,
url_support,
status=200,
headers={"ETag": "123", "Accept-Encoding": "zstd"},
)
tmp_zst_path = tmp_path / "prova_tmp.zst"
mock_zst(tmp_zst_path)
with open(tmp_zst_path, "rb") as content:
responses.add(responses.GET, url, status=200, body=content.read())
with open(tmp_zst_path, "rb") as content:
responses.add(responses.GET, url_support, status=200, body=content.read())
assert db.download(db_path, support_files_too=True)
assert os.path.exists(db_path)
assert not os.path.exists(db_path.with_suffix(db_path.suffix + ".zst"))
assert os.path.exists(db_path.with_suffix(db_path.suffix + ".zst.etag"))
assert not os.path.exists(os.path.join(os.path.dirname(db_path), support_filename))
assert os.path.exists(
os.path.join(os.path.dirname(db_path), os.path.splitext(support_filename)[0])
)
assert os.path.exists(
os.path.join(
os.path.dirname(db_path),
os.path.splitext(support_filename)[0] + ".zst.etag",
)
)
def test_download_support_file_missing(tmp_path, caplog):
url = "https://community-tc.services.mozilla.com/api/index/v1/task/project.bugbug.data_commits.latest/artifacts/public/commits.json.zst"
url_version = "https://community-tc.services.mozilla.com/api/index/v1/task/project.bugbug.data_commits.latest/artifacts/public/prova.json.version"
support_filename = "support_mock.zst"
url_support = urljoin(url, support_filename)
db_path = tmp_path / "prova.json"
db.register(db_path, url, 1, support_files=[support_filename])
responses.add(responses.GET, url_version, status=404)
responses.add(
responses.HEAD,
url_support,
status=404,
headers={"ETag": "123", "Accept-Encoding": "zstd"},
)
responses.add(
responses.GET,
url_support,
status=404,
body=requests.exceptions.HTTPError("HTTP error"),
)
assert not db.download_support_file(db_path, support_filename)
expected_message = f"Version file is not yet available to download for {db_path}"
assert expected_message in caplog.text
def test_is_different_schema(tmp_path):
url_zst = "https://community-tc.services.mozilla.com/api/index/v1/task/project.bugbug.data_commits.latest/artifacts/public/prova.json.zst"
url_version = "https://community-tc.services.mozilla.com/api/index/v1/task/project.bugbug.data_commits.latest/artifacts/public/prova.json.version"
db_path = tmp_path / "prova.json"
db.register(db_path, url_zst, 1, support_files=[])
assert os.path.exists(db_path.with_suffix(db_path.suffix + ".version"))
responses.add(responses.GET, url_version, status=404)
responses.add(responses.GET, url_version, status=424)
responses.add(responses.GET, url_version, status=200, body="1")
responses.add(responses.GET, url_version, status=200, body="42")
# When the remote version file doesn't exist (due to 404 status), we consider the current db version as being different.
assert db.is_different_schema(db_path)
# When the remote version file doesn't exist (due to 424 status), we consider the current db version as being different.
assert db.is_different_schema(db_path)
# When the remote version file exists and returns the same version as the current db, we consider that the current db version is not different from remote db version.
assert not db.is_different_schema(db_path)
# When the remote version file exists and returns a newer version than the current db, we consider that the current db version is different from remote db version.
assert db.is_different_schema(db_path)
db.register(db_path, url_zst, 43, support_files=[])
# When the remote version file exists and returns an older version than the current db, we consider that the current db version is different from remote db version.
assert db.is_different_schema(db_path)