Skip to content

Commit a477782

Browse files
committed
future-proof test suite
* Prefer <foo>.assert_called_* methods over assert <foo>.called_* * test_create_mixin: replace a MockCoroutine that had been mocking a non-async method with a regular Mock object * Box: update base64 sha that had been incorrectly calculated. * Update celery task move/copy test infrastructure. unittest-style test classes weren't working well with our task tests and fixtures. Switch to pytest style setup/teardown via conftest.py. Fixtures have been moved to conftest as well. * Add some clarifying documentation and debug logging to .tasks.core
1 parent 43b24b6 commit a477782

File tree

7 files changed

+320
-352
lines changed

7 files changed

+320
-352
lines changed

tests/providers/box/test_provider.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def file_content():
6161

6262
@pytest.fixture
6363
def file_sha_b64():
64-
return 'KrIc0sRT6ELxGc/oX3hZvabgltE='
64+
return '2jmj7l5rSw0yVb/vlWAYkK/YBwk='
6565

6666

6767
@pytest.fixture
@@ -342,7 +342,7 @@ async def test_upload_limit_contiguous_upload(self, provider, file_stream):
342342
path = WaterButlerPath('/foobah/', _ids=('0', '1'))
343343
await provider.upload(file_stream, path)
344344

345-
assert provider._contiguous_upload.called_with(file_stream, path)
345+
provider._contiguous_upload.assert_called_with(path, file_stream)
346346
assert not provider._chunked_upload.called
347347

348348
provider.NONCHUNKED_UPLOAD_LIMIT = NONCHUNKED_UPLOAD_LIMIT
@@ -361,7 +361,7 @@ async def test_upload_limit_chunked_upload(self, provider, file_stream):
361361
path = WaterButlerPath('/foobah/', _ids=('0', '1'))
362362
await provider.upload(file_stream, path)
363363

364-
assert provider._chunked_upload.called_with(file_stream, path)
364+
provider._chunked_upload.assert_called_with(path, file_stream)
365365
assert not provider._contiguous_upload.called
366366

