Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cbbb044177 | ||
|
|
c630498168 | ||
|
|
f9d23c5bd4 | ||
|
|
3fa79faa9e | ||
|
|
556e26f21d | ||
|
|
01ee4664ff |
@@ -58,4 +58,4 @@ repos:
|
|||||||
rev: 'v2.6'
|
rev: 'v2.6'
|
||||||
hooks:
|
hooks:
|
||||||
- id: vulture
|
- id: vulture
|
||||||
entry: vulture . --min-confidence 90 --exclude */.venv/*.py
|
entry: vulture . --min-confidence 90 --exclude */.venv/*.py,*/tests/*.py
|
||||||
|
|||||||
@@ -2,13 +2,14 @@
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from io import BytesIO
|
|
||||||
|
|
||||||
from minio import Minio
|
from minio import Minio
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from shared.utils import check_env
|
from shared.utils import check_env
|
||||||
|
|
||||||
|
from .get import get
|
||||||
|
|
||||||
|
|
||||||
def get_image(
|
def get_image(
|
||||||
client: Minio,
|
client: Minio,
|
||||||
@@ -23,19 +24,11 @@ def get_image(
|
|||||||
bucket_name = str(os.getenv('MINIO_BUCKET_NAME'))
|
bucket_name = str(os.getenv('MINIO_BUCKET_NAME'))
|
||||||
object_name = f'images/{object_name}'
|
object_name = f'images/{object_name}'
|
||||||
# get object from bucket
|
# get object from bucket
|
||||||
try:
|
buffer = get(
|
||||||
response = client.get_object(
|
client=client,
|
||||||
bucket_name=bucket_name,
|
bucket_name=bucket_name,
|
||||||
object_name=object_name,
|
object_name=object_name,
|
||||||
)
|
)
|
||||||
buffer = BytesIO(response.data)
|
|
||||||
except Exception as exc:
|
|
||||||
logging.error('failed getting data from MinIO')
|
|
||||||
raise exc
|
|
||||||
finally:
|
|
||||||
response.close()
|
|
||||||
response.release_conn()
|
|
||||||
buffer.seek(0)
|
|
||||||
# convert data to image
|
# convert data to image
|
||||||
image = Image.open(buffer)
|
image = Image.open(buffer)
|
||||||
logging.debug('got data from %s', object_name)
|
logging.debug('got data from %s', object_name)
|
||||||
|
|||||||
@@ -3,13 +3,14 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
from io import BytesIO
|
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
from minio import Minio
|
from minio import Minio
|
||||||
|
|
||||||
from shared.utils import check_env
|
from shared.utils import check_env
|
||||||
|
|
||||||
|
from .get import get
|
||||||
|
|
||||||
|
|
||||||
def get_model(
|
def get_model(
|
||||||
client: Minio,
|
client: Minio,
|
||||||
@@ -20,24 +21,16 @@ def get_model(
|
|||||||
assert isinstance(object_name, str)
|
assert isinstance(object_name, str)
|
||||||
assert len(object_name) > 0
|
assert len(object_name) > 0
|
||||||
# prepare arguments
|
# prepare arguments
|
||||||
check_env({'MINIO_BUCKET_NAME_MODELS'})
|
check_env({'MINIO_BUCKET_NAME'})
|
||||||
bucket_name = str(os.getenv('MINIO_BUCKET_NAME_MODELS'))
|
bucket_name = str(os.getenv('MINIO_BUCKET_NAME'))
|
||||||
object_name = f'models/{object_name}'
|
object_name = f'models/{object_name}'
|
||||||
# get object from bucket
|
# get object from bucket
|
||||||
try:
|
buffer = get(
|
||||||
response = client.get_object(
|
client=client,
|
||||||
bucket_name=bucket_name,
|
bucket_name=bucket_name,
|
||||||
object_name=object_name,
|
object_name=object_name,
|
||||||
)
|
)
|
||||||
buffer = BytesIO(response.data)
|
|
||||||
except Exception as exc:
|
|
||||||
logging.error('failed getting data from MinIO')
|
|
||||||
raise exc
|
|
||||||
finally:
|
|
||||||
response.close()
|
|
||||||
response.release_conn()
|
|
||||||
# convert data to model checkpoint
|
# convert data to model checkpoint
|
||||||
buffer.seek(0)
|
|
||||||
model_content = torch.load(buffer)
|
model_content = torch.load(buffer)
|
||||||
logging.debug('finished')
|
logging.debug('finished')
|
||||||
return model_content
|
return model_content
|
||||||
|
|||||||
@@ -22,14 +22,13 @@ def put_image(
|
|||||||
# get bucket name from env
|
# get bucket name from env
|
||||||
bucket_name = os.getenv('MINIO_BUCKET_NAME', default='')
|
bucket_name = os.getenv('MINIO_BUCKET_NAME', default='')
|
||||||
assert len(bucket_name) > 0
|
assert len(bucket_name) > 0
|
||||||
# save image to buffer
|
# save data to buffer
|
||||||
buffer = BytesIO()
|
buffer = BytesIO()
|
||||||
image.save(buffer, 'png')
|
image.save(buffer, 'png')
|
||||||
# get md5 of buffer
|
# get md5 of buffer
|
||||||
checksum = md5(buffer.getbuffer()).hexdigest()
|
checksum = md5(buffer.getbuffer()).hexdigest()
|
||||||
# determine object name
|
# set object name
|
||||||
subfolder = 'images'
|
object_name = f'images/{checksum}'
|
||||||
object_name = f'{subfolder}/{checksum}'
|
|
||||||
# send data to bucket
|
# send data to bucket
|
||||||
put(
|
put(
|
||||||
client=client,
|
client=client,
|
||||||
|
|||||||
@@ -9,6 +9,10 @@ import torch
|
|||||||
from minio import Minio
|
from minio import Minio
|
||||||
from torch.nn import Module
|
from torch.nn import Module
|
||||||
|
|
||||||
|
from shared.utils import check_env
|
||||||
|
|
||||||
|
from .put import put
|
||||||
|
|
||||||
|
|
||||||
def put_model(
|
def put_model(
|
||||||
client: Minio,
|
client: Minio,
|
||||||
@@ -18,28 +22,22 @@ def put_model(
|
|||||||
used as object name."""
|
used as object name."""
|
||||||
assert isinstance(client, Minio)
|
assert isinstance(client, Minio)
|
||||||
assert isinstance(model, Module)
|
assert isinstance(model, Module)
|
||||||
bucket_name = os.getenv('MINIO_BUCKET_NAME', default='')
|
# get bucket name from env
|
||||||
assert len(bucket_name) > 0
|
check_env({'MINIO_BUCKET_NAME'})
|
||||||
subfolder = 'models'
|
bucket_name = str(os.getenv('MINIO_BUCKET_NAME'))
|
||||||
# save image to buffer
|
# save data to buffer
|
||||||
buffer = BytesIO()
|
buffer = BytesIO()
|
||||||
torch.save(model.state_dict(), buffer)
|
torch.save(model.state_dict(), buffer)
|
||||||
# get md5 of image
|
# get md5 of image
|
||||||
checksum = md5(buffer.getbuffer()).hexdigest()
|
checksum = md5(buffer.getbuffer()).hexdigest()
|
||||||
# prepare for saving
|
# set object name
|
||||||
num_bytes = buffer.tell()
|
object_name = f'models/{checksum}'
|
||||||
buffer.seek(0)
|
|
||||||
# send data to bucket
|
# send data to bucket
|
||||||
object_name = f'{subfolder}/{checksum}'
|
put(
|
||||||
try:
|
client=client,
|
||||||
client.put_object(
|
buffer=buffer,
|
||||||
bucket_name=bucket_name,
|
bucket_name=bucket_name,
|
||||||
object_name=object_name,
|
object_name=object_name,
|
||||||
length=num_bytes,
|
|
||||||
data=buffer,
|
|
||||||
)
|
)
|
||||||
except Exception as exc:
|
logging.debug('finished')
|
||||||
logging.error('failed saving data to MinIO')
|
|
||||||
raise exc
|
|
||||||
logging.debug('saved data to %s', object_name)
|
|
||||||
return checksum
|
return checksum
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
"""Integration tests related to base CRUD functions."""
|
||||||
|
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from shared.datastore import connect_minio, delete, get, put
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_get_data(
|
||||||
|
data_in_minio,
|
||||||
|
):
|
||||||
|
data, bucket_name, object_name = data_in_minio
|
||||||
|
client = connect_minio()
|
||||||
|
received_data = get(
|
||||||
|
client=client,
|
||||||
|
bucket_name=bucket_name,
|
||||||
|
object_name=object_name,
|
||||||
|
)
|
||||||
|
assert isinstance(data, BytesIO)
|
||||||
|
assert received_data == data
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_delete_data(
|
||||||
|
data_in_minio,
|
||||||
|
):
|
||||||
|
_, bucket_name, object_name = data_in_minio
|
||||||
|
client = connect_minio()
|
||||||
|
delete(
|
||||||
|
client=client,
|
||||||
|
bucket_name=bucket_name,
|
||||||
|
object_name=object_name,
|
||||||
|
)
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
_ = get(
|
||||||
|
client=client,
|
||||||
|
bucket_name=bucket_name,
|
||||||
|
object_name=object_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_put_data(
|
||||||
|
data,
|
||||||
|
):
|
||||||
|
buffer, bucket_name, object_name = data
|
||||||
|
client = connect_minio()
|
||||||
|
put(
|
||||||
|
client=client,
|
||||||
|
buffer=buffer,
|
||||||
|
bucket_name=bucket_name,
|
||||||
|
object_name=object_name,
|
||||||
|
)
|
||||||
|
received_data = get(
|
||||||
|
client=client,
|
||||||
|
bucket_name=bucket_name,
|
||||||
|
object_name=object_name,
|
||||||
|
)
|
||||||
|
assert received_data == data
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_update_data(
|
||||||
|
data_in_minio,
|
||||||
|
):
|
||||||
|
buffer, bucket_name, object_name = data_in_minio
|
||||||
|
client = connect_minio()
|
||||||
|
put(
|
||||||
|
client=client,
|
||||||
|
buffer=buffer,
|
||||||
|
bucket_name=bucket_name,
|
||||||
|
object_name=object_name,
|
||||||
|
)
|
||||||
|
received_data = get(
|
||||||
|
client=client,
|
||||||
|
bucket_name=bucket_name,
|
||||||
|
object_name=object_name,
|
||||||
|
)
|
||||||
|
assert received_data == buffer
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
"""Integration test configurations."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from PIL import Image
|
||||||
|
from testcontainers.minio import MinioContainer
|
||||||
|
|
||||||
|
env_var_map = {
|
||||||
|
'MINIO_ENDPOINT': 'localhost:9000',
|
||||||
|
'MINIO_ACCESS_KEY': 'test-access-key',
|
||||||
|
'MINIO_SECRET_KEY': 'test-secret-key',
|
||||||
|
'MINIO_BUCKET_NAME': 'test-bucket',
|
||||||
|
}
|
||||||
|
container_map = {
|
||||||
|
'minio': MinioContainer(
|
||||||
|
port=env_var_map['MINIO_ENDPOINT'].rsplit(':', maxsplit=1)[-1],
|
||||||
|
access_key=env_var_map['MINIO_ACCESS_KEY'],
|
||||||
|
secret_key=env_var_map['MINIO_SECRET_KEY'],
|
||||||
|
),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='session', autouse=True)
|
||||||
|
def setup_infrastructure(request: pytest.FixtureRequest) -> None:
|
||||||
|
"""Prepare infrastructure for integration test."""
|
||||||
|
# prepare infrastructure
|
||||||
|
for container in container_map.values():
|
||||||
|
container.start()
|
||||||
|
# update env var map
|
||||||
|
minio_host = container_map['minio'].get_container_host_ip()
|
||||||
|
minio_port = container_map['minio'].get_exposed_port(9000)
|
||||||
|
env_var_map['MINIO_ENDPOINT'] = f'{minio_host}:{minio_port}'
|
||||||
|
|
||||||
|
# ensure cleanup
|
||||||
|
def cleanup_infrastructure():
|
||||||
|
for container in container_map.values():
|
||||||
|
container.stop()
|
||||||
|
|
||||||
|
request.addfinalizer(cleanup_infrastructure)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='session', autouse=True)
|
||||||
|
def populate_env(
|
||||||
|
request: pytest.FixtureRequest,
|
||||||
|
setup_infrastructure,
|
||||||
|
) -> None:
|
||||||
|
"""Populate environment with variables used for testing."""
|
||||||
|
# update env
|
||||||
|
for key, val in env_var_map.items():
|
||||||
|
os.environ[key] = val
|
||||||
|
|
||||||
|
# ensure cleanup
|
||||||
|
def cleanup_env():
|
||||||
|
for key in env_var_map:
|
||||||
|
_ = os.environ.pop(key, default=None)
|
||||||
|
|
||||||
|
request.addfinalizer(cleanup_env)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def image() -> Image.Image:
|
||||||
|
# generate image
|
||||||
|
image = Image.new(mode='RGB', size=(480, 480))
|
||||||
|
# expose image
|
||||||
|
yield image
|
||||||
|
|
||||||
|
|
||||||
|
# @pytest.fixture
|
||||||
|
# def image_in_minio(
|
||||||
|
# image: Image.Image,
|
||||||
|
# ) -> tuple(Image.Image, str):
|
||||||
|
# #
|
||||||
|
|
||||||
|
|
||||||
|
# # expose image and object name
|
||||||
|
# yield image, object_name
|
||||||
|
# # cleanup
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
"""Integration test for connect_minio function."""
|
||||||
|
|
||||||
|
from minio import Minio
|
||||||
|
|
||||||
|
from shared.datastore import connect_minio
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_return_correct_type():
|
||||||
|
client = connect_minio()
|
||||||
|
assert isinstance(client, Minio)
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
"""Integration tests related to image CRUD."""
|
||||||
|
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
from shared.datastore import connect_minio, get_image, put_image
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_get_image(
|
||||||
|
image_in_minio,
|
||||||
|
):
|
||||||
|
image, object_name = image_in_minio
|
||||||
|
client = connect_minio()
|
||||||
|
received_image = get_image(
|
||||||
|
client=client,
|
||||||
|
object_name=object_name,
|
||||||
|
)
|
||||||
|
assert isinstance(image, Image.Image)
|
||||||
|
assert received_image == image
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_put_image(
|
||||||
|
image,
|
||||||
|
):
|
||||||
|
client = connect_minio()
|
||||||
|
object_name = put_image(
|
||||||
|
client=client,
|
||||||
|
image=image,
|
||||||
|
)
|
||||||
|
assert isinstance(object_name, str)
|
||||||
|
assert len(object_name) > 0
|
||||||
|
received_image = get_image(
|
||||||
|
client=client,
|
||||||
|
object_name=object_name,
|
||||||
|
)
|
||||||
|
assert received_image == image
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_update_image(
|
||||||
|
image_in_minio,
|
||||||
|
):
|
||||||
|
image, object_name = image_in_minio
|
||||||
|
client = connect_minio()
|
||||||
|
object_name = put_image(
|
||||||
|
client=client,
|
||||||
|
image=image,
|
||||||
|
)
|
||||||
|
assert isinstance(object_name, str)
|
||||||
|
assert len(object_name) > 0
|
||||||
|
received_image = get_image(
|
||||||
|
client=client,
|
||||||
|
object_name=object_name,
|
||||||
|
)
|
||||||
|
assert received_image == image
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
"""Integration tests related to model CRUD."""
|
||||||
|
|
||||||
|
from torch.nn import Module
|
||||||
|
|
||||||
|
from shared.datastore import connect_minio, get_model, put_model
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_get_model(
|
||||||
|
model_in_minio,
|
||||||
|
):
|
||||||
|
model, object_name = model_in_minio
|
||||||
|
client = connect_minio()
|
||||||
|
received_model = get_model(
|
||||||
|
client=client,
|
||||||
|
object_name=object_name,
|
||||||
|
)
|
||||||
|
assert isinstance(model, Module)
|
||||||
|
assert received_model == model
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_put_model(
|
||||||
|
model,
|
||||||
|
):
|
||||||
|
client = connect_minio()
|
||||||
|
object_name = put_model(
|
||||||
|
client=client,
|
||||||
|
model=model,
|
||||||
|
)
|
||||||
|
assert isinstance(object_name, str)
|
||||||
|
assert len(object_name) > 0
|
||||||
|
received_model = get_model(
|
||||||
|
client=client,
|
||||||
|
object_name=object_name,
|
||||||
|
)
|
||||||
|
assert received_model == model
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_update_model(
|
||||||
|
model_in_minio,
|
||||||
|
):
|
||||||
|
model, object_name = model_in_minio
|
||||||
|
client = connect_minio()
|
||||||
|
object_name = put_model(
|
||||||
|
client=client,
|
||||||
|
model=model,
|
||||||
|
)
|
||||||
|
assert isinstance(object_name, str)
|
||||||
|
assert len(object_name) > 0
|
||||||
|
received_model = get_model(
|
||||||
|
client=client,
|
||||||
|
object_name=object_name,
|
||||||
|
)
|
||||||
|
assert received_model == model
|
||||||
@@ -1,110 +0,0 @@
|
|||||||
"""Integration tests for functions exposed by the datastore module."""
|
|
||||||
|
|
||||||
import os
|
|
||||||
from io import BytesIO
|
|
||||||
from random import randbytes
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
from minio import Minio
|
|
||||||
from testcontainers.minio import MinioContainer
|
|
||||||
|
|
||||||
from shared.datastore import connect_minio, get, put
|
|
||||||
from shared.utils import setup_logging
|
|
||||||
|
|
||||||
MINIO_BUCKET_NAME = 'test-bucket'
|
|
||||||
minio = MinioContainer()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope='module', autouse=True)
|
|
||||||
def setup(request: pytest.FixtureRequest):
|
|
||||||
"""Setup function for datastore module integration tests."""
|
|
||||||
# start minio
|
|
||||||
minio.start()
|
|
||||||
|
|
||||||
# ensure minio is stopped after testing
|
|
||||||
def remove_container():
|
|
||||||
minio.stop()
|
|
||||||
|
|
||||||
request.addfinalizer(remove_container)
|
|
||||||
# extract information
|
|
||||||
minio_ip = minio.get_container_host_ip()
|
|
||||||
minio_port = minio.get_exposed_port(port=9000)
|
|
||||||
os.environ['MINIO_ENDPOINT'] = f'{minio_ip}:{minio_port}'
|
|
||||||
os.environ['MINIO_ACCESS_KEY'] = minio.access_key
|
|
||||||
os.environ['MINIO_SECRET_KEY'] = minio.secret_key
|
|
||||||
os.environ['MINIO_BUCKET_NAME'] = MINIO_BUCKET_NAME
|
|
||||||
|
|
||||||
|
|
||||||
def test_connect_minio():
|
|
||||||
"""Test function connect_minio."""
|
|
||||||
minio_client = connect_minio()
|
|
||||||
assert isinstance(minio_client, Minio)
|
|
||||||
|
|
||||||
|
|
||||||
def test_put():
|
|
||||||
"""Test function put."""
|
|
||||||
# connect to minio
|
|
||||||
minio_client = connect_minio()
|
|
||||||
# generate random bytes
|
|
||||||
buffer = BytesIO(randbytes(2**20))
|
|
||||||
# put data in Minio
|
|
||||||
put(
|
|
||||||
client=minio_client,
|
|
||||||
buffer=buffer,
|
|
||||||
bucket_name=MINIO_BUCKET_NAME,
|
|
||||||
object_name='test_put_data',
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def test_get():
|
|
||||||
"""Test function get."""
|
|
||||||
# connect to minio
|
|
||||||
minio_client = connect_minio()
|
|
||||||
# generate random bytes
|
|
||||||
buffer = BytesIO(randbytes(2**20))
|
|
||||||
# put data in minio
|
|
||||||
put(
|
|
||||||
client=minio_client,
|
|
||||||
buffer=buffer,
|
|
||||||
bucket_name=MINIO_BUCKET_NAME,
|
|
||||||
object_name='test_get_data',
|
|
||||||
)
|
|
||||||
# get data back from minio
|
|
||||||
buffer_rec = get(
|
|
||||||
client=minio_client,
|
|
||||||
bucket_name=MINIO_BUCKET_NAME,
|
|
||||||
object_name='test_get_data',
|
|
||||||
)
|
|
||||||
# check that data are the same
|
|
||||||
assert buffer.read() == buffer_rec.read()
|
|
||||||
|
|
||||||
|
|
||||||
def test_delete():
|
|
||||||
"""Test function delete."""
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_image():
|
|
||||||
"""Test function get_image."""
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
|
|
||||||
def test_put_image():
|
|
||||||
"""Test function put_image."""
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
|
|
||||||
def test_get_model():
|
|
||||||
"""Test function get_model."""
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
|
|
||||||
def test_put_model():
|
|
||||||
"""Test function put_model."""
|
|
||||||
raise NotImplementedError()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
os.environ['LOG_LEVEL'] = 'debug'
|
|
||||||
setup_logging()
|
|
||||||
pytest.main()
|
|
||||||
@@ -30,4 +30,4 @@ class TestConnectMinio(unittest.TestCase):
|
|||||||
self.tearDown()
|
self.tearDown()
|
||||||
# run test
|
# run test
|
||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
connect_minio()
|
_ = connect_minio()
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
"""Definition of unittests for delete function."""
|
"""Definition of unittests for delete function."""
|
||||||
|
|
||||||
import random
|
|
||||||
import string
|
|
||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import Mock
|
from unittest.mock import Mock
|
||||||
|
|
||||||
@@ -13,22 +11,19 @@ from shared.datastore import delete
|
|||||||
class TestDelete(unittest.TestCase):
|
class TestDelete(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
# mock minio client
|
# set relevant variables
|
||||||
self.client = Mock(spec=Minio)
|
self.client = Mock(spec=Minio)
|
||||||
# set bucket name
|
|
||||||
self.bucket_name = 'test-bucket'
|
self.bucket_name = 'test-bucket'
|
||||||
# set object name
|
self.object_name = '46KXJMFIAPVLM0TKRFZR5YPPTVJ6PJNX'
|
||||||
self.object_name = ''.join(
|
# set bad arguments
|
||||||
random.choices(
|
self.bad_client = 'not-minio-type'
|
||||||
string.ascii_uppercase + string.digits,
|
self.bad_string = float(0.0)
|
||||||
k=24,
|
self.len_0_string = ''
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_should_fail_on_wrong_input_type_client(self):
|
def test_should_fail_on_wrong_input_type_client(self):
|
||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
delete(
|
delete(
|
||||||
client='not-minio-type',
|
client=self.bad_client,
|
||||||
bucket_name=self.bucket_name,
|
bucket_name=self.bucket_name,
|
||||||
object_name=self.object_name,
|
object_name=self.object_name,
|
||||||
)
|
)
|
||||||
@@ -37,7 +32,7 @@ class TestDelete(unittest.TestCase):
|
|||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
delete(
|
delete(
|
||||||
client=self.client,
|
client=self.client,
|
||||||
bucket_name=0.0,
|
bucket_name=self.bad_string,
|
||||||
object_name=self.object_name,
|
object_name=self.object_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -45,7 +40,7 @@ class TestDelete(unittest.TestCase):
|
|||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
delete(
|
delete(
|
||||||
client=self.client,
|
client=self.client,
|
||||||
bucket_name='',
|
bucket_name=self.len_0_string,
|
||||||
object_name=self.object_name,
|
object_name=self.object_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -54,7 +49,7 @@ class TestDelete(unittest.TestCase):
|
|||||||
delete(
|
delete(
|
||||||
client=self.client,
|
client=self.client,
|
||||||
bucket_name=self.bucket_name,
|
bucket_name=self.bucket_name,
|
||||||
object_name=0.0,
|
object_name=self.bad_string,
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_should_fail_on_wrong_input_length_object_name(self):
|
def test_should_fail_on_wrong_input_length_object_name(self):
|
||||||
@@ -62,16 +57,5 @@ class TestDelete(unittest.TestCase):
|
|||||||
delete(
|
delete(
|
||||||
client=self.client,
|
client=self.client,
|
||||||
bucket_name=self.bucket_name,
|
bucket_name=self.bucket_name,
|
||||||
object_name='',
|
object_name=self.len_0_string,
|
||||||
)
|
|
||||||
|
|
||||||
def test_should_call_client__remove_object(self):
|
|
||||||
delete(
|
|
||||||
client=self.client,
|
|
||||||
bucket_name=self.bucket_name,
|
|
||||||
object_name=self.object_name,
|
|
||||||
)
|
|
||||||
self.client.remove_object.assert_called_with(
|
|
||||||
bucket_name=self.bucket_name,
|
|
||||||
object_name=self.object_name,
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,62 +1,63 @@
|
|||||||
# """Definition of unittests for get_image function."""
|
"""Definition of unittests for get_image function."""
|
||||||
|
|
||||||
# import os
|
import os
|
||||||
# import unittest
|
import unittest
|
||||||
# from unittest.mock import Mock
|
from unittest.mock import MagicMock
|
||||||
# from minio import Minio
|
|
||||||
# import random
|
|
||||||
# import string
|
|
||||||
# from PIL import Image
|
|
||||||
# from io import BytesIO
|
|
||||||
|
|
||||||
# from shared.datastore import get_image
|
from minio import Minio
|
||||||
|
|
||||||
|
from shared.datastore import get_image
|
||||||
|
|
||||||
|
|
||||||
# class TestGetImage(unittest.TestCase):
|
class TestGetImage(unittest.TestCase):
|
||||||
|
|
||||||
# def setUp(self):
|
def setUp(self):
|
||||||
# # mock minio client
|
# set relevant variables
|
||||||
# self.client = Mock(spec=Minio)
|
self.client = MagicMock(spec=Minio)
|
||||||
# # mock response object
|
self.object_name = '46KXJMFIAPVLM0TKRFZR5YPPTVJ6PJNX'
|
||||||
# image = Image.new(mode='RGB', size=(480,480))
|
self.env_var_map = {
|
||||||
# buffer = BytesIO()
|
'MINIO_BUCKET_NAME': 'test-bucket',
|
||||||
# image.save(buffer, 'png')
|
}
|
||||||
# self.client.get_object.return_value.data = buffer.getvalue()
|
# set bad arguments
|
||||||
# # set object name
|
self.bad_client = 'not-minio-type'
|
||||||
# self.object_name = ''.join(
|
self.bad_string = float(0.0)
|
||||||
# random.choices(
|
self.len_0_string = ''
|
||||||
# string.ascii_uppercase + string.digits,
|
# populate env
|
||||||
# k=24
|
for key, val in self.env_var_map.items():
|
||||||
# )
|
os.environ[key] = val
|
||||||
# )
|
|
||||||
# # populate env
|
|
||||||
# self.env_var_map = {
|
|
||||||
# 'MINIO_BUCKET_NAME': 'test-bucket'
|
|
||||||
# }
|
|
||||||
# for key, val in self.env_var_map.items():
|
|
||||||
# os.environ[key] = val
|
|
||||||
|
|
||||||
# def tearDown(self):
|
def tearDown(self):
|
||||||
# # clean env
|
# clean env
|
||||||
# for key in self.env_var_map:
|
for key in self.env_var_map:
|
||||||
# _ = os.environ.pop(key, default=None)
|
_ = os.environ.pop(key, default=None)
|
||||||
|
|
||||||
# def test_should_fail_when_env_not_set(self):
|
def test_should_fail_when_env_not_set(self):
|
||||||
# # ensure env not set
|
# ensure env not set
|
||||||
# self.tearDown()
|
self.tearDown()
|
||||||
# # run test
|
# run test
|
||||||
# with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
# get_image(
|
get_image(
|
||||||
# client=self.client,
|
client=self.client,
|
||||||
# object_name=self.object_name
|
object_name=self.object_name,
|
||||||
# )
|
)
|
||||||
|
|
||||||
# def test_should_call_client__get_object(self):
|
def test_should_fail_on_wrong_input_type_client(self):
|
||||||
# get_image(
|
with self.assertRaises(AssertionError):
|
||||||
# client=self.client,
|
get_image(
|
||||||
# object_name=self.object_name,
|
client=self.bad_client,
|
||||||
# )
|
object_name=self.object_name,
|
||||||
# self.client.get_object.assert_called_with(
|
)
|
||||||
# bucket_name=self.env_var_map['MINIO_BUCKET_NAME'],
|
|
||||||
# object_name=f'images/{self.object_name}',
|
def test_should_fail_on_wrong_input_type_object_name(self):
|
||||||
# )
|
with self.assertRaises(AssertionError):
|
||||||
|
get_image(
|
||||||
|
client=self.client,
|
||||||
|
object_name=self.bad_string,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_length_object_name(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
get_image(
|
||||||
|
client=self.client,
|
||||||
|
object_name=self.len_0_string,
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,62 +1,63 @@
|
|||||||
# """Definition of unittest for get_model function."""
|
"""Definition of unittest for get_model function."""
|
||||||
|
|
||||||
# import os
|
import os
|
||||||
# import unittest
|
import unittest
|
||||||
# from unittest.mock import Mock
|
from unittest.mock import MagicMock
|
||||||
# from minio import Minio
|
|
||||||
# import random
|
|
||||||
# import string
|
|
||||||
|
|
||||||
# from io import BytesIO
|
from minio import Minio
|
||||||
|
|
||||||
# from shared.datastore import get_model
|
from shared.datastore import get_model
|
||||||
|
|
||||||
|
|
||||||
# class TestGetModel(unittest.TestCase):
|
class TestGetModel(unittest.TestCase):
|
||||||
|
|
||||||
# def setUp(self):
|
def setUp(self):
|
||||||
# # mock minio client
|
# set relevant variables
|
||||||
# self.client = Mock(spec=Minio)
|
self.client = MagicMock(spec=Minio)
|
||||||
# # mock response object
|
self.object_name = '46KXJMFIAPVLM0TKRFZR5YPPTVJ6PJNX'
|
||||||
# image = Image.new(mode='RGB', size=(480,480))
|
self.env_var_map = {
|
||||||
# buffer = BytesIO()
|
'MINIO_BUCKET_NAME': 'test-bucket',
|
||||||
# image.save(buffer, 'png')
|
}
|
||||||
# self.client.get_object.return_value.data = buffer.getvalue()
|
# set bad arguments
|
||||||
# # set object name
|
self.bad_client = 'not-minio-type'
|
||||||
# self.object_name = ''.join(
|
self.bad_string = float(0.0)
|
||||||
# random.choices(
|
self.len_0_string = ''
|
||||||
# string.ascii_uppercase + string.digits,
|
# populate env
|
||||||
# k=24
|
for key, val in self.env_var_map.items():
|
||||||
# )
|
os.environ[key] = val
|
||||||
# )
|
|
||||||
# # populate env
|
|
||||||
# self.env_var_map = {
|
|
||||||
# 'MINIO_BUCKET_NAME': 'test-bucket'
|
|
||||||
# }
|
|
||||||
# for key, val in self.env_var_map.items():
|
|
||||||
# os.environ[key] = val
|
|
||||||
|
|
||||||
# def tearDown(self):
|
def tearDown(self):
|
||||||
# # clean env
|
# clean env
|
||||||
# for key in self.env_var_map:
|
for key in self.env_var_map:
|
||||||
# _ = os.environ.pop(key, default=None)
|
_ = os.environ.pop(key, default=None)
|
||||||
|
|
||||||
# def test_should_fail_when_env_not_set(self):
|
def test_should_fail_when_env_not_set(self):
|
||||||
# # ensure env not set
|
# ensure env not set
|
||||||
# self.tearDown()
|
self.tearDown()
|
||||||
# # run test
|
# run test
|
||||||
# with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
# get_image(
|
get_model(
|
||||||
# client=self.client,
|
client=self.client,
|
||||||
# object_name=self.object_name
|
object_name=self.object_name,
|
||||||
# )
|
)
|
||||||
|
|
||||||
# def test_should_call_client__get_object(self):
|
def test_should_fail_on_wrong_input_type_client(self):
|
||||||
# get_image(
|
with self.assertRaises(AssertionError):
|
||||||
# client=self.client,
|
get_model(
|
||||||
# object_name=self.object_name,
|
client=self.bad_client,
|
||||||
# )
|
object_name=self.object_name,
|
||||||
# self.client.get_object.assert_called_with(
|
)
|
||||||
# bucket_name=self.env_var_map['MINIO_BUCKET_NAME'],
|
|
||||||
# object_name=f'images/{self.object_name}',
|
def test_should_fail_on_wrong_input_type_object_name(self):
|
||||||
# )
|
with self.assertRaises(AssertionError):
|
||||||
|
get_model(
|
||||||
|
client=self.client,
|
||||||
|
object_name=self.bad_string,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_length_object_name(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
get_model(
|
||||||
|
client=self.client,
|
||||||
|
object_name=self.len_0_string,
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
"""Definition of unittests for get function."""
|
"""Definition of unittests for get function."""
|
||||||
|
|
||||||
import random
|
|
||||||
import string
|
|
||||||
import unittest
|
import unittest
|
||||||
from io import BytesIO
|
from unittest.mock import MagicMock
|
||||||
from unittest.mock import Mock
|
|
||||||
|
|
||||||
from minio import Minio
|
from minio import Minio
|
||||||
|
|
||||||
@@ -12,29 +9,20 @@ from shared.datastore import get
|
|||||||
|
|
||||||
|
|
||||||
class TestGet(unittest.TestCase):
|
class TestGet(unittest.TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
# mock minio client
|
# set relevant variables
|
||||||
self.client = Mock(spec=Minio)
|
self.client = MagicMock(spec=Minio)
|
||||||
# set bucket name
|
|
||||||
self.bucket_name = 'test-bucket'
|
self.bucket_name = 'test-bucket'
|
||||||
# set object name
|
self.object_name = '46KXJMFIAPVLM0TKRFZR5YPPTVJ6PJNX'
|
||||||
self.object_name = ''.join(
|
# set bad arguments
|
||||||
random.choices(
|
self.bad_client = 'not-minio-type'
|
||||||
string.ascii_uppercase + string.digits,
|
self.bad_string = float(0.0)
|
||||||
k=24,
|
self.len_0_string = ''
|
||||||
),
|
|
||||||
)
|
|
||||||
# mock response object
|
|
||||||
self.client.get_object.return_value.status = 200
|
|
||||||
buffer = BytesIO(random.randbytes(2**20))
|
|
||||||
self.client.return_value.get_object = buffer
|
|
||||||
self.client.get_object.return_value.read = buffer.getvalue()
|
|
||||||
|
|
||||||
def test_should_fail_on_wrong_input_type_client(self):
|
def test_should_fail_on_wrong_input_type_client(self):
|
||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
get(
|
get(
|
||||||
client='not-minio-type',
|
client=self.bad_client,
|
||||||
bucket_name=self.bucket_name,
|
bucket_name=self.bucket_name,
|
||||||
object_name=self.object_name,
|
object_name=self.object_name,
|
||||||
)
|
)
|
||||||
@@ -43,7 +31,7 @@ class TestGet(unittest.TestCase):
|
|||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
get(
|
get(
|
||||||
client=self.client,
|
client=self.client,
|
||||||
bucket_name=0.0,
|
bucket_name=self.bad_string,
|
||||||
object_name=self.object_name,
|
object_name=self.object_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -51,7 +39,7 @@ class TestGet(unittest.TestCase):
|
|||||||
with self.assertRaises(AssertionError):
|
with self.assertRaises(AssertionError):
|
||||||
get(
|
get(
|
||||||
client=self.client,
|
client=self.client,
|
||||||
bucket_name='',
|
bucket_name=self.len_0_string,
|
||||||
object_name=self.object_name,
|
object_name=self.object_name,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -60,7 +48,7 @@ class TestGet(unittest.TestCase):
|
|||||||
get(
|
get(
|
||||||
client=self.client,
|
client=self.client,
|
||||||
bucket_name=self.bucket_name,
|
bucket_name=self.bucket_name,
|
||||||
object_name=0.0,
|
object_name=self.bad_string,
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_should_fail_on_wrong_input_length_object_name(self):
|
def test_should_fail_on_wrong_input_length_object_name(self):
|
||||||
@@ -68,16 +56,5 @@ class TestGet(unittest.TestCase):
|
|||||||
get(
|
get(
|
||||||
client=self.client,
|
client=self.client,
|
||||||
bucket_name=self.bucket_name,
|
bucket_name=self.bucket_name,
|
||||||
object_name='',
|
object_name=self.len_0_string,
|
||||||
)
|
|
||||||
|
|
||||||
def test_should_call_client__get_object(self):
|
|
||||||
get(
|
|
||||||
client=self.client,
|
|
||||||
bucket_name=self.bucket_name,
|
|
||||||
object_name=self.object_name,
|
|
||||||
)
|
|
||||||
self.client.get_object.assert_called_with(
|
|
||||||
bucket_name=self.bucket_name,
|
|
||||||
object_name=self.object_name,
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
"""Definition of unittests for put_image function."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from minio import Minio
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
from shared.datastore import put_image
|
||||||
|
|
||||||
|
|
||||||
|
class TestPutImage(unittest.TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
# set relevant variables
|
||||||
|
self.client = MagicMock(spec=Minio)
|
||||||
|
self.image = Image.new(mode='RGB', size=(480, 480))
|
||||||
|
self.env_var_map = {
|
||||||
|
'MINIO_BUCKET_NAME': 'test-bucket',
|
||||||
|
}
|
||||||
|
# set bad arguments
|
||||||
|
self.bad_client = 'not-minio-type'
|
||||||
|
self.bad_image = 'not-image-type'
|
||||||
|
# populate env
|
||||||
|
for key, val in self.env_var_map.items():
|
||||||
|
os.environ[key] = val
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_type_client(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
put_image(
|
||||||
|
client=self.bad_client,
|
||||||
|
image=self.image,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_type_image(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
put_image(
|
||||||
|
client=self.client,
|
||||||
|
image=self.bad_image,
|
||||||
|
)
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
"""Definition of unittests for put_model function."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from minio import Minio
|
||||||
|
from torch.nn import Module
|
||||||
|
|
||||||
|
from shared.datastore import put_model
|
||||||
|
|
||||||
|
|
||||||
|
class TestPutModel(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
# set relevant variables
|
||||||
|
self.client = MagicMock(spec=Minio)
|
||||||
|
self.model = MagicMock(spec=Module)
|
||||||
|
self.env_var_map = {
|
||||||
|
'MINIO_BUCKET_NAME': 'test-bucket',
|
||||||
|
}
|
||||||
|
# set bad arguments
|
||||||
|
self.bad_client = 'not-minio-type'
|
||||||
|
self.bad_model = 'not-image-type'
|
||||||
|
# populate env
|
||||||
|
for key, val in self.env_var_map.items():
|
||||||
|
os.environ[key] = val
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_type_client(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
put_model(
|
||||||
|
client=self.bad_client,
|
||||||
|
model=self.model,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_type_model(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
put_model(
|
||||||
|
client=self.client,
|
||||||
|
model=self.bad_model,
|
||||||
|
)
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
"""Definition of unittests for put function."""
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
from io import BytesIO
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from minio import Minio
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
from shared.datastore import put
|
||||||
|
|
||||||
|
|
||||||
|
class TestPut(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
# set relevant variables
|
||||||
|
self.client = MagicMock(spec=Minio)
|
||||||
|
self.bucket_name = 'test-bucket'
|
||||||
|
self.object_name = '46KXJMFIAPVLM0TKRFZR5YPPTVJ6PJNX'
|
||||||
|
self.image = Image.new(mode='RGB', size=(480, 480))
|
||||||
|
self.buffer = BytesIO()
|
||||||
|
self.image.save(self.buffer, 'png')
|
||||||
|
# set bad arguments
|
||||||
|
self.bad_client = 'not-minio-type'
|
||||||
|
self.bad_string = float(0.0)
|
||||||
|
self.bad_buffer = 'not-buffer-type'
|
||||||
|
self.len_0_string = ''
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_type_client(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
put(
|
||||||
|
client=self.bad_client,
|
||||||
|
buffer=self.buffer,
|
||||||
|
bucket_name=self.bucket_name,
|
||||||
|
object_name=self.object_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_type_buffer(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
put(
|
||||||
|
client=self.client,
|
||||||
|
buffer=self.bad_buffer,
|
||||||
|
bucket_name=self.bucket_name,
|
||||||
|
object_name=self.object_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_type_bucket_name(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
put(
|
||||||
|
client=self.client,
|
||||||
|
buffer=self.buffer,
|
||||||
|
bucket_name=self.bad_string,
|
||||||
|
object_name=self.object_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_length_bucket_name(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
put(
|
||||||
|
client=self.client,
|
||||||
|
buffer=self.buffer,
|
||||||
|
bucket_name=self.len_0_string,
|
||||||
|
object_name=self.object_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_type_object_name(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
put(
|
||||||
|
client=self.client,
|
||||||
|
buffer=self.buffer,
|
||||||
|
bucket_name=self.bucket_name,
|
||||||
|
object_name=self.bad_string,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_should_fail_on_wrong_input_length_object_name(self):
|
||||||
|
with self.assertRaises(AssertionError):
|
||||||
|
put(
|
||||||
|
client=self.client,
|
||||||
|
buffer=self.buffer,
|
||||||
|
bucket_name=self.bucket_name,
|
||||||
|
object_name=self.len_0_string,
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user