From dba3388567818cb5c60be9d406d3e1cccc84b3ea Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 29 Apr 2026 14:22:03 +0200 Subject: Refactor database connections and add integration tests - Use context managers for sqlite3 connections in cron, vclogs, and database utils - Add URL helper utility for yt-dlp with clean_url_for_ytdlp function - Add slow integration test markers and new integration test files - Fix test imports to use conftest directly instead of tests.conftest - Improve downloader error handling and use COPYPARTY_URL_UPLOAD env var - Update pytest.ini with slow test markers and warning filters - Document slow test usage in AGENTS.md --- tests/cogs/test_trigger_integration.py | 54 ++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tests/cogs/test_trigger_integration.py (limited to 'tests/cogs/test_trigger_integration.py') diff --git a/tests/cogs/test_trigger_integration.py b/tests/cogs/test_trigger_integration.py new file mode 100644 index 0000000..4828387 --- /dev/null +++ b/tests/cogs/test_trigger_integration.py @@ -0,0 +1,54 @@ +import pytest +from unittest.mock import MagicMock, AsyncMock, patch +from cogs.trigger import Trigger + + +@pytest.fixture +def trigger_cog(bot): + from cogs.utils.database import init_database + init_database() + from cogs.trigger import Trigger + cog = Trigger(bot) + return cog + + +@pytest.mark.slow +@pytest.mark.asyncio +async def test_trigger_response_flow(trigger_cog): + """Test trigger creation and response in a simulated message flow.""" + cog = trigger_cog + + ctx = MagicMock() + ctx.author = MagicMock() + ctx.author.id = 123 + ctx.guild = MagicMock() + ctx.guild.id = 456 + ctx.respond = AsyncMock() + + # Create a trigger using the create command + await cog.create.callback(cog, ctx, name="test_trigger") + + # Add a keyword and response + await cog.add.callback(cog, ctx, name="test_trigger", trigger="hello", response="world") + + # Verify trigger was created - check DB + from cogs.utils.database import get_connection + with get_connection() as conn: + c = conn.cursor() + c.execute("SELECT id FROM triggers WHERE name=?", ("test_trigger",)) + result = c.fetchone() + assert result is not None + + # Simulate a message that should trigger + message = MagicMock() + message.author = ctx.author + message.author.bot = False + message.guild = ctx.guild + message.content = "say hello to me" + message.channel.send = AsyncMock() # Make send awaitable + + # Check if trigger matches using the static method + await Trigger.trigger(message) + + # Verify message.channel.send was called with the response + message.channel.send.assert_called_once_with("world") -- cgit v1.2.3