Skip to content

Commit

Permalink
correct test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
prv-proton committed Dec 17, 2024
1 parent b03c127 commit d8fb658
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 27 deletions.
26 changes: 8 additions & 18 deletions backend/lcfs/tests/compliance_report/test_update_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ async def test_update_compliance_report_status_change(
mock_report.current_status.status = ComplianceReportStatusEnum.Draft
mock_report.compliance_period = MagicMock()
mock_report.compliance_period.description = "2024"
mock_report.transaction_id = 123

new_status = MagicMock(spec=ComplianceReportStatus)
new_status.status = ComplianceReportStatusEnum.Submitted
Expand All @@ -82,7 +83,7 @@ async def test_update_compliance_report_status_change(
mock_repo.get_compliance_report_status_by_desc.return_value = new_status
compliance_report_update_service.handle_status_change = AsyncMock()
mock_repo.update_compliance_report.return_value = mock_report
compliance_report_update_service._perform_notificaiton_call = AsyncMock()
compliance_report_update_service._perform_notification_call = AsyncMock()

# Call the method
updated_report = await compliance_report_update_service.update_compliance_report(
Expand All @@ -104,7 +105,7 @@ async def test_update_compliance_report_status_change(
mock_report, compliance_report_update_service.request.user
)
mock_repo.update_compliance_report.assert_called_once_with(mock_report)
compliance_report_update_service._perform_notificaiton_call.assert_called_once_with(
compliance_report_update_service._perform_notification_call.assert_called_once_with(
mock_report, "Submitted"
)

Expand All @@ -119,11 +120,11 @@ async def test_update_compliance_report_no_status_change(
mock_report.compliance_report_id = report_id
mock_report.current_status = MagicMock(spec=ComplianceReportStatus)
mock_report.current_status.status = ComplianceReportStatusEnum.Draft

# Fix for JSON serialization
mock_report.compliance_period = MagicMock()
mock_report.compliance_period.description = "2024"
mock_report.transaction_id = 123

# Status does not change
report_data = ComplianceReportUpdateSchema(
status="Draft", supplemental_note="Test note"
)
Expand All @@ -134,10 +135,7 @@ async def test_update_compliance_report_no_status_change(
mock_report.current_status
)
mock_repo.update_compliance_report.return_value = mock_report

# Mock the handle_status_change method
compliance_report_update_service.handle_status_change = AsyncMock()
compliance_report_update_service._perform_notificaiton_call = AsyncMock()
compliance_report_update_service._perform_notification_call = AsyncMock()

# Call the method
updated_report = await compliance_report_update_service.update_compliance_report(
Expand All @@ -146,18 +144,10 @@ async def test_update_compliance_report_no_status_change(

# Assertions
assert updated_report == mock_report
mock_repo.get_compliance_report_by_id.assert_called_once_with(
report_id, is_model=True
)
mock_repo.get_compliance_report_status_by_desc.assert_called_once_with(
report_data.status
)
compliance_report_update_service.handle_status_change.assert_not_called()
mock_repo.add_compliance_report_history.assert_not_called()
mock_repo.update_compliance_report.assert_called_once_with(mock_report)
compliance_report_update_service._perform_notificaiton_call.assert_called_once_with(
compliance_report_update_service._perform_notification_call.assert_called_once_with(
mock_report, "Draft"
)
mock_repo.update_compliance_report.assert_called_once_with(mock_report)


@pytest.mark.anyio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@ async def test_create_initiative_agreement(service, mock_repo, mock_request):
internal_comment=None,
)

# Mock _perform_notificaiton_call to isolate it
service._perform_notificaiton_call = AsyncMock()
# Mock _perform_notification_call to isolate it
service._perform_notification_call = AsyncMock()

# Call the service method
result = await service.create_initiative_agreement(create_data)

# Assertions
assert result == mock_initiative_agreement
mock_repo.create_initiative_agreement.assert_called_once()
service._perform_notificaiton_call.assert_called_once_with(
service._perform_notification_call.assert_called_once_with(
mock_initiative_agreement
)

Expand Down
12 changes: 6 additions & 6 deletions backend/lcfs/tests/transfer/test_transfer_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ async def test_create_transfer_success(transfer_service, mock_transfer_repo):
)
mock_transfer_repo.create_transfer.return_value = transfer_data

# Patch the _perform_notificaiton_call method
with patch.object(transfer_service, "_perform_notificaiton_call", AsyncMock()):
# Patch the _perform_notification_call method
with patch.object(transfer_service, "_perform_notification_call", AsyncMock()):
result = await transfer_service.create_transfer(transfer_data)

assert result.transfer_id == transfer_id
assert isinstance(result, TransferCreateSchema)
transfer_service._perform_notificaiton_call.assert_called_once()
transfer_service._perform_notification_call.assert_called_once()


@pytest.mark.anyio
Expand Down Expand Up @@ -121,8 +121,8 @@ async def test_update_transfer_success(
mock_transfer_repo.get_transfer_by_id.return_value = transfer
mock_transfer_repo.update_transfer.return_value = transfer

# Replace _perform_notificaiton_call with an AsyncMock
transfer_service._perform_notificaiton_call = AsyncMock()
# Replace _perform_notification_call with an AsyncMock
transfer_service._perform_notification_call = AsyncMock()

result = await transfer_service.update_transfer(transfer)

Expand All @@ -133,7 +133,7 @@ async def test_update_transfer_success(
# Verify mocks
mock_transfer_repo.get_transfer_by_id.assert_called_once_with(transfer_id)
mock_transfer_repo.update_transfer.assert_called_once_with(transfer)
transfer_service._perform_notificaiton_call.assert_awaited_once_with(
transfer_service._perform_notification_call.assert_awaited_once_with(
transfer, status="Return to analyst"
)

Expand Down

0 comments on commit d8fb658

Please sign in to comment.