diff --git a/concordia/tests/test_admin_filters.py b/concordia/tests/test_admin_filters.py new file mode 100644 index 000000000..2e9c5a3d3 --- /dev/null +++ b/concordia/tests/test_admin_filters.py @@ -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) diff --git a/concordia/tests/utils.py b/concordia/tests/utils.py index 1b1959633..d43be5842 100644 --- a/concordia/tests/utils.py +++ b/concordia/tests/utils.py @@ -11,6 +11,7 @@ Item, MediaType, Project, + SiteReport, Tag, Topic, Transcription, @@ -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( *,