From 53f58eb743120a1e272f7f131ecf2beb662e7f47 Mon Sep 17 00:00:00 2001 From: stijndcl Date: Tue, 21 Jun 2022 21:06:11 +0200 Subject: [PATCH] Write a few tests --- tests/conftest.py | 16 +++- tests/test_didier/__init__.py | 0 tests/test_didier/test_utils/__init__.py | 0 .../test_utils/test_discord/__init__.py | 0 .../test_utils/test_discord/test_prefix.py | 84 +++++++++++++++++++ tests/test_dummy.py | 2 - 6 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 tests/test_didier/__init__.py create mode 100644 tests/test_didier/test_utils/__init__.py create mode 100644 tests/test_didier/test_utils/test_discord/__init__.py create mode 100644 tests/test_didier/test_utils/test_discord/test_prefix.py delete mode 100644 tests/test_dummy.py diff --git a/tests/conftest.py b/tests/conftest.py index 1ae9878..a74ba4c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,5 @@ -import os from typing import AsyncGenerator +from unittest.mock import MagicMock import pytest @@ -7,6 +7,7 @@ from alembic import command, config from sqlalchemy.ext.asyncio import AsyncSession from database.engine import engine +from didier import Didier @pytest.fixture(scope="session") @@ -38,3 +39,16 @@ async def database_session(tables) -> AsyncGenerator[AsyncSession, None]: await transaction.rollback() await connection.close() + + +@pytest.fixture +def mock_client() -> Didier: + """Fixture to get a mock Didier instance + The mock uses 0 as the id + """ + mock_client = MagicMock() + mock_user = MagicMock() + mock_user.id = 0 + mock_client.user = mock_user + + return mock_client diff --git a/tests/test_didier/__init__.py b/tests/test_didier/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_didier/test_utils/__init__.py b/tests/test_didier/test_utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_didier/test_utils/test_discord/__init__.py b/tests/test_didier/test_utils/test_discord/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_didier/test_utils/test_discord/test_prefix.py b/tests/test_didier/test_utils/test_discord/test_prefix.py new file mode 100644 index 0000000..d49e8e3 --- /dev/null +++ b/tests/test_didier/test_utils/test_discord/test_prefix.py @@ -0,0 +1,84 @@ +from unittest.mock import MagicMock + +from didier import Didier +from didier.utils.discord.prefix import get_prefix + + +def test_get_prefix_didier(mock_client: Didier): + """Test the "didier" prefix""" + mock_message = MagicMock() + mock_message.content = "didier test" + assert get_prefix(mock_client, mock_message) == "didier " + + +def test_get_prefix_didier_cased(mock_client: Didier): + """Test the "didier" prefix with random casing""" + mock_message = MagicMock() + mock_message.content = "Didier test" + assert get_prefix(mock_client, mock_message) == "Didier " + + mock_message = MagicMock() + mock_message.content = "DIDIER test" + assert get_prefix(mock_client, mock_message) == "DIDIER " + + mock_message = MagicMock() + mock_message.content = "DiDiEr test" + assert get_prefix(mock_client, mock_message) == "DiDiEr " + + +def test_get_prefix_default(mock_client: Didier): + """Test the fallback prefix (used when nothing matched)""" + mock_message = MagicMock() + mock_message.content = "random message" + assert get_prefix(mock_client, mock_message) == "didier" + + +def test_get_prefix_big_d(mock_client: Didier): + """Test the "big d" prefix""" + mock_message = MagicMock() + mock_message.content = "big d test" + assert get_prefix(mock_client, mock_message) == "big d " + + +def test_get_prefix_big_d_cased(mock_client: Didier): + """Test the "big d" prefix with random casing""" + mock_message = MagicMock() + mock_message.content = "Big d test" + assert get_prefix(mock_client, mock_message) == "Big d " + + mock_message = MagicMock() + mock_message.content = "Big D test" + assert get_prefix(mock_client, mock_message) == "Big D " + + mock_message = MagicMock() + mock_message.content = "BIG D test" + assert get_prefix(mock_client, mock_message) == "BIG D " + + +def test_get_prefix_mention_username(mock_client: Didier): + """Test the @mention prefix when mentioned by username""" + mock_message = MagicMock() + prefix = f"<@{mock_client.user.id}> " + mock_message.content = f"{prefix}test" + + assert get_prefix(mock_client, mock_message) == prefix + + +def test_get_prefix_mention_nickname(mock_client: Didier): + """Test the @mention prefix when mentioned by server nickname""" + mock_message = MagicMock() + prefix = f"<@!{mock_client.user.id}> " + mock_message.content = f"{prefix}test" + + assert get_prefix(mock_client, mock_message) == prefix + + +def test_get_prefix_whitespace(mock_client: Didier): + """Test that variable whitespace doesn't matter""" + mock_message = MagicMock() + mock_message.content = "didiertest" + assert get_prefix(mock_client, mock_message) == "didier" + + mock_message = MagicMock() + mock_message.content = "didier test" + assert get_prefix(mock_client, mock_message) == "didier " diff --git a/tests/test_dummy.py b/tests/test_dummy.py deleted file mode 100644 index 569bcda..0000000 --- a/tests/test_dummy.py +++ /dev/null @@ -1,2 +0,0 @@ -def test_dummy(tables): - assert True