From d8b516ee017bcf66937d17d513d8d7195bec1e7c Mon Sep 17 00:00:00 2001 From: Sergei Maertens Date: Fri, 20 Dec 2024 17:00:03 +0100 Subject: [PATCH] :white_check_mark: [#4930] Test the export form implementation Ensures that the filters are correctly implemented. --- .../forms/tests/admin/test_form_statistics.py | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/src/openforms/forms/tests/admin/test_form_statistics.py b/src/openforms/forms/tests/admin/test_form_statistics.py index f8710fc1dc..b20094ce61 100644 --- a/src/openforms/forms/tests/admin/test_form_statistics.py +++ b/src/openforms/forms/tests/admin/test_form_statistics.py @@ -11,6 +11,7 @@ from openforms.submissions.tests.factories import SubmissionFactory from ...forms import ExportStatisticsForm +from ..factories import FormFactory @disable_admin_mfa() @@ -97,3 +98,92 @@ def test_successful_export_downloads_file(self): response["Content-Disposition"], 'attachment; filename="submissions_2024-12-01_2025-01-01.xlsx"', ) + + def test_export_form_filters(self): + """ + Test that the form filters correctly filter down the matching log records. + """ + plugin = DemoRegistration("demo") + form1, form2, form3 = FormFactory.create_batch(3) + with freeze_time("2024-12-20T16:44:00+01:00"): + registered_submission_1 = SubmissionFactory.create( + form=form1, + registration_success=True, + public_registration_reference="SUB-01", + ) + logevent.registration_success(registered_submission_1, plugin=plugin) + + failed_submission = SubmissionFactory.create( + form=form1, + registration_failed=True, + public_registration_reference="FAIL-01", + ) + logevent.registration_failure(failed_submission, error=Exception("nope")) + with freeze_time("2024-11-20T12:00:00+01:00"): + registered_submission_2 = SubmissionFactory.create( + form=form2, + registration_success=True, + public_registration_reference="SUB-02", + ) + logevent.registration_success(registered_submission_2, plugin=plugin) + + with freeze_time("2024-12-05T12:00:00+01:00"): + registered_submission_3 = SubmissionFactory.create( + form=form3, + registration_success=True, + public_registration_reference="SUB-03", + ) + logevent.registration_success(registered_submission_3, plugin=plugin) + + with freeze_time("2024-12-06T10:00:00+01:00"): + registered_submission_4 = SubmissionFactory.create( + form=form3, + registration_success=True, + public_registration_reference="SUB-04", + ) + logevent.registration_success(registered_submission_4, plugin=plugin) + + with self.subTest("filter on start date"): + export_form1 = ExportStatisticsForm( + data={ + "start_date": "2024-12-14", + "end_date": "2024-12-31", + } + ) + assert export_form1.is_valid() + + dataset1 = export_form1.export() + + self.assertEqual(len(dataset1), 1) + self.assertEqual(dataset1[0][0], "SUB-01") + + with self.subTest("filter on end date"): + export_form2 = ExportStatisticsForm( + data={ + "start_date": "2024-01-01", + "end_date": "2024-12-05", + } + ) + assert export_form2.is_valid() + + dataset2 = export_form2.export() + + self.assertEqual(len(dataset2), 2) + self.assertEqual(dataset2[0][0], "SUB-02") + self.assertEqual(dataset2[1][0], "SUB-03") + + with self.subTest("filter on subset of forms"): + export_form3 = ExportStatisticsForm( + data={ + "start_date": "2024-01-01", + "end_date": "2024-12-31", + "limit_to_forms": [form2.pk, form3.pk], + } + ) + assert export_form3.is_valid() + + dataset3 = export_form3.export() + self.assertEqual(len(dataset3), 3) + self.assertEqual(dataset3[0][0], "SUB-02") + self.assertEqual(dataset3[1][0], "SUB-03") + self.assertEqual(dataset3[2][0], "SUB-04")