Files
derpibooru-telegram-bot/test_app.py
2025-08-23 04:02:45 +03:00

43 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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