-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jamiedemaria
committed
Mar 18, 2022
1 parent
b0a885f
commit 8063405
Showing
5 changed files
with
259 additions
and
3 deletions.
There are no files selected for viewing
7 changes: 6 additions & 1 deletion
7
python_modules/libraries/dagster-gcp/dagster_gcp/gcs/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
from .compute_log_manager import GCSComputeLogManager | ||
from .file_manager import GCSFileHandle | ||
from .io_manager import PickledObjectGCSIOManager, gcs_pickle_io_manager | ||
from .gcs_fake_resource import FakeGCSBlob, FakeGCSBucket, FakeGCSClient | ||
from .io_manager import ( | ||
PickledObjectGCSIOManager, | ||
gcs_pickle_asset_io_manager, | ||
gcs_pickle_io_manager, | ||
) | ||
from .resources import gcs_file_manager, gcs_resource |
92 changes: 92 additions & 0 deletions
92
python_modules/libraries/dagster-gcp/dagster_gcp/gcs/gcs_fake_resource.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from typing import Dict, Optional, Union | ||
|
||
|
||
class FakeGCSBlob: | ||
def __init__(self, name: str, bucket: "FakeGCSBucket"): | ||
from unittest import mock | ||
|
||
self.name = name | ||
self.data = b"" | ||
self.bucket = bucket | ||
self.mock_extras = mock.MagicMock() | ||
|
||
def exists(self, *args, **kwargs): | ||
self.mock_extras.exists(*args, **kwargs) | ||
return True | ||
|
||
def delete(self, *args, **kwargs): | ||
self.mock_extras.delete(*args, **kwargs) | ||
del self.bucket.blobs[self.name] | ||
|
||
def download_as_bytes(self, *args, **kwargs): | ||
self.mock_extras.download_as_bytes(*args, **kwargs) | ||
return self.data | ||
|
||
def upload_from_string(self, data: Union[bytes, str], *args, **kwargs): | ||
self.mock_extras.upload_from_string(*args, **kwargs) | ||
if isinstance(data, str): | ||
self.data = data.encode() | ||
else: | ||
self.data = data | ||
|
||
|
||
class FakeGCSBucket: | ||
def __init__(self, name: str): | ||
from unittest import mock | ||
|
||
self.name = name | ||
self.blobs: Dict[str, FakeGCSBlob] = {} | ||
self.mock_extras = mock.MagicMock() | ||
|
||
def blob(self, blob_name: str, *args, **kwargs): | ||
self.mock_extras.blob(*args, **kwargs) | ||
|
||
if blob_name not in self.blobs.keys(): | ||
self.blobs[blob_name] = FakeGCSBlob(name=blob_name, bucket=self) | ||
|
||
return self.blobs[blob_name] | ||
|
||
def exists(self, *args, **kwargs): | ||
self.mock_extras.exists(*args, **kwargs) | ||
return True | ||
|
||
|
||
class FakeGCSClient: | ||
def __init__(self): | ||
from unittest import mock | ||
|
||
self.buckets: Dict[str, FakeGCSBucket] = {} | ||
self.mock_extras = mock.MagicMock() | ||
|
||
def bucket(self, bucket_name: str, *args, **kwargs): | ||
self.mock_extras.bucket(*args, **kwargs) | ||
|
||
if bucket_name not in self.buckets.keys(): | ||
self.buckets[bucket_name] = FakeGCSBucket(name=bucket_name) | ||
|
||
return self.buckets[bucket_name] | ||
|
||
def list_buckets(self, *args, **kwargs): | ||
self.mock_extras.list_buckets(*args, **kwargs) | ||
for bucket in self.buckets.values(): | ||
yield bucket | ||
|
||
def list_blobs( | ||
self, | ||
bucket_or_name: Union[FakeGCSBucket, str], | ||
*args, | ||
prefix: Optional[str] = None, | ||
**kwargs, | ||
): | ||
self.mock_extras.list_blobs(*args, **kwargs) | ||
|
||
if isinstance(bucket_or_name, str): | ||
bucket = self.bucket(bucket_or_name) | ||
else: | ||
bucket = bucket_or_name | ||
|
||
for blob in self.buckets[bucket.name].blobs.values(): | ||
if prefix is None: | ||
yield blob | ||
elif prefix in blob.name: | ||
yield blob |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
python_modules/libraries/dagster-gcp/dagster_gcp_tests/gcs_tests/test_fake_gcs_resource.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from dagster_gcp.gcs import FakeGCSBlob, FakeGCSBucket, FakeGCSClient | ||
|
||
|
||
def test_fake_blob_read_write(): | ||
bucket = FakeGCSBucket("my_bucket") | ||
blob = FakeGCSBlob("my_blob", bucket) | ||
|
||
assert blob.exists() | ||
|
||
my_string = "this is a unit test" | ||
blob.upload_from_string(my_string) | ||
assert blob.download_as_bytes() == my_string.encode() | ||
|
||
my_bytes = b"these are some bytes" | ||
blob.upload_from_string(my_bytes) | ||
assert blob.download_as_bytes() == my_bytes | ||
|
||
|
||
def test_blob_delete(): | ||
bucket = FakeGCSBucket("my_bucket") | ||
foo = bucket.blob("foo") | ||
bar = bucket.blob("bar") | ||
|
||
foo.upload_from_string("foo") | ||
bar.upload_from_string("bar") | ||
|
||
assert "foo" in bucket.blobs.keys() | ||
assert "bar" in bucket.blobs.keys() | ||
|
||
foo.delete() | ||
|
||
assert "foo" not in bucket.blobs.keys() | ||
assert "bar" in bucket.blobs.keys() | ||
|
||
bar.delete() | ||
|
||
assert "bar" not in bucket.blobs.keys() | ||
|
||
|
||
def test_bucket(): | ||
bucket = FakeGCSBucket("my_bucket") | ||
|
||
assert bucket.exists() | ||
|
||
foo = bucket.blob("foo") | ||
bar = bucket.blob("bar") | ||
|
||
assert bucket.blob("foo") == foo | ||
assert bucket.blob("bar") == bar | ||
|
||
|
||
def test_client_blobs(): | ||
client = FakeGCSClient() | ||
|
||
foo = client.bucket("foo") | ||
assert client.bucket("foo") == foo | ||
|
||
bar = foo.blob("bar") | ||
assert [bar] == list(client.list_blobs("foo")) | ||
|
||
baz = foo.blob("baz/aaa") | ||
assert [bar, baz] == list(client.list_blobs("foo")) | ||
|
||
assert [baz] == list(client.list_blobs("foo", prefix="baz")) | ||
assert [] == list(client.list_blobs("foo", prefix="xyz")) | ||
|
||
|
||
def test_client_bucekts(): | ||
client = FakeGCSClient() | ||
|
||
foo = client.bucket("foo") | ||
bar = client.bucket("bar") | ||
|
||
assert [foo, bar] == list(client.list_buckets()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters