Skip to content

Commit

Permalink
Added tests for celery task review
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuastegmaier committed Nov 20, 2024
1 parent e40c851 commit b96e4c9
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 5 deletions.
5 changes: 2 additions & 3 deletions concordia/admin/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,14 @@ def celery_task_review(request):
asset_unstarted = 0
asset_failure = 0
context = {
"title": "Active Importer Tasks",
"title": "Importer Tasks",
"campaigns": [],
"projects": [],
}
idx = request.GET.get("id")

if idx is not None:
projects = Project.objects.filter(campaign_id=int(idx))
for project in projects:
for project in Project.objects.filter(campaign_id=int(idx)):
asset_successful = 0
asset_failure = 0
asset_incomplete = 0
Expand Down
228 changes: 228 additions & 0 deletions concordia/tests/test_admin_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
CreateTestUsers,
StreamingTestMixin,
create_asset,
create_campaign,
create_card,
create_project,
create_site_report,
)
from importer.tests.utils import create_import_asset


@mock.patch("importer.utils.excel.load_workbook", autospec=True)
Expand Down Expand Up @@ -1035,6 +1038,231 @@ def test_large_number_urls(self):
self.assertEqual(messages[3], "All Processes Completed")


class TestCeleryTaskReview(CreateTestUsers, TestCase):
def setUp(self):
# We don't set up our data here because we want to test
# both with and without data
self.login_user(is_staff=True, is_superuser=True)
self.path = reverse("admin:celery-review")

def add_campaigns(self):
self.add_active_campaigns()
self.add_completed_campaigns()
self.add_retired_campaigns()

def add_active_campaigns(self):
self.campaign1 = create_campaign(
slug="test-active-campaign-1", title="Test Active Campaign 1"
)
self.campaign2 = create_campaign(
slug="test-active-campaign-2", title="Test Active Campaign 2"
)

def add_completed_campaigns(self):
self.completed_campaign1 = create_campaign(
slug="test-completed-campaign-1",
title="Test Completed Campaign 1",
status=Campaign.Status.COMPLETED,
)
self.completed_campaign2 = create_campaign(
slug="test-completed-campaign-2",
title="Test Completed Campaign 1",
status=Campaign.Status.COMPLETED,
)

def add_retired_campaigns(self):
self.retired_campaign1 = create_campaign(
slug="test-retired-campaign-1",
title="Test Retired Campaign 1",
status=Campaign.Status.RETIRED,
)
self.retired_campaign2 = create_campaign(
slug="test-retired-campaign-2",
title="Test Retired Campaign 1",
status=Campaign.Status.RETIRED,
)

def add_projects(self):
# Active campaign 1, three projects
create_project(
campaign=self.campaign1,
slug="campaign1-project-1",
title="Campaign 1 Project 1",
)
create_project(
campaign=self.campaign1,
slug="campaign1-project-2",
title="Campaign 1 Project 2",
)
create_project(
campaign=self.campaign1,
slug="campaign1-project-3",
title="Campaign 1 Project 3",
)

# Active campaign 2, two projects
create_project(
campaign=self.campaign2,
slug="campaign1-project-1",
title="Campaign 2 Project 1",
)
create_project(
campaign=self.campaign2,
slug="campaign1-project-2",
title="Campaign 2 Project 2",
)

# Completed campaign 1, two projects
create_project(
campaign=self.completed_campaign1,
slug="completed-campaign1-project-1",
title="Completed Campaign 1 Project 1",
)
create_project(
campaign=self.completed_campaign1,
slug="completed-campaign1-project-2",
title="Completed Campaign 1 Project 2",
)

# Completed campaign 2, one project
create_project(
campaign=self.completed_campaign2,
slug="completed-campaign2-project-1",
title="Completed Campaign 1 Project 1",
)

# We don't create any for retired campaigns since the campaigns
# are only created to make sure the view ignores them

def add_tasks(self, campaign):
data = []
for project in campaign.project_set.all():
import_asset = create_import_asset(1, project=project)
item = import_asset.import_item
import_job = item.job
create_import_asset(
2,
import_item=item,
import_job=import_job,
project=project,
last_started=timezone.now(),
)
create_import_asset(
3,
import_item=item,
import_job=import_job,
project=project,
failed=timezone.now(),
last_started=timezone.now(),
)
create_import_asset(
4,
import_item=item,
import_job=import_job,
project=project,
completed=timezone.now(),
last_started=timezone.now(),
)
data.append(
{
"title": project.title,
"id": project.id,
"campaign_id": str(campaign.id),
"successful": 1,
"incomplete": 1,
"unstarted": 1,
"failure": 1,
}
)
return data

def test_empty_dashboard(self):
response = self.client.get(self.path)
context = response.context

