8

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?

tehhowch
  • 9,645
  • 4
  • 24
  • 42
Rodrigo
  • 135
  • 4
  • 45
  • 107

1 Answers1

2

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.

Andi
  • 8,154
  • 3
  • 30
  • 34