Skip to content

Commit

Permalink
Testing django-qstash results model
Browse files Browse the repository at this point in the history
  • Loading branch information
jmitchel3 committed Dec 31, 2024
1 parent 88c50bf commit 9173373
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ source = [
"django_qstash",
"tests",
]
omit = [
"*/migrations/*",
]

[tool.coverage.paths]
source = [
Expand Down
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

INSTALLED_APPS: list[str] = [
"django_qstash",
"django_qstash.results",
]

MIDDLEWARE: list[str] = [
Expand Down
3 changes: 3 additions & 0 deletions tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
76 changes: 76 additions & 0 deletions tests/test_results_models.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ commands =
[flake8]
max-line-length = 88
extend-ignore = E203,E501
exclude = */migrations/*

[tool.pytest.ini_options]
addopts = """\
Expand Down

0 comments on commit 9173373

Please sign in to comment.