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 pathlib

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(tmp_path: pathlib.Path) -> None:
    # Creates two temporary directories for readable/writable buckets.
    src_dir = tmp_path / "src"
    dest_dir = tmp_path / "dest"
    src_dir.mkdir()
    dest_dir.mkdir()

    # A sample file on the readable bucket.
    (src_dir / "hello.txt").write_text("Hello.")

    # Mounts directories.
    with gcs_patch(
        [
            Mount("readable", src_dir, readable=True),
            Mount("writable", dest_dir, writable=True),
        ],
    ):
        client = google.cloud.storage.Client()

        # Reads a blob.
        blob = client.bucket("readable").blob("hello.txt")
        assert blob.download_as_text() == "Hello."

        # Writes a blob.
        blob = client.bucket("writable").blob("world.txt")
        blob.upload_from_string("World.")

    # Checks if the file is written appropriately.
    assert (dest_dir / "world.txt").read_text() == "World."

Patched methods

Methods listed below have specialized behavior to mount the local filesystem.

Other methods are mapped to MagicMock.

  • Client()
  • Client.bucket()
  • Bucket()
  • Bucket.blob()
  • Blob()
  • Blob.download_to_file()
  • Blob.download_to_filename()
  • Blob.download_as_bytes()
  • Blob.download_as_text()
  • Blob.upload_from_file()
  • Blob.upload_from_filename()
  • 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.2.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

cloud_storage_mocker-0.2.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file cloud_storage_mocker-0.2.0.tar.gz.

File metadata

  • Download URL: cloud_storage_mocker-0.2.0.tar.gz
  • Upload date:
  • Size: 6.1 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.2.0.tar.gz
Algorithm Hash digest
SHA256 2e28849fae696b4fcdc56b79c668505ca45489af9a12fd3c431edfc2164942b6
MD5 ae43f24eab4fff221272ba1d8fe9214d
BLAKE2b-256 14dd2be84a77ed2e8e820f2b285ff99280b495936520d4b06acd366cbeee7a0b

See more details on using hashes here.

File details

Details for the file cloud_storage_mocker-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for cloud_storage_mocker-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9e63fef1d3f531706403549a95e44fa8f670cd4e9a812125b24de11d4197ebd4
MD5 0d2b8fea07c936f233771ae58816a02d
BLAKE2b-256 a4671bb3eee48a94e93389af9a5351a838b2378fc25d4cce7740c5cdd7747c9e

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