Skip to content

Commit

Permalink
tests: report submission service
Browse files Browse the repository at this point in the history
chore: cleanup

chore: cleanup

chore: cleanup
  • Loading branch information
shon-button committed Dec 20, 2024
1 parent 95ea07a commit b53df0e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 5 deletions.
8 changes: 4 additions & 4 deletions bc_obps/reporting/service/report_verification_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
Expand Down
70 changes: 70 additions & 0 deletions bc_obps/reporting/tests/service/test_report_submission_service.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b53df0e

Please sign in to comment.