self.assertEqual(response.status_code, 200)
self.assertIn("campaigns", context)
campaigns = list(context["campaigns"])
self.assertEqual(campaigns, [])

def test_dashboard(self):
self.add_active_campaigns()
response = self.client.get(self.path)
context = response.context
self.assertEqual(response.status_code, 200)
self.assertIn("campaigns", context)
self.assertIn(self.campaign1, context["campaigns"])
self.assertIn(self.campaign2, context["campaigns"])

self.add_completed_campaigns()
response = self.client.get(self.path)
context = response.context
self.assertEqual(response.status_code, 200)
self.assertIn("campaigns", context)
campaigns = list(context["campaigns"])
self.assertIn(self.campaign1, campaigns)
self.assertIn(self.campaign2, campaigns)
self.assertIn(self.completed_campaign1, campaigns)
self.assertIn(self.completed_campaign2, campaigns)

self.add_retired_campaigns()
response = self.client.get(self.path)
context = response.context
self.assertEqual(response.status_code, 200)
self.assertIn("campaigns", context)
campaigns = list(context["campaigns"])
self.assertIn(self.campaign1, campaigns)
self.assertIn(self.campaign2, campaigns)
self.assertIn(self.completed_campaign1, campaigns)
self.assertIn(self.completed_campaign2, campaigns)
self.assertNotIn(self.retired_campaign1, campaigns)
self.assertNotIn(self.retired_campaign2, campaigns)

def test_campaign_dashboard(self):
self.add_campaigns()
self.add_projects()

data = self.add_tasks(self.campaign1)
response = self.client.get(self.path, {"id": self.campaign1.id})
context = response.context
self.assertIn("campaigns", context)
self.assertEqual(context["campaigns"], [])
self.assertIn("totalassets", context)
self.assertEqual(context["totalassets"], 12)
self.assertIn("projects", context)
self.assertEqual(context["projects"], data)

data = self.add_tasks(self.campaign2)
response = self.client.get(self.path, {"id": self.campaign2.id})
context = response.context
self.assertIn("campaigns", context)
self.assertEqual(context["campaigns"], [])
self.assertIn("totalassets", context)
self.assertEqual(context["totalassets"], 8)
self.assertIn("projects", context)
self.assertEqual(context["projects"], data)

data = self.add_tasks(self.completed_campaign1)
response = self.client.get(self.path, {"id": self.completed_campaign1.id})
context = response.context
self.assertIn("campaigns", context)
self.assertEqual(context["campaigns"], [])
self.assertIn("totalassets", context)
self.assertEqual(context["totalassets"], 8)
self.assertIn("projects", context)
self.assertEqual(context["projects"], data)

data = self.add_tasks(self.completed_campaign2)
response = self.client.get(self.path, {"id": self.completed_campaign2.id})
context = response.context
self.assertIn("campaigns", context)
self.assertEqual(context["campaigns"], [])
self.assertIn("totalassets", context)
self.assertEqual(context["totalassets"], 4)
self.assertIn("projects", context)
self.assertEqual(context["projects"], data)


class TestSerializedObjectView(TestCase):
def setUp(self):
self.card = create_card()
Expand Down
49 changes: 47 additions & 2 deletions importer/tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,55 @@
from concordia.tests.utils import create_project
from importer.models import ImportJob
from django.utils.text import slugify

from concordia.tests.utils import create_asset, create_item, create_project
from importer.models import ImportItem, ImportItemAsset, ImportJob


def create_import_job(*, project=None, **kwargs):
# project is a concordia.models.Project instance
if project is None:
project = create_project()
import_job = ImportJob(project=project, **kwargs)
import_job.save()
return import_job


def create_import_item(item=None, project=None, import_job=None, **kwargs):
# item is a concordia.models.Item instance
# project is a concordia.models.Project instance
# import_job is an importer.models.ImportJob instance
if import_job is None:
import_job = create_import_job(project=project)
if item is None:
item = create_item(project=import_job.project)
import_item = ImportItem(item=item, job=import_job, **kwargs)
import_item.save()
return import_item


def create_import_asset(
sequence_number,
asset=None,
item=None,
import_item=None,
project=None,
import_job=None,
**kwargs,
):
# asset is a concordia.models.Asset instance
# item is a concordia.models.Item instance
# import_item is an importer.models.ImportItem instance
# project is a concordia.models.Project instance
# import_job is an importer.models.ImportJob instance
if import_item is None:
import_item = create_import_item(
item=item, import_job=import_job, project=project
)
if asset is None:
item_slug = slugify(import_item.item.title)
slug = f"{item_slug}-{sequence_number}"
asset = create_asset(item=import_item.item, slug=slug)
import_asset = ImportItemAsset(
sequence_number=sequence_number, asset=asset, import_item=import_item, **kwargs
)
import_asset.save()
return import_asset

0 comments on commit b96e4c9

Please sign in to comment.