Add tests
This commit is contained in:
51
tests/test_fetch.py
Normal file
51
tests/test_fetch.py
Normal file
@@ -0,0 +1,51 @@
|
||||
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
|
||||
24
tests/test_post.py
Normal file
24
tests/test_post.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from app import post_image
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@patch("app.bot.send_photo", new_callable=AsyncMock)
|
||||
@patch("app.fetch_image", return_value=(
|
||||
"https://example.com/full.png",
|
||||
"tester",
|
||||
"https://derpibooru.org/images/123",
|
||||
["gay", "cute"]
|
||||
))
|
||||
async def test_post_image_success(mock_fetch, mock_send_photo, tmp_path, monkeypatch):
|
||||
"""Тестируем успешную публикацию картинки"""
|
||||
|
||||
# временный файл для sent_images
|
||||
monkeypatch.setattr("app.SENT_IMAGES_FILE", tmp_path / "sent.json")
|
||||
monkeypatch.setattr("app.sent_images", set())
|
||||
|
||||
await post_image(tags=["gay"])
|
||||
|
||||
mock_fetch.assert_called_once()
|
||||
mock_send_photo.assert_awaited_once()
|
||||
Reference in New Issue
Block a user