52 lines
1.6 KiB
Python
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
|