Files
derpibooru-telegram-bot/tests/test_fetch.py
2025-08-23 10:11:53 +03:00

38 lines
1.2 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.
from unittest.mock import AsyncMock, MagicMock
import pytest
import app
app.sent_images = set() # сброс уже отправленных изображений
@pytest.mark.asyncio
async def test_fetch_image_success():
fake_tags = ["gay"]
# Ответ от API
mock_response_data = {
"images": [
{
"representations": {"full": "http://example.com/img.jpg"},
"uploader": "tester",
"view_url": "http://derpibooru.org/img/1",
"tags": ["gay", "pony"]
}
]
}
# Мок объекта ответа, поддерживающий async with
mock_response = AsyncMock()
mock_response.status = 200
mock_response.json = AsyncMock(return_value=mock_response_data)
# Мок объекта session.get, который возвращает mock_response через __aenter__
mock_session = MagicMock()
mock_session.get.return_value.__aenter__.return_value = mock_response
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 == "http://derpibooru.org/img/1"
assert "gay" in img_tags