|
| 1 | +from unittest.mock import AsyncMock, MagicMock |
| 2 | + |
| 3 | +import pytest |
| 4 | +from discord.ext import commands |
| 5 | + |
| 6 | +from europython_discord.dog.cog import DogCog |
| 7 | +from europython_discord.dog.config import DogConfig |
| 8 | +from europython_discord.dog.dogclient import DogClient |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def config() -> DogConfig: |
| 13 | + return DogConfig() |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def bot() -> MagicMock: |
| 18 | + return MagicMock(spec=commands.Bot) |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def mock_client() -> DogClient: |
| 23 | + client = MagicMock(spec=DogClient) |
| 24 | + client.fetch_random_dog.return_value = "https://images.dog.ceo/dog.jpg" |
| 25 | + return client |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def cog(bot: MagicMock, config: DogConfig, mock_client: DogClient) -> DogCog: |
| 30 | + return DogCog(bot, config, client=mock_client) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def ctx() -> AsyncMock: |
| 35 | + mock = AsyncMock(spec=commands.Context) |
| 36 | + mock.channel.name = "pet-appreciation" |
| 37 | + mock.send = AsyncMock() |
| 38 | + return mock |
| 39 | + |
| 40 | + |
| 41 | +async def test_dog_command_success(cog: DogCog, ctx: AsyncMock) -> None: |
| 42 | + await cog.dog_command.callback(cog, ctx) |
| 43 | + |
| 44 | + ctx.send.assert_awaited_once() |
| 45 | + embed = ctx.send.call_args.kwargs["embed"] |
| 46 | + assert embed.image.url == "https://images.dog.ceo/dog.jpg" |
| 47 | + |
| 48 | + |
| 49 | +async def test_dog_command_api_error(cog: DogCog, ctx: AsyncMock, mock_client: DogClient) -> None: |
| 50 | + mock_client.fetch_random_dog.return_value = None |
| 51 | + |
| 52 | + await cog.dog_command.callback(cog, ctx) |
| 53 | + |
| 54 | + ctx.send.assert_awaited_once() |
| 55 | + text = ctx.send.call_args.args[0] |
| 56 | + |
| 57 | + assert text in cog.config.error_messages |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize( |
| 61 | + "channel_name", |
| 62 | + ["wrong-channel", "general", ""], |
| 63 | +) |
| 64 | +async def test_dog_command_wrong_channel(cog: DogCog, channel_name: str) -> None: |
| 65 | + ctx = AsyncMock(spec=commands.Context) |
| 66 | + ctx.channel.name = channel_name |
| 67 | + ctx.send = AsyncMock() |
| 68 | + |
| 69 | + await cog.dog_command.callback(cog, ctx) |
| 70 | + |
| 71 | + ctx.send.assert_not_awaited() |
0 commit comments