import io import httpx import pytest import pytest_asyncio from src.backend.utils import images SAMPLE_WEBP_URL: str = "https://www.gstatic.com/webp/gallery/1.webp" SAMPLE_JPG_URL: str = "https://www.gstatic.com/webp/gallery/1.jpg" @pytest_asyncio.fixture(scope="session") async def _sample_image(): """Sample image.""" client = httpx.AsyncClient() async def _(url): response = await client.get(url) return response.content yield _ await client.aclose() @pytest_asyncio.fixture(scope="session") async def sample_jpg(_sample_image): """Sample image.""" return await _sample_image(SAMPLE_JPG_URL) @pytest_asyncio.fixture(scope="session") async def sample_webp(_sample_image): """Sample image.""" return await _sample_image(SAMPLE_WEBP_URL) @pytest.mark.asyncio @pytest.mark.parametrize("as_bytesio", [True, False]) async def test_is_webp(sample_jpg, sample_webp, as_bytesio): """Test is_webp function, downloading test images.""" if as_bytesio: sample_jpg = images.BytesIO(sample_jpg) sample_webp = images.BytesIO(sample_webp) assert images.is_webp(sample_webp) assert not images.is_webp(sample_jpg) @pytest.mark.asyncio @pytest.mark.parametrize("as_bytesio", [True, False]) async def test_convert_webp_to_png(sample_webp, as_bytesio): if as_bytesio: sample_webp = io.BytesIO(sample_webp) assert images.is_webp(sample_webp) assert not images.is_webp(images.convert_to_png(sample_webp))