Skip to content

Commit

Permalink
feat: Add can_sign to compliance summary
Browse files Browse the repository at this point in the history
* Add new UI for SigningAuthorityDeclaration to have a disabled state with a warning message
* Calculate can_sign based on number of records in each schedule and use to set flag
* Update submit logic to check the same flag and raise an error
* Update compliance summary calculation to only call get effective records one
* Update / Add tests accordingly
* Skip creating / updating transaction when final total is 0
  • Loading branch information
dhaselhan committed Nov 18, 2024
1 parent bea27fc commit 379fd11
Show file tree
Hide file tree
Showing 13 changed files with 295 additions and 45 deletions.
2 changes: 2 additions & 0 deletions backend/lcfs/tests/compliance_report/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ def _create_compliance_report_summary(
version=1,
is_locked=False,
quarter=None,
can_sign=False
):

return ComplianceReportSummarySchema(
Expand All @@ -206,6 +207,7 @@ def _create_compliance_report_summary(
version=version,
is_locked=is_locked,
quarter=quarter,
can_sign=can_sign
)

return _create_compliance_report_summary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,8 @@ async def test_calculate_fuel_quantities_success_not_fossil_derived(
)

result = await compliance_report_repo.calculate_fuel_quantities(
compliance_report=compliance_reports[0],
compliance_report_id=compliance_reports[0].compliance_report_id,
effective_fuel_supplies=non_fossil_fuel_supplies,
fossil_derived=False,
)

Expand All @@ -965,7 +966,8 @@ async def test_calculate_fuel_quantities_success_fossil_derived(
)

result = await compliance_report_repo.calculate_fuel_quantities(
compliance_report=compliance_reports[0],
compliance_report_id=compliance_reports[0].compliance_report_id,
effective_fuel_supplies=fossil_fuel_supplies,
fossil_derived=True,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from lcfs.web.api.base import FilterModel
from lcfs.web.api.compliance_report.schema import (
ComplianceReportUpdateSchema,
ComplianceReportSummaryUpdateSchema,
)
from lcfs.services.s3.client import DocumentService

Expand Down Expand Up @@ -329,6 +330,7 @@ async def test_get_compliance_report_summary_success(
mock_calculate_compliance_report_summary.assert_called_once_with(1)
mock_validate_organization_access.assert_called_once_with(1)


@pytest.mark.anyio
async def test_get_compliance_report_summary_invalid_payload(
client: AsyncClient,
Expand Down Expand Up @@ -386,14 +388,21 @@ async def test_update_compliance_report_summary_success(
set_mock_user(fastapi_app, [RoleEnum.SUPPLIER])

mock_compliance_report_summary = compliance_report_summary_schema()
request_schema = ComplianceReportSummaryUpdateSchema(
compliance_report_id=1,
renewable_fuel_target_summary=mock_compliance_report_summary.renewable_fuel_target_summary,
low_carbon_fuel_target_summary=mock_compliance_report_summary.low_carbon_fuel_target_summary,
non_compliance_penalty_summary=mock_compliance_report_summary.non_compliance_penalty_summary,
summary_id=mock_compliance_report_summary.summary_id,
)
mock_validate_organization_access.return_value = None
mock_update_compliance_report_summary.return_value = (
mock_compliance_report_summary
)

url = fastapi_app.url_path_for("update_compliance_report_summary", report_id=1)

payload = mock_compliance_report_summary.dict(by_alias=True)
payload = request_schema.model_dump(by_alias=True)

response = await client.put(url, json=payload)

Expand All @@ -404,9 +413,7 @@ async def test_update_compliance_report_summary_success(
)

assert response.json() == expected_response
mock_update_compliance_report_summary.assert_called_once_with(
1, mock_compliance_report_summary
)
mock_update_compliance_report_summary.assert_called_once_with(1, request_schema)
mock_validate_organization_access.assert_called_once_with(1)


Expand Down Expand Up @@ -507,6 +514,7 @@ async def test_update_compliance_report_success(
)
mock_validate_organization_access.assert_called_once_with(1)


@pytest.mark.anyio
async def test_update_compliance_report_forbidden(
client: AsyncClient,
Expand Down
95 changes: 94 additions & 1 deletion backend/lcfs/tests/compliance_report/test_summary_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import pytest

from lcfs.db.models.compliance.ComplianceReportSummary import ComplianceReportSummary
from lcfs.web.api.compliance_report.schema import ComplianceReportSummaryRowSchema
from lcfs.web.api.compliance_report.schema import (
ComplianceReportSummaryRowSchema,
)


@pytest.mark.anyio
Expand Down Expand Up @@ -670,3 +672,94 @@ async def test_calculate_renewable_fuel_target_summary_no_copy_lines_6_and_8(
assert result[7].gasoline == 0 # Line 8 should not be copied
assert result[7].diesel == 0
assert result[7].jet_fuel == 0


@pytest.mark.anyio
async def test_can_sign_flag_logic(
compliance_report_summary_service, mock_repo, mock_trxn_repo
):
# Scenario 1: All conditions met
mock_effective_fuel_supplies = [MagicMock()]
mock_notional_transfers = MagicMock(notional_transfers=[MagicMock()])
mock_fuel_exports = [MagicMock()]
mock_allocation_agreements = [MagicMock()]
mock_compliance_report = MagicMock(
compliance_report_group_uuid="mock-group-uuid",
compliance_period=MagicMock(effective_date=MagicMock(year=2024)),
organization_id=1,
compliance_report_id=1,
summary=MagicMock(is_locked=False),
)

mock_trxn_repo.calculate_available_balance_for_period.return_value = 1000

# Mock previous retained and obligation dictionaries
previous_retained = {"gasoline": 10, "diesel": 20, "jet_fuel": 30}
previous_obligation = {"gasoline": 5, "diesel": 10, "jet_fuel": 15}

# Mock repository methods
mock_repo.get_compliance_report_by_id = AsyncMock(
return_value=mock_compliance_report
)
mock_repo.calculate_fuel_quantities = AsyncMock(
return_value={
"gasoline": 100,
"diesel": 50,
"jet_fuel": 25,
}
)
mock_repo.get_assessed_compliance_report_by_period = AsyncMock(
return_value=MagicMock(
summary=MagicMock(
line_6_renewable_fuel_retained_gasoline=previous_retained["gasoline"],
line_6_renewable_fuel_retained_diesel=previous_retained["diesel"],
line_6_renewable_fuel_retained_jet_fuel=previous_retained["jet_fuel"],
line_8_obligation_deferred_gasoline=previous_obligation["gasoline"],
line_8_obligation_deferred_diesel=previous_obligation["diesel"],
line_8_obligation_deferred_jet_fuel=previous_obligation["jet_fuel"],
)
)
)

compliance_report_summary_service.fuel_supply_repo.get_effective_fuel_supplies = (
AsyncMock(return_value=mock_effective_fuel_supplies)
)
compliance_report_summary_service.notional_transfer_service.calculate_notional_transfers = AsyncMock(
return_value=mock_notional_transfers
)
compliance_report_summary_service.fuel_export_repo.get_effective_fuel_exports = (
AsyncMock(return_value=mock_fuel_exports)
)
compliance_report_summary_service.allocation_agreement_repo.get_allocation_agreements = AsyncMock(
return_value=mock_allocation_agreements
)

# Call the method
result = (
await compliance_report_summary_service.calculate_compliance_report_summary(1)
)

# Assert that `can_sign` is True
assert result.can_sign is True

# Scenario 2: No conditions met
compliance_report_summary_service.fuel_supply_repo.get_effective_fuel_supplies = (
AsyncMock(return_value=[])
)
compliance_report_summary_service.notional_transfer_service.calculate_notional_transfers = AsyncMock(
return_value=MagicMock(notional_transfers=[])
)
compliance_report_summary_service.fuel_export_repo.get_effective_fuel_exports = (
AsyncMock(return_value=[])
)
compliance_report_summary_service.allocation_agreement_repo.get_allocation_agreements = AsyncMock(
return_value=[]
)

# Call the method again
result = (
await compliance_report_summary_service.calculate_compliance_report_summary(1)
)

# Assert that `can_sign` is False
assert result.can_sign is False
Loading

0 comments on commit 379fd11

Please sign in to comment.