1

I'm having a trouble while mocking Google Cloud Storage Client in Python.

I've tried a couple solutions, but none of them seems to be working in my case. This is what I've tried: Unit testing Mock GCS Python unittest.mock google storage - how to achieve exceptions.NotFound as side effect How to Mock a Google API Library with Python 3.7 for Unit Testing So basically I have a class for downloading the decoded GCS object. This is the class that I want to test:

class Extract:

    def __init__(self, storage_client: storage.Client) -> None:
        self.client = storage_client

    @retry.Retry(initial=3.0, maximum=60.0, deadline=240.0)
    def _get_storage_object(self, bucket: str, blob: str) -> bytes:
        bucket_obj = self.client.get_bucket(bucket)
        blob_obj = bucket_obj.blob(blob)
        data = blob_obj.download_as_bytes(timeout=60)
        return data

    def get_decoded_blob(self, bucket: str, blob: str) -> Union[dict, list]:
        raw_blob = self._get_storage_object(bucket=bucket, blob=blob)
        decoded_blob = decode_data(raw_blob)
        return decoded_blob

And this last version of code for unit testing of the class.

def decode_data(data: bytes) -> List[Dict[str, Any]]:
    decoded_blob = data.decode('utf-8')
    decoded_data = ast.literal_eval(decoded_blob)
    return decoded_data

class TestExtract:

    @pytest.fixture()
    @mock.patch('src.pipeline.extract.storage', spec=True)
    def storage_client(self, mock_storage):
        mock_storage_client = mock_storage.Client.return_value
        mock_bucket = mock.Mock()
        mock_bucket.blob.return_value.download_as_bytes.return_value = 'data'.encode('utf-8')
        mock_storage_client.bucket.return_value = mock_bucket
        return mock_storage_client

    
    def test_storage_client(self, storage_client):
        gcs = extract.Extract(storage_client)
        output = gcs._get_storage_object('bucket', 'blob')
        assert output == b'data'

The error for the above code is that mock does not return return_value which was assigned in the fixture. Instead, it returns the MagicMock object as below: AssertionError: assert <MagicMock na...806039010368'> == b'data'

Anyone can help me in this case?

danevaughn
  • 11
  • 2

0 Answers0