fix: implement backend testing amendments and atomic source publication
Some checks failed
Mostovik Backend CI/CD / Tests and lint (push) Successful in 3m16s
Mostovik Backend CI/CD / Deploy dev (push) Has been cancelled
Mostovik Backend CI/CD / Deploy and verify internal main (push) Has been cancelled
Mostovik Backend CI/CD / Build linux/amd64 release images (push) Has been cancelled
Some checks failed
Mostovik Backend CI/CD / Tests and lint (push) Successful in 3m16s
Mostovik Backend CI/CD / Deploy dev (push) Has been cancelled
Mostovik Backend CI/CD / Deploy and verify internal main (push) Has been cancelled
Mostovik Backend CI/CD / Build linux/amd64 release images (push) Has been cancelled
This commit is contained in:
@@ -102,7 +102,7 @@ class CoreAdminTest(TestCase):
|
||||
job = BackgroundJob.objects.create(
|
||||
task_id=fake.uuid4(),
|
||||
task_name="test.task",
|
||||
status="pending",
|
||||
status="queued",
|
||||
)
|
||||
request = self._request()
|
||||
qs = BackgroundJob.objects.all()
|
||||
@@ -110,7 +110,7 @@ class CoreAdminTest(TestCase):
|
||||
self.admin.revoke_jobs(request, qs)
|
||||
revoke_mock.assert_called_once_with(job.task_id, terminate=True)
|
||||
job.refresh_from_db()
|
||||
self.assertEqual(job.status, "revoked")
|
||||
self.assertEqual(job.status, "cancelled")
|
||||
|
||||
|
||||
class AdminDashboardTest(TestCase):
|
||||
|
||||
107
tests/apps/core/test_job_acceptance_lifecycle.py
Normal file
107
tests/apps/core/test_job_acceptance_lifecycle.py
Normal file
@@ -0,0 +1,107 @@
|
||||
"""Canonical terminal states are shared by jobs and source cards."""
|
||||
|
||||
import pytest
|
||||
from apps.core.models import BackgroundJob
|
||||
from apps.core.serializers import BackgroundJobSerializer
|
||||
from apps.parsers.source_cards import SOURCE_CARD_BY_SLUG, SourceCardService
|
||||
from django.core.cache import cache
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_new_job_uses_canonical_states_and_terminal_failure_is_private():
|
||||
cache.clear()
|
||||
task_name = SOURCE_CARD_BY_SLUG["planned-inspections"].task_names[0]
|
||||
job = BackgroundJob.objects.create(task_id="acceptance-failed", task_name=task_name)
|
||||
assert job.status == "queued"
|
||||
job.mark_started()
|
||||
assert job.status == "running"
|
||||
job.update_progress(100, "Publishing")
|
||||
assert job.progress == 99
|
||||
job.fail("HTTPError: https://user:secret@example.test/private?token=secret")
|
||||
for _ in range(2):
|
||||
payload = BackgroundJobSerializer(job).data
|
||||
assert payload["status"] == "failed"
|
||||
assert "secret" not in str(payload)
|
||||
assert "HTTPError" not in str(payload)
|
||||
card = SourceCardService.get_card("planned-inspections")
|
||||
assert card["status"] == "failed"
|
||||
assert card["active_tasks"] == []
|
||||
assert "HTTPError" not in card["error_message"]
|
||||
assert "secret" not in card["error_message"]
|
||||
job.complete()
|
||||
assert job.status == "failed"
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_success_and_skipped_jobs_are_terminal_and_card_progress_is_100():
|
||||
cache.clear()
|
||||
name = SOURCE_CARD_BY_SLUG["planned-inspections"].task_names[0]
|
||||
job = BackgroundJob.objects.create(
|
||||
task_id="acceptance-success", task_name=name, user_id=1
|
||||
)
|
||||
job.complete(result={"status": "skipped"})
|
||||
assert job.status == "skipped"
|
||||
assert job.is_finished and job.is_successful
|
||||
from apps.core.services import BackgroundJobService
|
||||
|
||||
assert job.pk in [
|
||||
item.pk
|
||||
for item in BackgroundJobService.get_user_jobs(job.user_id, status="skipped")
|
||||
]
|
||||
assert BackgroundJobSerializer(job).data["status"] == "skipped"
|
||||
card = SourceCardService.get_card("planned-inspections")
|
||||
assert card["status"] == "skipped"
|
||||
assert card["progress"] == 100
|
||||
assert card["active_tasks"] == []
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_status_data_migration_maps_legacy_rows_and_supports_rollback():
|
||||
from importlib import import_module
|
||||
from types import SimpleNamespace
|
||||
|
||||
from django.apps import apps
|
||||
from django.db import connection
|
||||
|
||||
migration = import_module("apps.core.migrations.0002_canonical_job_statuses")
|
||||
for index, status in enumerate(migration.STATUS_MAP):
|
||||
BackgroundJob.objects.create(
|
||||
task_id=f"migration-{index}", task_name="test", status=status
|
||||
)
|
||||
BackgroundJob.objects.create(
|
||||
task_id="migration-skipped",
|
||||
task_name="test",
|
||||
status="success",
|
||||
result={"status": "skipped"},
|
||||
)
|
||||
editor = SimpleNamespace(connection=connection)
|
||||
migration.forward_statuses(apps, editor)
|
||||
assert set(BackgroundJob.objects.values_list("status", flat=True)) == set(
|
||||
migration.STATUS_MAP.values()
|
||||
) | {"skipped"}
|
||||
migration.reverse_statuses(apps, editor)
|
||||
assert set(BackgroundJob.objects.values_list("status", flat=True)) == {
|
||||
"pending",
|
||||
"started",
|
||||
"retry",
|
||||
"failure",
|
||||
"revoked",
|
||||
"success",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_skipped_load_does_not_advance_last_successful_publication():
|
||||
from apps.parsers.models import ParserLoadLog
|
||||
|
||||
cache.clear()
|
||||
success = ParserLoadLog.objects.create(
|
||||
source="inspections", batch_id=1, status="success", records_count=10
|
||||
)
|
||||
ParserLoadLog.objects.create(
|
||||
source="inspections", batch_id=2, status="skipped", records_count=0
|
||||
)
|
||||
card = SourceCardService.get_card("planned-inspections")
|
||||
assert card["status"] == "skipped"
|
||||
assert card["last_updated_at"] == success.updated_at
|
||||
assert card["latest_success_load"]["status"] == "success"
|
||||
@@ -46,9 +46,13 @@ def test_revoke_preserves_worker_terminal_result(terminal):
|
||||
assert response.status_code == 200
|
||||
job.refresh_from_db()
|
||||
assert {field: getattr(job, field) for field in expected} == expected
|
||||
assert response.data["status"] == ("success" if terminal == "success" else "error")
|
||||
assert response.data["status"] == ("success" if terminal == "success" else "failed")
|
||||
assert response.data["progress"] == expected["progress"]
|
||||
assert response.data["message"] == expected["progress_message"]
|
||||
assert response.data["message"] == (
|
||||
expected["progress_message"]
|
||||
if terminal == "success"
|
||||
else "Не удалось обновить данные. Повторите попытку позже."
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
|
||||
Reference in New Issue
Block a user