тесты

This commit is contained in:
DarkShyMW
2025-08-23 10:11:53 +03:00
parent d01d58cc7f
commit c9a3d40458
5 changed files with 60 additions and 65 deletions

View File

@@ -1,51 +1,37 @@
from unittest.mock import AsyncMock, MagicMock
import pytest
from aioresponses import aioresponses
from app import fetch_image, DERPYBOORU_API_SEARCH
import app
app.sent_images = set() # сброс уже отправленных изображений
@pytest.mark.asyncio
async def test_fetch_image_success():
"""Тестируем успешный возврат картинки из API"""
tags = ["gay"]
fake_tags = ["gay"]
mock_data = {
# Ответ от API
mock_response_data = {
"images": [
{
"id": 123,
"representations": {"full": "http://example.com/img.jpg"},
"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"
}
"view_url": "http://derpibooru.org/img/1",
"tags": ["gay", "pony"]
}
]
}
with aioresponses() as mocked:
mocked.get(DERPYBOORU_API_SEARCH, status=200, payload=mock_data)
# Мок объекта ответа, поддерживающий async with
mock_response = AsyncMock()
mock_response.status = 200
mock_response.json = AsyncMock(return_value=mock_response_data)
async with __import__("aiohttp").ClientSession() as session:
result = await fetch_image(session, tags)
# Мок объекта session.get, который возвращает mock_response через __aenter__
mock_session = MagicMock()
mock_session.get.return_value.__aenter__.return_value = mock_response
assert result is not None
url, author, source, img_tags = result
assert url == "https://example.com/full.png"
url, author, source, img_tags = await app.fetch_image(mock_session, fake_tags)
assert url == "http://example.com/img.jpg"
assert author == "tester"
assert source == "https://derpibooru.org/images/123"
assert source == "http://derpibooru.org/img/1"
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