Reputation: 4681
I have this class method
def _copy_blob(self, source_blob: str, target_file_path: str) -> None:
"""
Copy blob to a new location
:param source_blob:
:param target_file_path:
"""
copied_blob = self.blob_service_client.get_blob_client(self.container, target_file_path)
copied_blob.start_copy_from_url(source_blob)
I wrote unit test for the first line:
def test_copy_blob(mocker, file, source_blob, target_file_path):
blob_service_client_mock = mocker.MagicMock()
blob_service_client_mock.account_name = 'test_account'
file = TestClass(file, blob_service_client_mock, container='test')
file._copy_blob(source_blob, target_file_path)
blob_service_client_mock.get_blob_client.assert_any_call('test', target_file_path)
How can I test
copied_blob.start_copy_from_url(source_blob)
has been called?
Upvotes: 2
Views: 672
Reputation: 909
You will do it by patching the get_blob_client
method.
Here we miss a little context, but let's imagine the following augmentation of your code:
from blob_service_client import BlobServiceClient
class BlobBlop:
def __init__():
self.blob_service_client = BlobServiceClient()
def _copy_blob(self, source_blob: str, target_file_path: str) -> None:
"""
Copy blob to a new location
:param source_blob:
:param target_file_path:
"""
copied_blob = self.blob_service_client.get_blob_client(self.container, target_file_path)
copied_blob.start_copy_from_url(source_blob)
The test would be:
import ...
@patch("yourpath.yourmodule.BlobServiceClient")
def test__copy_blob(patched_blob_service_client):
mocked_blob_service_client = Mock()
patched_blob_service_client.return_value = mocked_blob_service_client
mocked_blob_client = Mock()
mocked_blob_service_client.get_blob_client.return_value = mocked_blob_client
test_object = BlobBlop()
test_object._copy_blob("the_source_blob", "the_target_path")
mocked_blob_client.start_copy_from_url.assert_called_with("the_source_blob")
Upvotes: 1
Reputation: 66551
blob_service_client_mock.get_blob_client.return_value.start_copy_from_url.assert_called()
blob_service_client_mock.get_blob_client.return_value
is what will be returned by blob_service_client_mock.get_blob_client()
, so copied_blob
in your case (docs: return_value
). The rest is just the assertion.
Upvotes: 0