updated integration tests

This commit is contained in:
brian
2024-11-16 19:46:37 +00:00
parent c630498168
commit cbbb044177
6 changed files with 271 additions and 110 deletions
@@ -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