Files
mostovik-backend/tests/apps/core/test_job_progress.py
Aleksandr Meshchryakov 49cbfd265c
All checks were successful
Mostovik Backend CI/CD / Tests and lint (push) Successful in 9m37s
Mostovik Backend CI/CD / Build linux/amd64 release images (push) Successful in 4m18s
Mostovik Backend CI/CD / Deploy and verify internal main (push) Has been skipped
Mostovik Backend CI/CD / Deploy customer main (push) Has been skipped
Mostovik Backend CI/CD / Deploy dev (push) Successful in 1m48s
feat(registries): add SME and budget imports and fix source API workflows
2026-09-13 23:13:47 +02:00

59 lines
2.0 KiB
Python

"""Регрессии для запоздалых и повторных событий фоновой задачи."""
import pytest
from apps.core.models import BackgroundJob, JobStatus
@pytest.mark.django_db
def test_stale_progress_never_reduces_persisted_value():
job = BackgroundJob.objects.create(task_id="monotonic", task_name="test.task")
stale = BackgroundJob.objects.get(pk=job.pk)
job.update_progress(75, "Новая стадия")
stale.update_progress(25, "Старая стадия")
job.refresh_from_db()
assert (job.progress, job.progress_message) == (75, "Новая стадия")
@pytest.mark.django_db
@pytest.mark.parametrize("terminal", ["complete", "fail", "revoke"])
def test_terminal_job_ignores_late_events(terminal):
job = BackgroundJob.objects.create(task_id=terminal, task_name="test.task")
stale = BackgroundJob.objects.get(pk=job.pk)
job.update_progress(65, "Обработка")
if terminal == "complete":
job.complete({"saved": 2})
elif terminal == "fail":
job.fail("Ошибка источника")
else:
job.revoke()
expected = (job.status, job.progress, job.completed_at, job.result, job.error)
stale.mark_started()
stale.mark_retry()
stale.update_progress(5, "Запоздалое событие")
stale.complete({"saved": 0})
stale.fail("Поздняя ошибка")
stale.revoke()
job.refresh_from_db()
assert (
job.status,
job.progress,
job.completed_at,
job.result,
job.error,
) == expected
@pytest.mark.django_db
def test_retry_preserves_progress_and_success_finishes_at_100():
job = BackgroundJob.objects.create(task_id="retry", task_name="test.task")
job.mark_started()
started_at = job.started_at
job.update_progress(40)
job.mark_retry()
job.mark_started()
job.update_progress(0)
assert job.progress == 40
assert job.started_at == started_at
job.complete()
assert (job.status, job.progress) == (JobStatus.SUCCESS, 100)