From b53df0e62ffc3602ce364c4aa85b9297ea07adb6 Mon Sep 17 00:00:00 2001 From: shon-button Date: Thu, 19 Dec 2024 16:18:40 -0500 Subject: [PATCH] tests: report submission service chore: cleanup chore: cleanup chore: cleanup --- .../service/report_verification_service.py | 8 +-- .../service/test_report_submission_service.py | 70 +++++++++++++++++++ .../test_report_verification_service.py | 2 +- 3 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 bc_obps/reporting/tests/service/test_report_submission_service.py diff --git a/bc_obps/reporting/service/report_verification_service.py b/bc_obps/reporting/service/report_verification_service.py index 6d3ee73538..b4437a6935 100644 --- a/bc_obps/reporting/service/report_verification_service.py +++ b/bc_obps/reporting/service/report_verification_service.py @@ -62,7 +62,7 @@ def save_report_verification(version_id: int, data: ReportVerificationIn) -> Rep @staticmethod def get_report_needs_verification(version_id: int) -> bool: """ - Determines if a report needs verification based on its purpose + Determines if a report needs verification data based on its purpose and attributable emissions. """ REGULATED_OPERATION_PURPOSES = { @@ -73,14 +73,14 @@ def get_report_needs_verification(version_id: int) -> bool: ATTRIBUTABLE_EMISSION_THRESHOLD = Decimal("25000000") registration_purpose = ReportAdditionalDataService.get_registration_purpose_by_version_id(version_id) - # Registration Purpose: Users must complete the verification page if the registration purpose is in REGULATED_OPERATION_PURPOSES + # Registration Purpose: verification data is required if the registration purpose is in REGULATED_OPERATION_PURPOSES if isinstance(registration_purpose, dict): - registration_purpose = registration_purpose.get("registration_purpose") + registration_purpose = registration_purpose.get("registration_purpose", {}) if registration_purpose in REGULATED_OPERATION_PURPOSES: return True - # Emission threshold: Users must complete the verification page if the registration purpose is Reporting Operation, and total TCo₂e >+ 25,000 + # Emission threshold: verification data is required if the registration purpose is Reporting Operation, and total TCo₂e >+ 25,000 if registration_purpose == Operation.Purposes.REPORTING_OPERATION: attributable_emissions = ComplianceService.get_emissions_attributable_for_reporting(version_id) return attributable_emissions >= ATTRIBUTABLE_EMISSION_THRESHOLD diff --git a/bc_obps/reporting/tests/service/test_report_submission_service.py b/bc_obps/reporting/tests/service/test_report_submission_service.py new file mode 100644 index 0000000000..616c85569b --- /dev/null +++ b/bc_obps/reporting/tests/service/test_report_submission_service.py @@ -0,0 +1,70 @@ +import pytest +from unittest.mock import patch, MagicMock +from uuid import UUID +from reporting.models.report_attachment import ReportAttachment +from reporting.models.report_version import ReportVersion +from reporting.service.report_submission_service import ReportSubmissionService + + +class TestReportSubmissionService: + @patch("reporting.service.report_submission_service.ReportVerificationService.get_report_needs_verification") + @patch("reporting.models.report_attachment.ReportAttachment.objects.get") + def test_validate_report_with_verification_statement(self, mock_get_attachment, mock_get_verification): + # Arrange + version_id = 1 + mock_get_verification.return_value = True # Verification statement is mandatory + + # Act + ReportSubmissionService.validate_report(version_id) + + # Assert + mock_get_attachment.assert_called_once_with( + report_version_id=version_id, + attachment_type=ReportAttachment.ReportAttachmentType.VERIFICATION_STATEMENT, + ) + + @patch("reporting.service.report_submission_service.ReportVerificationService.get_report_needs_verification") + @patch("reporting.models.report_attachment.ReportAttachment.objects.get") + def test_validate_report_without_verification_statement(self, mock_get_attachment, mock_get_verification): + # Arrange + version_id = 1 + mock_get_verification.return_value = False # Verification statement is not mandatory + + # Act + ReportSubmissionService.validate_report(version_id) + + # Assert + mock_get_attachment.assert_not_called() + + @patch("reporting.service.report_submission_service.ReportVerificationService.get_report_needs_verification") + @patch("reporting.models.report_attachment.ReportAttachment.objects.get") + def test_validate_report_raises_exception_if_verification_missing(self, mock_get_attachment, mock_get_verification): + # Arrange + version_id = 1 + mock_get_verification.return_value = True # Verification statement is mandatory + mock_get_attachment.side_effect = ReportAttachment.DoesNotExist + + # Act & Assert + with pytest.raises(Exception, match="verification_statement"): + ReportSubmissionService.validate_report(version_id) + + @patch("reporting.models.report_version.ReportVersion.objects.get") + @patch("reporting.service.report_submission_service.ReportSubmissionService.validate_report") + def test_submit_report(self, mock_validate_report, mock_get_report_version): + # Arrange + version_id = 1 + user_guid = UUID("12345678-1234-5678-1234-567812345678") + + mock_report_version = MagicMock() + mock_get_report_version.return_value = mock_report_version + + # Act + result = ReportSubmissionService.submit_report(version_id, user_guid) + + # Assert + mock_validate_report.assert_called_once_with(version_id) + mock_get_report_version.assert_called_once_with(id=version_id) + mock_report_version.set_create_or_update.assert_called_once_with(user_guid) + assert mock_report_version.status == ReportVersion.ReportVersionStatus.Submitted + mock_report_version.save.assert_called_once() + assert result == mock_report_version diff --git a/bc_obps/reporting/tests/service/test_report_verification_service.py b/bc_obps/reporting/tests/service/test_report_verification_service.py index a58c882e0c..d5690ef678 100644 --- a/bc_obps/reporting/tests/service/test_report_verification_service.py +++ b/bc_obps/reporting/tests/service/test_report_verification_service.py @@ -91,7 +91,7 @@ def test_get_report_needs_verification_returns_false_for_non_regulated_purpose( """ # Arrange: Simulate a purpose that is not in REGULATED_OPERATION_PURPOSES and NOT Operation.Purposes.REPORTING_OPERATION - mock_get_registration_purpose.return_value = "Electricity Import Operation" + mock_get_registration_purpose.return_value = Operation.Purposes.ELECTRICITY_IMPORT_OPERATION # Act: Call the method to determine if the report needs verification result = ReportVerificationService.get_report_needs_verification(self.report_version.id)