Files
visual_critical_discourse_a…/shared/datastore/tests/integration/conftest.py
T
2024-11-16 19:46:37 +00:00

79 lines
2.0 KiB
Python

"""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