Skip to main content

Mocker library of Google Cloud Storage with local filesystem mounting.

Project description

cloud-storage-mocker

Mocker library of Google Cloud Storage with local filesystem mounting.

Getting started

This library provides patch context manager, which replaces following classes on the google-cloud-storage package:

  • Client
  • Bucket
  • Blob

patch takes a list of Mount objects, which represents a mapping between a bucket name and a local storage. Each mount has Boolean configs readable and writable to control read/write access to the mounted bucket.

A canonical way to use this library is writing unit tests that are working with Google Cloud Storage:

import tempfile

import google.cloud.storage

from cloud_storage_mocker import patch as gcs_patch, Mount

# A pytest test case.
def test_something() -> None:
    # Creates a temporary directory
    # And mounts it to the Cloud Storage bucket "my_bucket"
    with (
        tempfile.TemporaryDirectory() as readable_dir,
        tempfile.TemporaryDirectory() as writable_dir,
        gcs_patch([
            Mount("readable_bucket", readable_dir, readable=True),
            Mount("writable_bucket", writable_dir, writable=True),
        ]),
    ):
        # Reads a blob.
        with open(readable_dir + "/test.txt", "w") as fp:
            fp.write("Hello.")
        blob = google.cloud.storage.Client().bucket("readable_bucket").blob("test.txt")
        assert readable_blob.download_as_text() == "Hello."

        # Writes a blob.
        blob = google.cloud.storage.Client().bucket("writable_bucket").blob("test.txt")
        blob.upload_from_string("World.")
        with open(writable_dir + "/test.txt") as fp:
            assert fp.read() == "World."

Patched methods

Methods below has a specialized behavior to mount the local filesystem.

Other methods are mapped to MagicMock.

  • Client()
  • Client.bucket()
  • Bucket.blob()
  • Blob.download_as_text()
  • Blob.upload_from_string()

Caution

This library is provided for writing unit tests that work with the Google Cloud Storage client library. DO NOT use this library on any code on the production.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cloud_storage_mocker-0.0.1a4.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cloud_storage_mocker-0.0.1a4-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file cloud_storage_mocker-0.0.1a4.tar.gz.

File metadata

  • Download URL: cloud_storage_mocker-0.0.1a4.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for cloud_storage_mocker-0.0.1a4.tar.gz
Algorithm Hash digest
SHA256 552b3f62ff01fa6592376edb71a107ad34c41ba24adfe6cccc36ed4b96047964
MD5 388a2585ea0d318075049b15c866c328
BLAKE2b-256 103f96d49d8d46c4f98afcabe35d59b8bac283f6b8ffc8e6f74c40de9c943300

See more details on using hashes here.

File details

Details for the file cloud_storage_mocker-0.0.1a4-py3-none-any.whl.

File metadata

File hashes

Hashes for cloud_storage_mocker-0.0.1a4-py3-none-any.whl
Algorithm Hash digest
SHA256 df40f6491a0e309d645e9176db5ec1bf25a1ed9e06a100ba44afed3b51bb3f47
MD5 c0b3144639a9ade223147104143e7ae7
BLAKE2b-256 12e3c5f976b7fc17286110212fabb01f5dfd6c142531dbb0130f19aed798b4aa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page