Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: changelog edit mode #1988

Open
wants to merge 16 commits into
base: release-1.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"version",
"action_type",
"units",
'is_new_supplemental_entry',
}

# Example test cases from the dataset
Expand Down Expand Up @@ -375,46 +376,6 @@ async def test_delete_fuel_export_success(
mock_repo.create_fuel_export.assert_awaited_once()


@pytest.mark.anyio
async def test_delete_fuel_export_already_deleted(
fuel_export_action_service, mock_repo, mock_fuel_code_repo
):
fe_data = create_sample_fe_data()
user_type = UserTypeEnum.SUPPLIER

# Exclude invalid fields
fe_data_dict = fe_data.model_dump(exclude=FUEL_EXPORT_EXCLUDE_FIELDS)

# Mock existing export already marked as deleted
existing_export = FuelExport(
**fe_data_dict,
fuel_export_id=1,
version=1,
action_type=ActionTypeEnum.DELETE,
)
existing_export.compliance_units = 0
existing_export.fuel_type = {
"fuel_type_id": 3,
"fuel_type": "Electricity",
"units": "kWh",
}
existing_export.fuel_category = {"category": "Diesel"}
existing_export.units = "Litres"
mock_repo.get_latest_fuel_export_by_group_uuid.return_value = existing_export

# Call the method under test
result = await fuel_export_action_service.delete_fuel_export(fe_data, user_type)

# Assertions
assert isinstance(result, DeleteFuelExportResponseSchema)
assert result.success is True
assert result.message == "Fuel export record already deleted."
mock_repo.get_latest_fuel_export_by_group_uuid.assert_awaited_once_with(
fe_data.group_uuid
)
mock_repo.create_fuel_export.assert_not_awaited()


@pytest.mark.anyio
async def test_populate_fuel_export_fields(
fuel_export_action_service, mock_fuel_code_repo
Expand Down Expand Up @@ -448,7 +409,8 @@ async def test_populate_fuel_export_fields(
assert populated_export.eer == 1 # Default EER
assert populated_export.energy_density == fe_data.energy_density
# Energy calculation
assert populated_export.energy == round(fe_data.energy_density * fe_data.quantity)
assert populated_export.energy == round(
fe_data.energy_density * fe_data.quantity)
# Compliance units calculation (should be negative)
assert populated_export.compliance_units < 0

Expand Down Expand Up @@ -479,7 +441,8 @@ async def test_compliance_units_calculation(
group_uuid=str(uuid4()),
version=0,
provisionOfTheActId=123,
provisionOfTheAct={"provision_of_the_act_id": 123, "name": "Test Provision"},
provisionOfTheAct={"provision_of_the_act_id": 123,
"name": "Test Provision"},
exportDate=datetime.now().date(),
)

Expand Down
20 changes: 13 additions & 7 deletions backend/lcfs/tests/fuel_export/test_fuel_exports_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ async def test_get_fuel_export_list_success(fuel_export_service, mock_repo):
export_date=date.today(),
group_uuid="test-uuid",
provision_of_the_act_id=1,
provision_of_the_act={"provision_of_the_act_id": 1, "name": "Test Provision"},
provision_of_the_act={
"provision_of_the_act_id": 1, "name": "Test Provision"},
version=0,
user_type=UserTypeEnum.SUPPLIER,
action_type=ActionTypeEnum.CREATE,
Expand All @@ -83,7 +84,7 @@ async def test_get_fuel_export_list_success(fuel_export_service, mock_repo):
assert isinstance(result, FuelExportsSchema)
# Expect the repo call to include exclude_draft_reports=True based on the user
mock_repo.get_fuel_export_list.assert_called_once_with(
1, exclude_draft_reports=True
1, False, exclude_draft_reports=True
)


Expand All @@ -106,7 +107,8 @@ async def test_get_fuel_exports_paginated_success(fuel_export_service, mock_repo
units="L",
export_date=date.today(),
provision_of_the_act_id=1,
provision_of_the_act={"provision_of_the_act_id": 1, "name": "Test Provision"},
provision_of_the_act={
"provision_of_the_act_id": 1, "name": "Test Provision"},
)
mock_repo.get_fuel_exports_paginated.return_value = ([mock_export], 1)

Expand Down Expand Up @@ -138,7 +140,8 @@ async def test_action_create_fuel_export_success(fuel_export_action_service, moc
fuel_category_id=1,
fuel_category=mock_fuel_category.dict(),
provisionOfTheActId=1,
provisionOfTheAct={"provision_of_the_act_id": 1, "name": "Act Provision"},
provisionOfTheAct={"provision_of_the_act_id": 1,
"name": "Act Provision"},
quantity=100,
units="L",
export_date=date.today(),
Expand All @@ -153,7 +156,8 @@ async def test_action_create_fuel_export_success(fuel_export_action_service, moc
user_type=UserTypeEnum.SUPPLIER,
action_type=ActionTypeEnum.CREATE,
provision_of_the_act_id=1,
provision_of_the_act={"provision_of_the_act_id": 1, "name": "Act Provision"},
provision_of_the_act={
"provision_of_the_act_id": 1, "name": "Act Provision"},
fuel_type_id=1,
fuel_category_id=1,
quantity=100,
Expand Down Expand Up @@ -184,7 +188,8 @@ async def test_action_update_fuel_export_success(fuel_export_action_service, moc
fuel_category=mock_fuel_category.dict(),
quantity=100,
provisionOfTheActId=1,
provisionOfTheAct={"provision_of_the_act_id": 1, "name": "Act Provision"},
provisionOfTheAct={"provision_of_the_act_id": 1,
"name": "Act Provision"},
units="L",
export_date=date.today(),
compliance_period="2024",
Expand All @@ -200,7 +205,8 @@ async def test_action_update_fuel_export_success(fuel_export_action_service, moc
fuel_type_id=1,
fuel_category_id=1,
provision_of_the_act_id=1,
provision_of_the_act={"provision_of_the_act_id": 1, "name": "Act Provision"},
provision_of_the_act={
"provision_of_the_act_id": 1, "name": "Act Provision"},
quantity=100,
units="L",
export_date=date.today(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"version",
"action_type",
"units",
'is_new_supplemental_entry',
}

# Example test cases from the dataset
Expand Down Expand Up @@ -426,46 +427,6 @@ async def test_delete_fuel_supply_success(
mock_repo.create_fuel_supply.assert_awaited_once()


@pytest.mark.anyio
async def test_delete_fuel_supply_already_deleted(
fuel_supply_action_service, mock_repo, mock_fuel_code_repo
):
fe_data = create_sample_fs_data()
user_type = UserTypeEnum.SUPPLIER

# Exclude invalid fields
fe_data_dict = fe_data.model_dump(exclude=FUEL_SUPPLY_EXCLUDE_FIELDS)

# Mock existing supply already marked as deleted
existing_supply = FuelSupply(
**fe_data_dict,
fuel_supply_id=1,
version=1,
action_type=ActionTypeEnum.DELETE,
)
existing_supply.compliance_units = 0
existing_supply.fuel_type = {
"fuel_type_id": 3,
"fuel_type": "Electricity",
"units": "kWh",
}
existing_supply.fuel_category = {"category": "Diesel"}
existing_supply.units = "Litres"
mock_repo.get_latest_fuel_supply_by_group_uuid.return_value = existing_supply

# Call the method under test
result = await fuel_supply_action_service.delete_fuel_supply(fe_data, user_type)

# Assertions
assert isinstance(result, DeleteFuelSupplyResponseSchema)
assert result.success is True
assert result.message == "Already deleted."
mock_repo.get_latest_fuel_supply_by_group_uuid.assert_awaited_once_with(
fe_data.group_uuid
)
mock_repo.create_fuel_supply.assert_not_awaited()


@pytest.mark.anyio
async def test_populate_fuel_supply_fields(
fuel_supply_action_service, mock_fuel_code_repo
Expand Down Expand Up @@ -499,7 +460,8 @@ async def test_populate_fuel_supply_fields(
assert populated_supply.eer == 1 # Default EER
assert populated_supply.energy_density == fe_data.energy_density
# Energy calculation
assert populated_supply.energy == round(fe_data.energy_density * fe_data.quantity)
assert populated_supply.energy == round(
fe_data.energy_density * fe_data.quantity)
assert populated_supply.compliance_units > 0

mock_fuel_code_repo.get_standardized_fuel_data.assert_awaited_once_with(
Expand Down Expand Up @@ -592,7 +554,8 @@ def test_calculate_compliance_units(case):
"""
# Extract input parameters
quantity = case["input"]["quantity"]
units = case["input"]["units"] # Not used in calculation but included for context
# Not used in calculation but included for context
units = case["input"]["units"]
target_ci = case["input"]["target_ci"]
ci_of_fuel = case["input"]["ci_of_fuel"]
energy_density = case["input"]["energy_density"]
Expand Down
21 changes: 14 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 @@ -69,13 +69,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)


@pytest.mark.anyio
Expand Down Expand Up @@ -113,7 +115,8 @@ async def test_get_fuel_supply_list(fuel_supply_service):
}

# Set the repository method to return the valid fuel supply record.
mock_repo.get_fuel_supply_list = AsyncMock(return_value=[valid_fuel_supply])
mock_repo.get_fuel_supply_list = AsyncMock(
return_value=[valid_fuel_supply])

compliance_report_id = 1
response = await service.get_fuel_supply_list(compliance_report_id)
Expand Down Expand Up @@ -304,8 +307,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 @@ -320,7 +325,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 Down Expand Up @@ -369,5 +375,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()
26 changes: 13 additions & 13 deletions backend/lcfs/web/api/fuel_export/actions_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"version",
"action_type",
"units",
"is_new_supplemental_entry",
}


Expand Down Expand Up @@ -202,22 +203,21 @@ async def delete_fuel_export(
fe_data.group_uuid
)

if existing_export and existing_export.action_type == ActionTypeEnum.DELETE:
if fe_data.is_new_supplemental_entry:
await self.repo.delete_fuel_export(fuel_export_id=fe_data.fuel_export_id)
return DeleteFuelExportResponseSchema(
success=True, message="Fuel export record already deleted."
success=True, message="Marked as deleted."
)
else:
delete_export = FuelExport(
compliance_report_id=fe_data.compliance_report_id,
group_uuid=fe_data.group_uuid,
version=(existing_export.version +
1) if existing_export else 0,
action_type=ActionTypeEnum.DELETE,
user_type=user_type,
)

# Create a new version with action_type DELETE
delete_export = FuelExport(
compliance_report_id=fe_data.compliance_report_id,
group_uuid=fe_data.group_uuid,
version=(existing_export.version + 1) if existing_export else 0,
action_type=ActionTypeEnum.DELETE,
user_type=user_type,
)

# Copy over necessary fields from the latest version
if existing_export:
for field in existing_export.__table__.columns.keys():
if field not in FUEL_EXPORT_EXCLUDE_FIELDS:
setattr(delete_export, field, getattr(
Expand Down
Loading