From 5675dfd20b24f1e0eef06b11d9ec41a894d3a5bf Mon Sep 17 00:00:00 2001 From: salmonik Date: Tue, 28 Apr 2026 20:05:36 +0200 Subject: multitude of changes. but primarly, use db for all data. Signed-off-by: salmonik --- tests/cogs/test_cron.py | 88 +++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 55 deletions(-) (limited to 'tests/cogs/test_cron.py') diff --git a/tests/cogs/test_cron.py b/tests/cogs/test_cron.py index 93031f8..91fcb82 100644 --- a/tests/cogs/test_cron.py +++ b/tests/cogs/test_cron.py @@ -1,5 +1,3 @@ -import json -import os import pytest from datetime import datetime from unittest.mock import MagicMock, patch @@ -19,68 +17,48 @@ def mock_bot(): def create_cog(mock_bot, tmp_path): with patch.object(Cron, "__init__", lambda self, bot: None): - cog = Cron.__new__(Cron) - cog.bot = mock_bot - cog.scheduled_tasks = [] - cog.cron_file = str(tmp_path / "cron_jobs.json") - cog.task = MagicMock() - return cog + with patch("cogs.cron.init_database"): + mock_conn = MagicMock() + mock_cursor = MagicMock() + mock_cursor.fetchall.return_value = [] + mock_conn.cursor.return_value = mock_cursor + mock_conn.close.return_value = None + + with patch("cogs.cron.get_connection", return_value=mock_conn): + with patch("cogs.utils.database.get_db_path", return_value=":memory:"): + cog = Cron.__new__(Cron) + cog.bot = mock_bot + cog.scheduled_tasks = [] + cog.task = MagicMock() + return cog class TestLoadTasks: - def test_load_tasks_file_not_exists(self, mock_bot, tmp_path): - cog = create_cog(mock_bot, tmp_path) - cog.cron_file = str(tmp_path / "nonexistent.json") - cog.load_tasks() - assert cog.scheduled_tasks == [] - - def test_load_tasks_empty_file(self, mock_bot, tmp_path): - cron_file = tmp_path / "empty.json" - cron_file.write_text("") - cog = create_cog(mock_bot, tmp_path) - cog.cron_file = str(cron_file) - cog.load_tasks() - assert cog.scheduled_tasks == [] - - def test_load_tasks_valid_file(self, mock_bot, tmp_path): - cron_file = tmp_path / "valid.json" - tasks = [{"id": 1, "cron_expression": "* * * * *", "type": "audio", "channel_id": 123, "active": True}] - cron_file.write_text(json.dumps(tasks)) - cog = create_cog(mock_bot, tmp_path) - cog.cron_file = str(cron_file) - cog.load_tasks() - assert len(cog.scheduled_tasks) == 1 - assert cog.scheduled_tasks[0]["id"] == 1 + def test_load_tasks_with_data(self, mock_bot, tmp_path): + mock_conn = MagicMock() + mock_cursor = MagicMock() + mock_cursor.fetchall.return_value = [ + (1, "* * * * *", "audio", 123, 1, None, "test message") + ] + mock_conn.cursor.return_value = mock_cursor + mock_conn.close.return_value = None - def test_load_tasks_io_error(self, mock_bot, tmp_path): - cog = create_cog(mock_bot, tmp_path) - with patch("builtins.open", side_effect=IOError("test error")): + with patch("cogs.cron.get_connection", return_value=mock_conn): + cog = Cron.__new__(Cron) + cog.bot = mock_bot + cog.scheduled_tasks = [] + cog.task = MagicMock() cog.load_tasks() - assert cog.scheduled_tasks == [] + assert len(cog.scheduled_tasks) == 1 + assert cog.scheduled_tasks[0]["id"] == 1 class TestSaveTasks: - def test_save_tasks_with_real_file(self, mock_bot, tmp_path): - cron_file = tmp_path / "cron_jobs.json" - cog = create_cog(mock_bot, tmp_path) - cog.cron_file = str(cron_file) - cog.scheduled_tasks = [{"id": 1, "cron_expression": "* * * * *"}] - cog.save_tasks() - assert os.path.exists(cron_file) - with open(cron_file) as f: - loaded = json.load(f) - assert len(loaded) == 1 - - def test_save_tasks_creates_directory_structure(self, mock_bot, tmp_path): + def test_save_tasks(self, mock_bot, tmp_path): cog = create_cog(mock_bot, tmp_path) - new_dir = tmp_path / "new_dir" - cron_file = new_dir / "cron_jobs.json" - cog.cron_file = str(cron_file) - cog.scheduled_tasks = [{"id": 1}] - + cog.scheduled_tasks = [{"id": 1, "cron_expression": "* * * * *", "type": "audio", "channel_id": 123, "active": True}] cog.save_tasks() - - assert os.path.exists(new_dir) + assert len(cog.scheduled_tasks) == 1 class TestShouldRunTask: @@ -163,4 +141,4 @@ class TestTaskTypes: def test_task_type_message_aliases(self, mock_bot, tmp_path): message_types = ("wyślij_wiadomość", "send_message") for task_type in message_types: - assert task_type in ("wyślij_wiadomość", "send_message") + assert task_type in ("wyślij_wiadomość", "send_message") \ No newline at end of file -- cgit v1.2.3