From 20b80731c05be7703bb3485e6bf789aa92deb9ae Mon Sep 17 00:00:00 2001 From: DarkShy <30442729+DarkShyMW@users.noreply.github.com> Date: Sat, 23 Aug 2025 04:02:45 +0300 Subject: [PATCH] Create test_app.py --- test_app.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 test_app.py 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