added fixtures
This commit is contained in:
@@ -1,8 +1,12 @@
|
|||||||
"""Integration test configurations."""
|
"""Integration test configurations."""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import random
|
||||||
|
from collections.abc import Iterator
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from minio import Minio
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from testcontainers.minio import MinioContainer
|
from testcontainers.minio import MinioContainer
|
||||||
|
|
||||||
@@ -11,6 +15,7 @@ env_var_map = {
|
|||||||
'MINIO_ACCESS_KEY': 'test-access-key',
|
'MINIO_ACCESS_KEY': 'test-access-key',
|
||||||
'MINIO_SECRET_KEY': 'test-secret-key',
|
'MINIO_SECRET_KEY': 'test-secret-key',
|
||||||
'MINIO_BUCKET_NAME': 'test-bucket',
|
'MINIO_BUCKET_NAME': 'test-bucket',
|
||||||
|
'MINIO_OBJECT_NAME': '46KXJMFIAPVLM0TKRFZR5YPPTVJ6PJNX',
|
||||||
}
|
}
|
||||||
container_map = {
|
container_map = {
|
||||||
'minio': MinioContainer(
|
'minio': MinioContainer(
|
||||||
@@ -58,8 +63,70 @@ def populate_env(
|
|||||||
request.addfinalizer(cleanup_env)
|
request.addfinalizer(cleanup_env)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope='session')
|
||||||
|
def minio_client(
|
||||||
|
setup_infrastructure,
|
||||||
|
populate_env,
|
||||||
|
) -> Iterator[Minio]:
|
||||||
|
# prepare arguments
|
||||||
|
minio_endpoint = str(os.getenv('MINIO_ENDPOINT'))
|
||||||
|
minio_access_key = str(os.getenv('MINIO_ACCESS_KEY'))
|
||||||
|
minio_secret_key = str(os.getenv('MINIO_SECRET_KEY'))
|
||||||
|
minio_bucket_name = str(os.getenv('MINIO_BUCKET_NAME'))
|
||||||
|
# connect to minio
|
||||||
|
client = Minio(
|
||||||
|
endpoint=minio_endpoint,
|
||||||
|
access_key=minio_access_key,
|
||||||
|
secret_key=minio_secret_key,
|
||||||
|
secure=False,
|
||||||
|
)
|
||||||
|
# ensure bucket exists
|
||||||
|
if not client.bucket_exists(bucket_name=minio_bucket_name):
|
||||||
|
client.make_bucket(bucket_name=minio_bucket_name)
|
||||||
|
# expose client
|
||||||
|
yield client
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def image() -> Image.Image:
|
def data() -> Iterator[bytes]:
|
||||||
|
# generate random data
|
||||||
|
num_bytes = 2**21 # 2 MB
|
||||||
|
data = random.randbytes(n=num_bytes)
|
||||||
|
# expose data
|
||||||
|
yield data
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def data_in_minio(
|
||||||
|
minio_client,
|
||||||
|
data,
|
||||||
|
) -> Iterator[tuple[BytesIO, str, str]]:
|
||||||
|
# prepare arguments
|
||||||
|
minio_bucket_name = str(os.getenv('MINIO_BUCKET_NAME'))
|
||||||
|
minio_object_name = str(os.getenv('MINIO_OBJECT_NAME'))
|
||||||
|
# convert data
|
||||||
|
buffer = BytesIO(data)
|
||||||
|
# prepare for saving
|
||||||
|
num_bytes = buffer.tell()
|
||||||
|
buffer.seek(0)
|
||||||
|
# send data to bucket
|
||||||
|
minio_client.put_object(
|
||||||
|
bucket_name=minio_bucket_name,
|
||||||
|
object_name=minio_object_name,
|
||||||
|
length=num_bytes,
|
||||||
|
data=buffer,
|
||||||
|
)
|
||||||
|
# expose data
|
||||||
|
yield buffer, minio_bucket_name, minio_object_name
|
||||||
|
# clean up
|
||||||
|
minio_client.remove_object(
|
||||||
|
bucket_name=minio_bucket_name,
|
||||||
|
object_name=minio_object_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def image() -> Iterator[Image.Image]:
|
||||||
# generate image
|
# generate image
|
||||||
image = Image.new(mode='RGB', size=(480, 480))
|
image = Image.new(mode='RGB', size=(480, 480))
|
||||||
# expose image
|
# expose image
|
||||||
|
|||||||
Reference in New Issue
Block a user