Mock the result of accessing public GCS bucket
Asked Answered
F

1

9

I have the following code:

bucket = get_bucket('bucket-name')
blob = bucket.blob(os.path.join(*pieces))
blob.upload_from_string('test')
blob.make_public()
result = blob.public_url
# result is `<Mock name='mock().get_bucket().blob().public_url`

And I would do like to mock the result of public_url, my unit test code is something like this

with ExitStack() as st:
    from google.cloud import storage
    blob_mock = mock.Mock(spec=storage.Blob)
    blob_mock.public_url.return_value = 'http://'

    bucket_mock = mock.Mock(spec=storage.Bucket)
    bucket_mock.blob.return_value = blob_mock

    storage_client_mock = mock.Mock(spec=storage.Client)
    storage_client_mock.get_bucket.return_value = bucket_mock

    st.enter_context(
        mock.patch('google.cloud.storage.Client', storage_client_mock))
    my_function()

Is there something like FakeRedis or moto for Google Storage, so I can mock google.cloud.storage.Blob.public_url?

Fermin answered 17/8, 2018 at 16:31 Comment(0)
H
3

I found this fake gcs server written in Go which can be run within a Docker container and consumed by the Python library. See Python examples.

Heresy answered 26/8, 2020 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.