Skip to content

Commit

Permalink
✅ [#4930] Test the export form implementation
Browse files Browse the repository at this point in the history
Ensures that the filters are correctly implemented.
  • Loading branch information
sergei-maertens committed Dec 20, 2024
1 parent 10f5300 commit 5627a76
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/openforms/forms/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def export_registration_statistics(
)

_start_date = make_aware(datetime.combine(start_date, time.min))
_end_date = make_aware(datetime.combine(end_date, time.max))
_end_date = make_aware(datetime.combine(end_date, time.min))

log_records = TimelineLogProxy.objects.filter(
content_type=ContentType.objects.get_for_model(Submission),
Expand Down
90 changes: 90 additions & 0 deletions src/openforms/forms/tests/admin/test_form_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from openforms.submissions.tests.factories import SubmissionFactory

from ...forms import ExportStatisticsForm
from ..factories import FormFactory


@disable_admin_mfa()
Expand Down Expand Up @@ -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": "2025-01-01",
}
)
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-06",
}
)
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": "2025-01-01",
"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")

0 comments on commit 5627a76

Please sign in to comment.