From b80130406d58154829b3a271b491ae4b2512c744 Mon Sep 17 00:00:00 2001 From: Yohanna Lisnichuk Date: Tue, 28 Jan 2025 15:52:12 -0300 Subject: [PATCH] test: add tests for manual job scheduling --- .../process_manager/test_process.py | 2 + tests/data_registry/test_admin.py | 52 +++++++++++++++++++ tests/fixtures/fixtures.json | 23 ++++++++ 3 files changed, 77 insertions(+) create mode 100644 tests/data_registry/test_admin.py diff --git a/tests/data_registry/process_manager/test_process.py b/tests/data_registry/process_manager/test_process.py index 5d0c1593..f273f888 100644 --- a/tests/data_registry/process_manager/test_process.py +++ b/tests/data_registry/process_manager/test_process.py @@ -22,6 +22,7 @@ def test_task_progress(self): settings.JOB_TASKS_PLAN = ["test"] # First call initializes the job and runs the first task. + self.assertTrue(collection.is_out_of_date()) process(collection) job = job_set.first() task = job.task_set.order_by("order").first() @@ -46,6 +47,7 @@ def test_task_progress(self): self.assertIsNotNone(task.start) self.assertIsNotNone(task.end) self.assertEqual("OK", task.result) + self.assertFalse(collection.is_out_of_date()) def test_delete_jobs(self): collection = Collection.objects.get(pk=1) diff --git a/tests/data_registry/test_admin.py b/tests/data_registry/test_admin.py new file mode 100644 index 00000000..f891996a --- /dev/null +++ b/tests/data_registry/test_admin.py @@ -0,0 +1,52 @@ +from django.contrib.auth.models import User +from django.contrib.contenttypes.models import ContentType +from django.contrib.messages import get_messages +from django.test import TestCase +from django.urls import reverse +from django.utils import timezone + +from data_registry.models import Collection, Job + + +class AdminTests(TestCase): + fixtures = ["tests/fixtures/fixtures.json"] + + @classmethod + def setUpTestData(cls): + cls.user = User.objects.create_superuser("Test") + + def test_create_job(self): + content_type = ContentType.objects.get_for_model(Collection) + collection = Collection.objects.get(pk=1) + # No jobs + data = { + "action": "create_job", + "_selected_action": collection.pk, + } + self.client.force_login(self.user) + response = self.client.post(reverse(f"admin:{content_type.app_label}_{content_type.model}_changelist"), data) + self.assertEqual( + str(next(iter(get_messages(response.wsgi_request)))), + "Created 0 jobs. 1 publications either have incomplete jobs or will be scheduled shortly.", + ) + self.assertEqual(collection.job_set.count(), 0) + + # Out of date job + self.client.cookies.pop("messages") + out_of_date_collection = Collection.objects.get(pk=2) + out_of_date_job = Job.objects.get(pk=1) + out_of_date_job.start = timezone.now() + out_of_date_job.save() + data["_selected_action"] = out_of_date_collection.pk + response = self.client.post(reverse(f"admin:{content_type.app_label}_{content_type.model}_changelist"), data) + self.assertEqual(str(next(iter(get_messages(response.wsgi_request)))), "Created 1 jobs.") + self.assertEqual(out_of_date_collection.job_set.count(), 2) + + # Incomplete job + self.client.cookies.pop("messages") + response = self.client.post(reverse(f"admin:{content_type.app_label}_{content_type.model}_changelist"), data) + self.assertEqual( + str(next(iter(get_messages(response.wsgi_request)))), + "Created 0 jobs. 1 publications either have incomplete jobs or will be scheduled shortly.", + ) + self.assertEqual(out_of_date_collection.job_set.count(), 2) diff --git a/tests/fixtures/fixtures.json b/tests/fixtures/fixtures.json index 6c3aee54..eca69b2a 100644 --- a/tests/fixtures/fixtures.json +++ b/tests/fixtures/fixtures.json @@ -9,5 +9,28 @@ "created": "2001-02-03T04:05:06Z", "modified": "2001-02-03T07:08:09Z" } + }, + { + "model": "data_registry.collection", + "pk": 2, + "fields": { + "title": "test_out_of_date", + "country": "PT", + "retrieval_frequency": "MONTHLY", + "created": "2001-02-03T04:05:06Z", + "modified": "2001-02-03T07:08:09Z" + } + }, + { + "model": "data_registry.job", + "pk": 1, + "fields": { + "collection_id": 2, + "created": "2001-02-03T04:05:06Z", + "modified": "2001-02-03T07:08:09Z", + "start": "2030-02-03T07:08:09Z", + "end": "2030-02-04T07:08:09Z", + "status": "COMPLETED" + } } ]