Skip to content

Commit

Permalink
fix: backend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-hashimoto committed Jan 2, 2025
1 parent 91b4d3b commit 031ebdf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
4 changes: 3 additions & 1 deletion backend/lcfs/tests/fuel_supply/test_fuel_supplies_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ async def test_check_duplicate(fuel_supply_repo, mock_db_session):
compliance_report_id=1,
fuel_type_id=1,
fuel_category_id=1,
end_use_id=24,
provision_of_the_act_id=1,
quantity=1000,
units="L",
Expand All @@ -82,7 +83,8 @@ async def test_check_duplicate(fuel_supply_repo, mock_db_session):
# Set up the mock chain using regular MagicMock since the chained methods are sync
mock_result_chain = MagicMock()
mock_result_chain.scalars = MagicMock(return_value=mock_result_chain)
mock_result_chain.first = MagicMock(return_value=MagicMock(spec=FuelSupply))
mock_result_chain.first = MagicMock(
return_value=MagicMock(spec=FuelSupply))

# Define an async execute function that returns our mock chain
async def mock_execute(*args, **kwargs):
Expand Down
23 changes: 16 additions & 7 deletions backend/lcfs/tests/fuel_supply/test_fuel_supplies_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ def fuel_supply_service():
@pytest.mark.anyio
async def test_get_fuel_supply_options(fuel_supply_service):
service, mock_repo, mock_fuel_code_repo = fuel_supply_service
mock_repo.get_fuel_supply_table_options = AsyncMock(return_value={"fuel_types": []})
mock_repo.get_fuel_supply_table_options = AsyncMock(
return_value={"fuel_types": []})
compliance_period = "2023"

response = await service.get_fuel_supply_options(compliance_period)

assert isinstance(response, FuelTypeOptionsResponse)
mock_repo.get_fuel_supply_table_options.assert_awaited_once_with(compliance_period)
mock_repo.get_fuel_supply_table_options.assert_awaited_once_with(
compliance_period)


# Asynchronous test for get_fuel_supply_list
Expand All @@ -90,7 +92,8 @@ async def test_get_fuel_supply_list(fuel_supply_service):
response = await service.get_fuel_supply_list(compliance_report_id)

assert isinstance(response, FuelSuppliesSchema)
mock_repo.get_fuel_supply_list.assert_awaited_once_with(compliance_report_id)
mock_repo.get_fuel_supply_list.assert_awaited_once_with(
compliance_report_id)


@pytest.mark.anyio
Expand All @@ -102,6 +105,7 @@ async def test_update_fuel_supply_not_found(fuel_supply_action_service):
compliance_report_id=1,
fuel_type_id=1,
fuel_category_id=1,
end_use_id=24,
provision_of_the_act_id=1,
quantity=2000,
units="L",
Expand Down Expand Up @@ -274,8 +278,10 @@ async def test_create_fuel_supply(fuel_supply_action_service):
"fuelCode": "FUEL123",
"carbonIntensity": 15.0,
},
provisionOfTheAct={"provisionOfTheActId": 1, "name": "Act Provision"},
endUseType={"endUseTypeId": 1, "type": "Transport", "subType": "Personal"},
provisionOfTheAct={"provisionOfTheActId": 1,
"name": "Act Provision"},
endUseType={"endUseTypeId": 1,
"type": "Transport", "subType": "Personal"},
units="L",
compliancePeriod="2024",
)
Expand All @@ -290,7 +296,8 @@ async def test_create_fuel_supply(fuel_supply_action_service):
)
mock_density = MagicMock(spec=EnergyDensity)
mock_density.density = 30.0
mock_fuel_code_repo.get_energy_density = AsyncMock(return_value=mock_density)
mock_fuel_code_repo.get_energy_density = AsyncMock(
return_value=mock_density)

user_type = UserTypeEnum.SUPPLIER

Expand All @@ -315,6 +322,7 @@ async def test_delete_fuel_supply(fuel_supply_action_service):
group_uuid="some-uuid",
fuel_type_id=1,
fuel_category_id=1,
end_use_id=24,
provision_of_the_act_id=1,
quantity=1000,
units="L",
Expand All @@ -338,5 +346,6 @@ async def test_delete_fuel_supply(fuel_supply_action_service):

assert response.success is True
assert response.message == "Marked as deleted."
mock_repo.get_latest_fuel_supply_by_group_uuid.assert_awaited_once_with("some-uuid")
mock_repo.get_latest_fuel_supply_by_group_uuid.assert_awaited_once_with(
"some-uuid")
mock_repo.create_fuel_supply.assert_awaited_once()
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ async def test_check_duplicate(fuel_supply_validation):
compliance_report_id=1,
fuel_type_id=1,
fuel_category_id=1,
end_use_id=24,
provision_of_the_act_id=1,
quantity=2000,
units="L",
Expand All @@ -54,6 +55,7 @@ async def test_validate_other_recognized_type(fuel_supply_validation):
compliance_report_id=1,
fuel_type_id=1, # Some recognized type ID
fuel_category_id=1,
end_use_id=24,
provision_of_the_act_id=1,
quantity=2000,
units="L",
Expand All @@ -76,6 +78,7 @@ async def test_validate_other_unrecognized_type_with_other(fuel_supply_validatio
compliance_report_id=1,
fuel_type_id=99, # Assume 99 is unrecognized "Other" type
fuel_category_id=1,
end_use_id=24,
provision_of_the_act_id=1,
quantity=2000,
units="L",
Expand All @@ -99,6 +102,7 @@ async def test_validate_other_unrecognized_type_missing_other(fuel_supply_valida
compliance_report_id=1,
fuel_type_id=99, # Assume 99 is unrecognized "Other" type
fuel_category_id=1,
end_use_id=24,
provision_of_the_act_id=1,
quantity=2000,
units="L",
Expand Down
6 changes: 5 additions & 1 deletion backend/lcfs/tests/fuel_supply/test_fuel_supplies_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ async def test_save_fuel_supply_row_create(
"compliance_report_id": 1,
"fuel_type_id": 1,
"fuel_category_id": 1,
"end_use_id": 24,
"provision_of_the_act_id": 1,
"quantity": 1000,
"units": "L",
Expand Down Expand Up @@ -104,6 +105,7 @@ async def test_save_fuel_supply_row_update(
"fuel_supply_id": 123,
"fuel_type_id": 1,
"fuel_category_id": 1,
"end_use_id": 24,
"provision_of_the_act_id": 1,
"quantity": 1000,
"units": "L",
Expand Down Expand Up @@ -160,6 +162,7 @@ async def test_save_fuel_supply_row_delete(
"fuel_supply_id": 123,
"fuel_type_id": 1,
"fuel_category_id": 1,
"end_use_id": 24,
"provision_of_the_act_id": 1,
"quantity": 1000,
"units": "L",
Expand Down Expand Up @@ -191,7 +194,8 @@ async def test_save_fuel_supply_row_delete(

assert response.status_code == status.HTTP_201_CREATED
data = response.json()
assert data == {"success": True, "message": "Fuel supply row deleted successfully"}
assert data == {"success": True,
"message": "Fuel supply row deleted successfully"}


@pytest.mark.anyio
Expand Down

0 comments on commit 031ebdf

Please sign in to comment.