тесты
This commit is contained in:
@@ -1,24 +1,22 @@
|
||||
# tests/test_post.py
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, patch
|
||||
from app import post_image
|
||||
import app
|
||||
|
||||
|
||||
@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):
|
||||
"""Тестируем успешную публикацию картинки"""
|
||||
async def test_post_image_success():
|
||||
# Мокаем fetch_image
|
||||
with patch("app.fetch_image", new_callable=AsyncMock) as mock_fetch:
|
||||
mock_fetch.return_value = (
|
||||
"http://example.com/img.jpg",
|
||||
"tester",
|
||||
"http://derpibooru.org/img/1",
|
||||
["gay", "pony"]
|
||||
)
|
||||
|
||||
# временный файл для 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()
|
||||
# Заменяем весь объект bot на мок
|
||||
mock_bot = AsyncMock()
|
||||
with patch.object(app, "bot", new=mock_bot):
|
||||
await app.post_image()
|
||||
mock_bot.send_photo.assert_awaited_once()
|
||||
|
||||
Reference in New Issue
Block a user