Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CONCD-661 Additional unit tests for concordia/admin/filters.py #2282

Merged
merged 8 commits into from
Feb 20, 2024
140 changes: 140 additions & 0 deletions concordia/tests/test_admin_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
from django.test import RequestFactory, TestCase
from django.utils import timezone

from concordia.admin import ItemAdmin, ProjectAdmin, TranscriptionAdmin
from concordia.admin.filters import (
ItemProjectListFilter,
OcrGeneratedFilter,
ProjectCampaignListFilter,
ProjectCampaignStatusListFilter,
SubmittedFilter,
)
from concordia.models import Campaign, Item, Project, Transcription
from concordia.tests.utils import (
CreateTestUsers,
create_campaign,
create_item,
create_project,
create_site_report,
create_transcription,
)


class NullableTimestampFilterTest(CreateTestUsers, TestCase):
def setUp(self):
user = self.create_user(username="tester")
create_transcription(user=user, submitted=timezone.now())

def test_lookups(self):
f = SubmittedFilter(
None, {"submitted": "null"}, Transcription, TranscriptionAdmin
)
transcriptions = f.queryset(None, Transcription.objects.all())
self.assertEqual(transcriptions.count(), 0)

f = SubmittedFilter(
None, {"submitted": "not-null"}, Transcription, TranscriptionAdmin
)
transcriptions = f.queryset(None, Transcription.objects.all())
self.assertEqual(transcriptions.count(), 1)

f = SubmittedFilter(
None, {"submitted": timezone.now()}, Transcription, TranscriptionAdmin
)
transcriptions = f.queryset(None, Transcription.objects.all())
self.assertEqual(transcriptions.count(), 1)


class CampaignListFilterTests(CreateTestUsers, TestCase):
def setUp(self):
self.campaign = create_project().campaign

def test_project_filter(self):
request = RequestFactory().get(
"/admin/concordia/project/?campaign__id__exact=%s" % self.campaign.id
)
f = ProjectCampaignListFilter(
request,
{"campaign__id__exact": self.campaign.id},
Project,
ProjectAdmin,
)
projects = f.queryset(None, Project.objects.all())
self.assertEqual(projects.count(), 1)

request = RequestFactory().get("/admin/concordia/project/?campaign__status=1")
f = ProjectCampaignListFilter(
request, {"campaign__status": Campaign.Status.ACTIVE}, Project, ProjectAdmin
)
projects = f.queryset(None, Project.objects.all())
self.assertEqual(projects.count(), 1)


class ItemFilterTests(CreateTestUsers, TestCase):
def setUp(self):
self.project = create_item().project

def test_project_filter(self):
request = RequestFactory().get(
"/admin/concordia/item/?project__in=%s" % self.project.pk
)
f = ItemProjectListFilter(
request, {"project__in": (self.project.id,)}, Item, ItemAdmin
)
items = f.queryset(None, Item.objects.all())
self.assertEqual(items.count(), 1)

request = RequestFactory().get(
"/admin/concordia/item/?project__campaign__id__exact=%s"
% self.project.campaign.pk
)
f = ItemProjectListFilter(
request,
{"project__campaign__id__exact": self.project.campaign.pk},
Item,
ItemAdmin,
)
items = f.queryset(None, Item.objects.all())
self.assertEqual(items.count(), 1)


class ProjectFilterTests(TestCase):
def setUp(self):
self.project = create_item().project

def test_project_campaign_status_list_filter(self):
f = ProjectCampaignStatusListFilter(None, {}, Project, ProjectAdmin)
projects = f.queryset(None, Project.objects.all())
self.assertEqual(projects.count(), 1)

f = ProjectCampaignStatusListFilter(
None, {"campaign__status": Campaign.Status.ACTIVE}, Project, ProjectAdmin
)
projects = f.queryset(None, Project.objects.all())
self.assertEqual(projects.count(), 1)


class SiteReportCampaignListFilterTests(TestCase):
def setUp(self):
create_campaign()
create_site_report()

def test_lookups(self):
pass


class TranscriptionFilterTests(CreateTestUsers, TestCase):
def setUp(self):
user = self.create_user(username="tester")
create_transcription(user=user)

def test_ocr_filter(self):
f = OcrGeneratedFilter("No", {}, Transcription, TranscriptionAdmin)
transcriptions = f.queryset(None, Transcription.objects.all())
self.assertEqual(transcriptions.count(), 1)

f = OcrGeneratedFilter(
"No", {"ocr_generated": False}, Transcription, TranscriptionAdmin
)
transcriptions = f.queryset(None, Transcription.objects.all())
self.assertEqual(transcriptions.count(), 1)
8 changes: 8 additions & 0 deletions concordia/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Item,
MediaType,
Project,
SiteReport,
Tag,
Topic,
Transcription,
Expand Down Expand Up @@ -60,6 +61,13 @@ def create_campaign(
return campaign


def create_site_report(do_save=True):
site_report = SiteReport()
if do_save:
site_report.save()
return site_report


@ensure_slug
def create_topic(
*,
Expand Down