-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1118 from ethho/dev-tests-plat-165
PLAT-165: Migrate test_s3.py
- Loading branch information
Showing
4 changed files
with
197 additions
and
17 deletions.
There are no files selected for viewing
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
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,89 @@ | ||
""" | ||
A schema for testing external attributes | ||
""" | ||
|
||
import tempfile | ||
import inspect | ||
import datajoint as dj | ||
from . import PREFIX, CONN_INFO, S3_CONN_INFO | ||
import numpy as np | ||
|
||
|
||
class Simple(dj.Manual): | ||
definition = """ | ||
simple : int | ||
--- | ||
item : blob@local | ||
""" | ||
|
||
|
||
class SimpleRemote(dj.Manual): | ||
definition = """ | ||
simple : int | ||
--- | ||
item : blob@share | ||
""" | ||
|
||
|
||
class Seed(dj.Lookup): | ||
definition = """ | ||
seed : int | ||
""" | ||
contents = zip(range(4)) | ||
|
||
|
||
class Dimension(dj.Lookup): | ||
definition = """ | ||
dim : int | ||
--- | ||
dimensions : blob | ||
""" | ||
contents = ([0, [100, 50]], [1, [3, 4, 8, 6]]) | ||
|
||
|
||
class Image(dj.Computed): | ||
definition = """ | ||
# table for storing | ||
-> Seed | ||
-> Dimension | ||
---- | ||
img : blob@share # objects are stored as specified by dj.config['stores']['share'] | ||
neg : blob@local # objects are stored as specified by dj.config['stores']['local'] | ||
""" | ||
|
||
def make(self, key): | ||
np.random.seed(key["seed"]) | ||
img = np.random.rand(*(Dimension() & key).fetch1("dimensions")) | ||
self.insert1(dict(key, img=img, neg=-img.astype(np.float32))) | ||
|
||
|
||
class Attach(dj.Manual): | ||
definition = """ | ||
# table for storing attachments | ||
attach : int | ||
---- | ||
img : attach@share # attachments are stored as specified by: dj.config['stores']['raw'] | ||
txt : attach # attachments are stored directly in the database | ||
""" | ||
|
||
|
||
class Filepath(dj.Manual): | ||
definition = """ | ||
# table for file management | ||
fnum : int # test comment containing : | ||
--- | ||
img : filepath@repo # managed files | ||
""" | ||
|
||
|
||
class FilepathS3(dj.Manual): | ||
definition = """ | ||
# table for file management | ||
fnum : int | ||
--- | ||
img : filepath@repo-s3 # managed files | ||
""" | ||
|
||
|
||
LOCALS_EXTERNAL = {k: v for k, v in locals().items() if inspect.isclass(v)} | ||
__all__ = list(LOCALS_EXTERNAL) |
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
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,50 @@ | ||
import pytest | ||
import urllib3 | ||
import certifi | ||
from .schema_external import SimpleRemote | ||
from datajoint.errors import DataJointError | ||
from datajoint.hash import uuid_from_buffer | ||
from datajoint.blob import pack | ||
from . import S3_CONN_INFO | ||
from minio import Minio | ||
|
||
|
||
class TestS3: | ||
def test_connection(self, http_client, minio_client): | ||
assert minio_client.bucket_exists(S3_CONN_INFO["bucket"]) | ||
|
||
def test_connection_secure(self, minio_client): | ||
assert minio_client.bucket_exists(S3_CONN_INFO["bucket"]) | ||
|
||
def test_remove_object_exception(self, schema_ext): | ||
# https://github.com/datajoint/datajoint-python/issues/952 | ||
|
||
# Insert some test data and remove it so that the external table is populated | ||
test = [1, [1, 2, 3]] | ||
SimpleRemote.insert1(test) | ||
SimpleRemote.delete() | ||
|
||
# Save the old external table minio client | ||
old_client = schema_ext.external["share"].s3.client | ||
|
||
# Apply our new minio client which has a user that does not exist | ||
schema_ext.external["share"].s3.client = Minio( | ||
S3_CONN_INFO["endpoint"], | ||
access_key="jeffjeff", | ||
secret_key="jeffjeff", | ||
secure=False, | ||
) | ||
|
||
# This method returns a list of errors | ||
error_list = schema_ext.external["share"].delete( | ||
delete_external_files=True, errors_as_string=False | ||
) | ||
|
||
# Teardown | ||
schema_ext.external["share"].s3.client = old_client | ||
schema_ext.external["share"].delete(delete_external_files=True) | ||
|
||
with pytest.raises(DataJointError): | ||
# Raise the error we want if the error matches the expected uuid | ||
if str(error_list[0][0]) == str(uuid_from_buffer(pack(test[1]))): | ||
raise error_list[0][2] |