diff --git a/pyproject.toml b/pyproject.toml index 2f60992..2ef89e6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -72,6 +72,9 @@ source = [ "django_qstash", "tests", ] +omit = [ + "*/migrations/*", +] [tool.coverage.paths] source = [ diff --git a/tests/settings.py b/tests/settings.py index 7a1455d..6be8a3e 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -12,6 +12,7 @@ INSTALLED_APPS: list[str] = [ "django_qstash", + "django_qstash.results", ] MIDDLEWARE: list[str] = [ diff --git a/tests/test_handlers.py b/tests/test_handlers.py index e6e6388..960c3ba 100644 --- a/tests/test_handlers.py +++ b/tests/test_handlers.py @@ -13,6 +13,9 @@ from django_qstash.handlers import QStashWebhook from django_qstash.handlers import TaskPayload +# Add pytest mark for database access +pytestmark = pytest.mark.django_db + class TestTaskPayload: def test_from_dict_valid(self): diff --git a/tests/test_results_models.py b/tests/test_results_models.py new file mode 100644 index 0000000..83ea33a --- /dev/null +++ b/tests/test_results_models.py @@ -0,0 +1,76 @@ +from __future__ import annotations + +import uuid +from datetime import datetime + +import pytest +from django.utils import timezone + +from django_qstash.results.models import TaskResult + + +@pytest.mark.django_db(transaction=True) +class TestTaskResult: + def test_create_task_result(self): + task_result = TaskResult.objects.create( + task_id="test-task-123", + task_name="test_module.test_function", + ) + + assert isinstance(task_result.id, uuid.UUID) + assert task_result.task_id == "test-task-123" + assert task_result.task_name == "test_module.test_function" + assert task_result.status == "PENDING" + assert isinstance(task_result.date_created, datetime) + assert task_result.date_done is None + assert task_result.result is None + assert task_result.traceback is None + assert task_result.args is None + assert task_result.kwargs is None + + def test_str_representation(self): + task_result = TaskResult.objects.create( + task_id="test-task-123", + task_name="test_module.test_function", + ) + + assert str(task_result) == "test_module.test_function (test-task-123)" + + def test_task_result_with_all_fields(self): + task_result = TaskResult.objects.create( + task_id="test-task-456", + task_name="test_module.other_function", + status="SUCCESS", + date_done=timezone.now(), + result={"success": True}, + traceback="No errors", + args=[1, 2, 3], + kwargs={"key": "value"}, + ) + + assert task_result.status == "SUCCESS" + assert isinstance(task_result.date_done, datetime) + assert task_result.result == {"success": True} + assert task_result.traceback == "No errors" + assert task_result.args == [1, 2, 3] + assert task_result.kwargs == {"key": "value"} + + def test_ordering(self): + # Create tasks with different completion dates + older_task = TaskResult.objects.create( + task_id="older-task", + task_name="test.older", + status="SUCCESS", + date_done=timezone.now(), + ) + newer_task = TaskResult.objects.create( + task_id="newer-task", + task_name="test.newer", + status="SUCCESS", + date_done=timezone.now(), + ) + + # Get all tasks and verify ordering + tasks = TaskResult.objects.all() + assert tasks[0].task_id == newer_task.task_id + assert tasks[1].task_id == older_task.task_id diff --git a/tox.ini b/tox.ini index 25f9009..40304e6 100644 --- a/tox.ini +++ b/tox.ini @@ -25,6 +25,7 @@ commands = [flake8] max-line-length = 88 extend-ignore = E203,E501 +exclude = */migrations/* [tool.pytest.ini_options] addopts = """\