367367
provider.NONCHUNKED_UPLOAD_LIMIT = NONCHUNKED_UPLOAD_LIMIT
@@ -387,9 +387,9 @@ async def test_chunked_upload(self, provider, file_stream, file_sha_b64,
387387
path = WaterButlerPath('/foobah/', _ids=('0', '1'))
388388
await provider._chunked_upload(path, file_stream)
389389

390-
assert provider._create_chunked_upload_session.called_with(path, file_stream)
391-
assert provider._upload_parts.called_with(file_stream, session_metadata)
392-
assert provider._complete_chunked_upload_session.called_with(session_metadata,
390+
provider._create_chunked_upload_session.assert_called_with(path, file_stream)
391+
provider._upload_parts.assert_called_with(file_stream, session_metadata)
392+
provider._complete_chunked_upload_session.assert_called_with(session_metadata,
393393
parts_manifest,
394394
file_sha_b64)
395395

tests/providers/s3/test_provider.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ async def test_chunked_upload_limit_chunked(self, provider, file_stream, mock_ti
458458

459459
await provider.upload(file_stream, path)
460460

461-
assert provider._chunked_upload.called_with(file_stream, path)
461+
provider._chunked_upload.assert_called_with(file_stream, path)
462462

463463
# Fixtures are shared between tests. Need to revert the settings back.
464464
provider.CONTIGUOUS_UPLOAD_SIZE_LIMIT = pd_settings.CONTIGUOUS_UPLOAD_SIZE_LIMIT
@@ -477,7 +477,7 @@ async def test_chunked_upload_limit_contiguous(self, provider, file_stream, mock
477477

478478
await provider.upload(file_stream, path)
479479

480-
assert provider._contiguous_upload.called_with(file_stream, path)
480+
provider._contiguous_upload.assert_called_with(file_stream, path)
481481

482482
provider.CONTIGUOUS_UPLOAD_SIZE_LIMIT = pd_settings.CONTIGUOUS_UPLOAD_SIZE_LIMIT
483483
provider.CHUNK_SIZE = pd_settings.CHUNK_SIZE
@@ -1277,7 +1277,7 @@ async def test_intra_copy(self, provider, file_header_metadata, mock_time):
12771277
metadata, exists = await provider.intra_copy(provider, source_path, dest_path)
12781278

12791279

1280-
assert provider._check_region.called
1280+
provider._check_region.assert_called()
12811281

12821282
assert metadata.kind == 'file'
12831283
assert not exists

tests/server/api/v1/test_create_mixin.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ async def test_postvalidate_put_folder(self, http_request):
3535
handler.kind = 'folder'
3636
handler.get_query_argument = mock.Mock(return_value='child!')
3737
handler.provider.exists = MockCoroutine(return_value=False)
38-
handler.provider.can_duplicate_names = MockCoroutine(return_value=False)
38+
handler.provider.can_duplicate_names = mock.Mock(return_value=False)
3939

4040
await handler.postvalidate_put()
4141

4242
assert handler.target_path == WaterButlerPath('/Folder1/child!/')
4343
handler.get_query_argument.assert_called_once_with('name', default=None)
44-
handler.provider.exists.assert_called_once_with(
45-
WaterButlerPath('/Folder1/child!', prepend=None))
44+
45+
handler.provider.exists.assert_has_calls([
46+
mock.call(WaterButlerPath('/Folder1/child!', prepend=None)),
47+
mock.call(WaterButlerPath('/Folder1/child!', prepend=None))
48+
])
4649

4750
@pytest.mark.asyncio
4851
async def test_postvalidate_put_folder_naming_conflict(self, http_request):

tests/tasks/conftest.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import time
2+
import asyncio
3+
from unittest import mock
4+
5+
import pytest
6+
7+
from waterbutler.core import remote_logging
8+
from waterbutler.core import utils as core_utils
9+
from waterbutler.core.path import WaterButlerPath
10+
11+
import tests.utils as test_utils
12+
13+
14+
# Task testing doesn't play nice with unittest-style tests, so fallback to regular pytest-style
15+
# setup/teardown.
16+
17+
def pytest_runtest_setup(item):
18+
policy = asyncio.get_event_loop_policy()
19+
policy.get_event_loop().close()
20+
event_loop = policy.new_event_loop()
21+
policy.set_event_loop(event_loop)
22+
23+
24+
def pytest_runtest_teardown(item):
25+
policy = asyncio.get_event_loop_policy()
26+
policy.get_event_loop().close()
27+
28+
29+
# Fixtures for task tests
30+
31+
@pytest.fixture(autouse=True)
32+
def log_to_keen(monkeypatch):
33+
mock_log_to_keen = test_utils.MockCoroutine()
34+
monkeypatch.setattr(remote_logging, 'log_to_keen', mock_log_to_keen)
35+
return mock_log_to_keen
36+
37+
38+
@pytest.fixture
39+
def callback(monkeypatch):
40+
mock_request = test_utils.MockCoroutine(return_value=(200, b'meowmeowmeow'))
41+
monkeypatch.setattr(core_utils, 'send_signed_request', mock_request)
42+
return mock_request
43+
44+
45+
@pytest.fixture
46+
def FAKE_TIME():
47+
return 1454684930.0
48+
49+
50+
@pytest.fixture
51+
def mock_time(monkeypatch, FAKE_TIME):
52+
mock_time = mock.Mock(return_value=FAKE_TIME)
53+
monkeypatch.setattr(time, 'time', mock_time)
54+
55+
56+
@pytest.fixture
57+
def src_path():
58+
return WaterButlerPath('/user/bin/python')
59+
60+
61+
@pytest.fixture
62+
def dest_path():
63+
return WaterButlerPath('/usr/bin/golang')
64+
65+
66+
@pytest.fixture
67+
def src_bundle(src_path):
68+
return {
69+
'nid': 'mst3k',
70+
'path': src_path,
71+
'provider': {
72+
'name': 'src',
73+
'auth': {
74+
'callback_url': '',
75+
},
76+
'settings': {},
77+
'credentials': {},
78+
}
79+
}
80+
81+
82+
@pytest.fixture
83+
def dest_bundle(dest_path):
84+
return {
85+
'nid': 'fbi4u',
86+
'path': dest_path,
87+
'provider': {
88+
'name': 'dest',
89+
'auth': {
90+
'callback_url': '',
91+
},
92+
'settings': {},
93+
'credentials': {},
94+
}
95+
}
96+
97+
98+
@pytest.fixture
99+
def bundles(src_bundle, dest_bundle):
100+
return src_bundle, dest_bundle

0 commit comments

Comments
 (0)