Skip to content

Commit

Permalink
Merge pull request #2054 from bcgov/fix/daniel-request-fixes
Browse files Browse the repository at this point in the history
fix: Remove stale self.request
  • Loading branch information
dhaselhan authored Feb 20, 2025
2 parents 471984d + 43bb5d6 commit e9bb3c5
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 107 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from datetime import datetime
import json
from unittest import mock

from lcfs.web.api.email.repo import CHESEmailRepository
import pytest

Expand Down Expand Up @@ -339,7 +341,7 @@ async def test_get_compliance_report_summary_success(
)

assert response.json() == expected_response
mock_calculate_compliance_report_summary.assert_called_once_with(1)
mock_calculate_compliance_report_summary.assert_called_once_with(1, mock.ANY)
mock_validate_organization_access.assert_called_once_with(1)


Expand Down Expand Up @@ -426,7 +428,10 @@ async def test_update_compliance_report_summary_success(
)

assert response.json() == expected_response
mock_update_compliance_report_summary.assert_called_once_with(1, request_schema)
mock_update_compliance_report_summary.assert_called_once_with(
1, request_schema, mock.ANY
)

mock_validate_organization_access.assert_called_once_with(1)


Expand Down Expand Up @@ -523,7 +528,7 @@ async def test_update_compliance_report_success(
assert response.json() == expected_response

mock_update_compliance_report.assert_called_once_with(
1, ComplianceReportUpdateSchema(**payload)
1, ComplianceReportUpdateSchema(**payload), mock.ANY
)
mock_validate_organization_access.assert_called_once_with(1)

Expand Down Expand Up @@ -623,7 +628,7 @@ async def test_update_compliance_report_draft_success(
assert response.json() == expected_response

mock_update_compliance_report.assert_called_once_with(
1, ComplianceReportUpdateSchema(**payload)
1, ComplianceReportUpdateSchema(**payload), mock.ANY
)


Expand Down Expand Up @@ -661,7 +666,7 @@ async def test_update_compliance_report_submitted_success(
assert response.json() == expected_response

mock_update_compliance_report.assert_called_once_with(
1, ComplianceReportUpdateSchema(**payload)
1, ComplianceReportUpdateSchema(**payload), mock.ANY
)


Expand Down Expand Up @@ -702,7 +707,7 @@ async def test_update_compliance_report_recommended_by_analyst_success(
assert response.json() == expected_response

mock_update_compliance_report.assert_called_once_with(
1, ComplianceReportUpdateSchema(**payload)
1, ComplianceReportUpdateSchema(**payload), mock.ANY
)


Expand Down Expand Up @@ -743,7 +748,7 @@ async def test_update_compliance_report_recommended_by_manager_success(
assert response.json() == expected_response

mock_update_compliance_report.assert_called_once_with(
1, ComplianceReportUpdateSchema(**payload)
1, ComplianceReportUpdateSchema(**payload), mock.ANY
)


Expand Down Expand Up @@ -781,5 +786,5 @@ async def test_update_compliance_report_assessed_success(
assert response.json() == expected_response

mock_update_compliance_report.assert_called_once_with(
1, ComplianceReportUpdateSchema(**payload)
1, ComplianceReportUpdateSchema(**payload), mock.ANY
)
10 changes: 7 additions & 3 deletions backend/lcfs/tests/compliance_report/test_summary_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from lcfs.db.models import FuelSupply, ComplianceReport
from lcfs.db.models import FuelSupply, ComplianceReport, UserProfile
from lcfs.db.models.compliance.ComplianceReportSummary import ComplianceReportSummary
from lcfs.web.api.compliance_report.schema import (
ComplianceReportSummaryRowSchema,
Expand Down Expand Up @@ -842,7 +842,9 @@ async def test_can_sign_flag_logic(

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

# Assert that `can_sign` is True
Expand All @@ -864,7 +866,9 @@ async def test_can_sign_flag_logic(

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

# Assert that `can_sign` is False
Expand Down
70 changes: 46 additions & 24 deletions backend/lcfs/tests/compliance_report/test_update_service.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from unittest import mock
from unittest.mock import AsyncMock, MagicMock, patch

import pytest
from fastapi import HTTPException

from lcfs.db.models import UserProfile
from lcfs.db.models.compliance.ComplianceReport import ComplianceReport
from lcfs.db.models.compliance.ComplianceReportStatus import (
ComplianceReportStatus,
Expand Down Expand Up @@ -87,9 +89,9 @@ async def test_update_compliance_report_status_change(
mock_repo.update_compliance_report.return_value = mock_report
compliance_report_update_service._perform_notification_call = AsyncMock()

# Call the method
# Call the method (updated to pass a user profile; in this test we use mock.ANY)
updated_report = await compliance_report_update_service.update_compliance_report(
report_id, report_data
report_id, report_data, mock.ANY
)

# Assertions
Expand All @@ -101,14 +103,14 @@ async def test_update_compliance_report_status_change(
report_data.status
)
compliance_report_update_service.handle_status_change.assert_called_once_with(
mock_report, new_status.status
mock_report, new_status.status, mock.ANY
)
mock_repo.add_compliance_report_history.assert_called_once_with(
mock_report, compliance_report_update_service.request.user
mock_report, UserProfile()
)
mock_repo.update_compliance_report.assert_called_once_with(mock_report)
compliance_report_update_service._perform_notification_call.assert_called_once_with(
mock_report, "Submitted"
mock_report, "Submitted", UserProfile()
)


Expand Down Expand Up @@ -139,15 +141,15 @@ async def test_update_compliance_report_no_status_change(
mock_repo.update_compliance_report.return_value = mock_report
compliance_report_update_service._perform_notification_call = AsyncMock()

# Call the method
# Call the method (now passing a UserProfile)
updated_report = await compliance_report_update_service.update_compliance_report(
report_id, report_data
report_id, report_data, UserProfile()
)

# Assertions
assert updated_report == mock_report
compliance_report_update_service._perform_notification_call.assert_called_once_with(
mock_report, "Draft"
mock_report, "Draft", mock.ANY
)
mock_repo.update_compliance_report.assert_called_once_with(mock_report)

Expand All @@ -165,10 +167,10 @@ async def test_update_compliance_report_not_found(
# Set up mocks
mock_repo.get_compliance_report_by_id.return_value = None

# Call the method and check for exception
# Call the method and check for exception (UserProfile now passed)
with pytest.raises(DataNotFoundException):
await compliance_report_update_service.update_compliance_report(
report_id, report_data
report_id, report_data, UserProfile()
)

mock_repo.get_compliance_report_by_id.assert_called_once_with(
Expand All @@ -190,7 +192,9 @@ async def test_handle_submitted_status_insufficient_permissions(

# Call the method and check for exception
with pytest.raises(HTTPException) as exc_info:
await compliance_report_update_service.handle_submitted_status(mock_report)
await compliance_report_update_service.handle_submitted_status(
mock_report, UserProfile()
)

assert exc_info.value.status_code == 403
assert exc_info.value.detail == "Forbidden."
Expand Down Expand Up @@ -271,19 +275,21 @@ async def test_handle_submitted_status_with_existing_summary(
mock_org_service.adjust_balance.return_value = MagicMock()

# Call the method
await compliance_report_update_service.handle_submitted_status(mock_report)
await compliance_report_update_service.handle_submitted_status(
mock_report, UserProfile()
)

# Assertions
mock_user_has_roles.assert_called_once_with(
compliance_report_update_service.request.user,
mock.ANY,
[RoleEnum.SUPPLIER, RoleEnum.SIGNING_AUTHORITY],
)
mock_repo.get_summary_by_report_id.assert_called_once_with(report_id)
compliance_report_summary_service.calculate_compliance_report_summary.assert_called_once_with(
report_id
report_id, mock.ANY
)

# Check if the summary is locked
# Check if the summary is locked by verifying that the save call includes a UserProfile
saved_summary = mock_repo.save_compliance_report_summary.call_args[0][0]
assert saved_summary.is_locked == True

Expand Down Expand Up @@ -355,16 +361,18 @@ async def test_handle_submitted_status_without_existing_summary(
# Mock the adjust_balance method to return a mocked transaction result
mock_org_service.adjust_balance.return_value = MagicMock()
# Call the method
await compliance_report_update_service.handle_submitted_status(mock_report)
await compliance_report_update_service.handle_submitted_status(
mock_report, UserProfile()
)

# Assertions
mock_repo.get_summary_by_report_id.assert_called_once_with(report_id)
compliance_report_summary_service.calculate_compliance_report_summary.assert_called_once_with(
report_id
report_id, mock.ANY
)

# Check if a new summary is created
mock_repo.add_compliance_report_summary.assert_called_once()
mock_repo.add_compliance_report_summary.assert_called_once_with(mock.ANY)
new_summary = mock_repo.add_compliance_report_summary.call_args[0][0]

# Check if calculated values are used
Expand Down Expand Up @@ -450,9 +458,12 @@ async def test_handle_submitted_status_partial_existing_values(
# Mock the adjust_balance method to return a mocked transaction result
mock_org_service.adjust_balance.return_value = MagicMock()
# Call the method
await compliance_report_update_service.handle_submitted_status(mock_report)
await compliance_report_update_service.handle_submitted_status(
mock_report, UserProfile()
)

# Assertions
mock_repo.save_compliance_report_summary.assert_called_once_with(mock.ANY)
saved_summary = mock_repo.save_compliance_report_summary.call_args[0][0]
assert (
saved_summary.renewable_fuel_target_summary[0].gasoline == 1000
Expand Down Expand Up @@ -540,9 +551,12 @@ async def test_handle_submitted_status_no_user_edits(
# Mock the adjust_balance method to return a mocked transaction result
mock_org_service.adjust_balance.return_value = MagicMock()
# Call the method
await compliance_report_update_service.handle_submitted_status(mock_report)
await compliance_report_update_service.handle_submitted_status(
mock_report, UserProfile()
)

# Assertions
mock_repo.save_compliance_report_summary.assert_called_once_with(mock.ANY)
saved_summary = mock_repo.save_compliance_report_summary.call_args[0][0]
assert (
saved_summary.renewable_fuel_target_summary[0].gasoline == 100
Expand Down Expand Up @@ -595,7 +609,9 @@ async def test_handle_submitted_no_sign(
compliance_report_update_service.org_service = mock_org_service

with pytest.raises(ServiceException):
await compliance_report_update_service.handle_submitted_status(mock_report)
await compliance_report_update_service.handle_submitted_status(
mock_report, UserProfile()
)


@pytest.mark.anyio
Expand Down Expand Up @@ -647,7 +663,9 @@ async def test_handle_submitted_status_no_credits(
mock_org_service.adjust_balance = AsyncMock()

# Execute
await compliance_report_update_service.handle_submitted_status(mock_report)
await compliance_report_update_service.handle_submitted_status(
mock_report, UserProfile()
)

# Assertions:
# 1) We did NOT call adjust_balance, because balance = 0
Expand Down Expand Up @@ -708,7 +726,9 @@ async def test_handle_submitted_status_insufficient_credits(
mock_org_service.adjust_balance.return_value = mock_transaction

# Execute
await compliance_report_update_service.handle_submitted_status(mock_report)
await compliance_report_update_service.handle_submitted_status(
mock_report, UserProfile()
)

# We should have called adjust_balance with -50 units (reserving partial)
mock_org_service.adjust_balance.assert_awaited_once_with(
Expand Down Expand Up @@ -770,7 +790,9 @@ async def test_handle_submitted_status_sufficient_credits(
mock_org_service.adjust_balance.return_value = mock_transaction

# Execute
await compliance_report_update_service.handle_submitted_status(mock_report)
await compliance_report_update_service.handle_submitted_status(
mock_report, UserProfile()
)

# We should have called adjust_balance with the full -100
mock_org_service.adjust_balance.assert_awaited_once_with(
Expand Down
11 changes: 5 additions & 6 deletions backend/lcfs/web/api/compliance_report/summary_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from lcfs.utils.constants import LCFS_Constants
from sqlalchemy import inspect

from lcfs.db.models import FuelSupply
from lcfs.db.models import FuelSupply, UserProfile
from lcfs.db.models.compliance.ComplianceReport import ComplianceReport
from lcfs.db.models.compliance.ComplianceReportSummary import ComplianceReportSummary
from lcfs.db.models.compliance.OtherUses import OtherUses
Expand Down Expand Up @@ -381,18 +381,19 @@ async def update_compliance_report_summary(
self,
report_id: int,
summary_data: ComplianceReportSummaryUpdateSchema,
user: UserProfile,
) -> ComplianceReportSummarySchema:
"""
Autosave compliance report summary details for a specific summary by ID.
"""
await self.repo.save_compliance_report_summary(summary_data)
summary_data = await self.calculate_compliance_report_summary(report_id)
summary_data = await self.calculate_compliance_report_summary(report_id, user)

return summary_data

@service_handler
async def calculate_compliance_report_summary(
self, report_id: int
self, report_id: int, user: UserProfile
) -> ComplianceReportSummarySchema:
"""Several fields on Report Summary are Transient until locked, this function will re-calculate fields as necessary"""
# TODO this method will have to be updated to handle supplemental reports
Expand Down Expand Up @@ -454,9 +455,7 @@ async def calculate_compliance_report_summary(
}

notional_transfers = (
await self.notional_transfer_service.get_notional_transfers(
compliance_report_id=report_id
)
await self.notional_transfer_service.get_notional_transfers(report_id, user)
)

notional_transfers_sums = {"gasoline": 0, "diesel": 0, "jet_fuel": 0}
Expand Down
Loading

0 comments on commit e9bb3c5

Please sign in to comment.