Skip to content

Commit

Permalink
test: add tests for manual job scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
yolile committed Jan 28, 2025
1 parent b67d6c1 commit b801304
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/data_registry/process_manager/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
52 changes: 52 additions & 0 deletions tests/data_registry/test_admin.py
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 23 additions & 0 deletions tests/fixtures/fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
]

0 comments on commit b801304

Please sign in to comment.