feat: align source data and nightly exports
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
"""Tests for organization source backfill tasks and schedules."""
|
||||
|
||||
from importlib import import_module
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
from apps.parsers.models import ParserLoadLog
|
||||
from django.apps import apps as django_apps
|
||||
from django.conf import settings
|
||||
from django.core.cache import cache
|
||||
from django.test import TestCase
|
||||
from django.test import TestCase, override_settings
|
||||
from django.utils import timezone
|
||||
from django_celery_beat.models import PeriodicTask
|
||||
from organizations.cache import get_organization_api_cache_version
|
||||
@@ -17,6 +19,7 @@ from organizations.models import (
|
||||
from organizations.tasks import (
|
||||
backfill_all_organization_sources,
|
||||
backfill_organization_sources_for_parser_batch,
|
||||
refresh_source_record_export_artifacts,
|
||||
)
|
||||
|
||||
from tests.apps.parsers.factories import IndustrialCertificateRecordFactory
|
||||
@@ -116,3 +119,62 @@ class OrganizationSnapshotScheduleMigrationTest(TestCase):
|
||||
self.assertEqual(task.crontab.minute, "30")
|
||||
self.assertEqual(task.crontab.hour, "4")
|
||||
self.assertEqual(str(task.crontab.timezone), "Europe/Moscow")
|
||||
|
||||
|
||||
class SourceRecordExportArtifactsTaskTest(TestCase):
|
||||
"""Checks nightly artifact generation and its distributed lock."""
|
||||
|
||||
def setUp(self):
|
||||
cache.clear()
|
||||
self.export_directory = TemporaryDirectory()
|
||||
self.settings_override = override_settings(
|
||||
SOURCE_RECORD_EXPORT_DIRECTORY=self.export_directory.name,
|
||||
SOURCE_RECORD_EXPORT_GENERATIONS_TO_KEEP=2,
|
||||
SOURCE_RECORD_EXPORT_LOCK_KEY="test:source-record-exports:lock",
|
||||
SOURCE_RECORD_EXPORT_LOCK_TTL_SECONDS=300,
|
||||
)
|
||||
self.settings_override.enable()
|
||||
|
||||
def tearDown(self):
|
||||
self.settings_override.disable()
|
||||
self.export_directory.cleanup()
|
||||
cache.clear()
|
||||
super().tearDown()
|
||||
|
||||
def test_refresh_task_builds_all_artifacts_and_releases_lock(self):
|
||||
result = refresh_source_record_export_artifacts()
|
||||
|
||||
self.assertEqual(result["status"], "success")
|
||||
self.assertEqual(result["artifacts_count"], 25)
|
||||
self.assertIsNone(cache.get(settings.SOURCE_RECORD_EXPORT_LOCK_KEY))
|
||||
|
||||
def test_refresh_task_skips_when_another_generation_holds_lock(self):
|
||||
cache.set(settings.SOURCE_RECORD_EXPORT_LOCK_KEY, "busy", timeout=300)
|
||||
|
||||
result = refresh_source_record_export_artifacts()
|
||||
|
||||
self.assertEqual(result, {"status": "skipped", "reason": "locked"})
|
||||
|
||||
|
||||
class SourceRecordExportScheduleMigrationTest(TestCase):
|
||||
"""Checks the nightly Celery Beat schedule for export artifacts."""
|
||||
|
||||
def test_migration_seeds_nightly_source_record_export_task(self):
|
||||
migration = import_module(
|
||||
"organizations.migrations.0008_seed_nightly_source_record_exports"
|
||||
)
|
||||
|
||||
migration.seed_nightly_source_record_export_schedule(django_apps, None)
|
||||
migration.seed_nightly_source_record_export_schedule(django_apps, None)
|
||||
|
||||
task = PeriodicTask.objects.get(name=migration.NIGHTLY_SOURCE_EXPORT_TASK_NAME)
|
||||
self.assertEqual(
|
||||
task.task,
|
||||
"organizations.tasks.refresh_source_record_export_artifacts",
|
||||
)
|
||||
self.assertTrue(task.enabled)
|
||||
self.assertEqual(task.args, "[]")
|
||||
self.assertEqual(task.kwargs, "{}")
|
||||
self.assertEqual(task.crontab.minute, "30")
|
||||
self.assertEqual(task.crontab.hour, "5")
|
||||
self.assertEqual(str(task.crontab.timezone), "Europe/Moscow")
|
||||
|
||||
Reference in New Issue
Block a user