diff --git a/test_app.py b/test_app.py new file mode 100644 index 0000000..683d39a --- /dev/null +++ b/test_app.py @@ -0,0 +1,42 @@ +import pytest +import asyncio +from aioresponses import aioresponses +from app import fetch_image, DERPYBOORU_API_SEARCH, DERPIBOORU_TOKEN + +@pytest.mark.asyncio +async def test_fetch_image_found(): + # Мокаем API-ответ с одним изображением + tags = ["safe", "pony"] + with aioresponses() as m: + m.get( + f"{DERPYBOORU_API_SEARCH}", + payload={ + "images": [ + { + "representations": {"full": "https://test.img/full.png"}, + "uploader": "tester", + "view_url": "https://derpibooru.org/images/1", + "tags": ["pony", "cute"] + } + ] + } + ) + async with asyncio.get_event_loop().run_until_complete: + result = await fetch_image(tags) + assert result is not None + url, author, source, img_tags = result + assert url == "https://test.img/full.png" + assert author == "tester" + assert source == "https://derpibooru.org/images/1" + assert "pony" in img_tags + +@pytest.mark.asyncio +async def test_fetch_image_not_found(): + tags = ["nonexistenttag123"] + with aioresponses() as m: + m.get( + f"{DERPYBOORU_API_SEARCH}", + payload={"images": []} + ) + result = await fetch_image(tags) + assert result is None