Files
derpibooru-telegram-bot/tests/test_fetch.py
DarkShyMW 16ee57b158 Add tests
2025-08-23 09:54:40 +03:00

52 lines
1.6 KiB
Python

import pytest
from aioresponses import aioresponses
from app import fetch_image, DERPYBOORU_API_SEARCH
@pytest.mark.asyncio
async def test_fetch_image_success():
"""Тестируем успешный возврат картинки из API"""
tags = ["gay"]
mock_data = {
"images": [
{
"id": 123,
"uploader": "tester",
"view_url": "https://derpibooru.org/images/123",
"tags": ["gay", "cute"],
"representations": {
"full": "https://example.com/full.png",
"large": "https://example.com/large.png"
}
}
]
}
with aioresponses() as mocked:
mocked.get(DERPYBOORU_API_SEARCH, status=200, payload=mock_data)
async with __import__("aiohttp").ClientSession() as session:
result = await fetch_image(session, tags)
assert result is not None
url, author, source, img_tags = result
assert url == "https://example.com/full.png"
assert author == "tester"
assert source == "https://derpibooru.org/images/123"
assert "gay" in img_tags
@pytest.mark.asyncio
async def test_fetch_image_empty():
"""Тестируем ситуацию, когда API не вернул картинок"""
tags = ["nonexistent"]
with aioresponses() as mocked:
mocked.get(DERPYBOORU_API_SEARCH, status=200, payload={"images": []})
async with __import__("aiohttp").ClientSession() as session:
result = await fetch_image(session, tags)
assert result is None