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.

Install

For package users:

pip install cloud-storage-mocker

For package developers:

git clone git@github.com:odashi/cloud-storage-mocker
cd cloud-storage-mocker
python -m venv venv
source venv/bin/activate
pip install -e '.[dev]'

How the package works

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 represent mappings between bucket names and directories on the local filesystem. Each Mount has Boolean configs readable and writable to control read/write permissions to the mounted buckets.

A canonical use-case of this package is writing unit tests to check the behavior of the code that works with Google Cloud Storage:

import tempfile

import google.cloud.storage  # type: ignore[import]

from cloud_storage_mocker import Mount
from cloud_storage_mocker import patch as gcs_patch


def test_something() -> None:
    # Creates two temporary directories, and mount them as readable/writable buckets.
    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 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 listed below have 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 basically provided for writing unit tests. 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.1a5.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

cloud_storage_mocker-0.0.1a5-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for cloud_storage_mocker-0.0.1a5.tar.gz
Algorithm Hash digest
SHA256 a2eb1e26eae93ce267a065e6cf67333cee3e81bb9c7b6e75653702ecdb8a20dc
MD5 50f64063b7a54631e6e30ed662727116
BLAKE2b-256 13d7412d7371007bc3ba4faa25f0931394fb30757d710012253e7c20a2688a46

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cloud_storage_mocker-0.0.1a5-py3-none-any.whl
Algorithm Hash digest
SHA256 97a719aa0e8eb5753c648669362d134ae4d491e6383aeb1547b53abaccef7e13
MD5 fcb30a33c1bdf9acb20f8ccb36eb6569
BLAKE2b-256 9a7a89073f2d9892e73182de176ddf590d99cbeb56b5076a0b5a3e82230b426b

See more details on using hashes here.

Supported by

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