Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evgfilim1 committed Nov 11, 2022
1 parent 65208d4 commit 66a3dee
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tests/test_modules/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import string
from unittest.mock import patch

import pytest
from hypothesis import assume, given
from hypothesis import strategies as st
from pyrogram import Client
Expand Down Expand Up @@ -100,6 +101,27 @@ def test_add_args(handler: CommandsHandler) -> None:
assert h.timeout == handler.timeout


def test_invalid_add() -> None:
"""Tests add() raises ValueError when invalid arguments are passed."""
commands = CommandsModule()

async def handler() -> None:
"""Test handler"""
pass

with patch.object(
CommandsModule,
"add_handler",
autospec=True,
) as mock:
with pytest.raises(ValueError):
# This call should fail because no commands are passed.
# Type linting is disabled because it's expected to fail.
commands.add(handler, prefix="?", usage="<usage>") # noqa # type: ignore

mock.assert_not_called()


@given(
handlers=st.lists(
st.tuples(
Expand Down Expand Up @@ -160,4 +182,27 @@ async def foo() -> None:
assert commands._middleware.has_handlers


def test_register_duplicates() -> None:
"""Tests register() raises ValueError when duplicate commands are added."""

commands = CommandsModule()
fake_client = Client("fake", in_memory=True)

@commands.add("foo")
@commands.add("foo")
async def foo() -> None:
"""Test handler"""
pass

with patch.object(
Client,
"add_handler",
autospec=True,
) as mock:
with pytest.raises(ValueError):
commands.register(fake_client)

mock.assert_not_called()


# endregion

0 comments on commit 66a3dee

Please sign in to comment.