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),
],
):
# Reads a blob.
blob = google.cloud.storage.Client().bucket("readable").blob("hello.txt")
assert blob.download_as_text() == "Hello."
# Writes a blob.
blob = google.cloud.storage.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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for cloud_storage_mocker-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9294d597bdc53fc5f4a044475204345a99679b80579236961b140db04bfd8a1b |
|
MD5 | 98459e159761df7646150808216765bc |
|
BLAKE2b-256 | beb3a1079a9b083610a33f3555ec987021dd6d63f052dd41e3d18076bf63a4b4 |
Hashes for cloud_storage_mocker-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ef2a5a5a642e558daa1fdc4ce7758b5cfc418562bb8da15409a5d1375677c4a |
|
MD5 | e74be83cab90662aa7eeeadac2122305 |
|
BLAKE2b-256 | 77bffc891c3c56baa81f73777ad5b6532235f58041b8fad8d6bf1f522c70e8a1 |