Skip to content

Commit

Permalink
cleaning code
Browse files Browse the repository at this point in the history
  • Loading branch information
areyeslo committed Feb 25, 2025
1 parent 40ee5e4 commit 162167d
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 37 deletions.
2 changes: 0 additions & 2 deletions backend/lcfs/db/models/fuel/FuelCategory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional
from pydantic import computed_field
from sqlalchemy import Column, Integer, Text, Enum, Numeric
from sqlalchemy.orm import relationship

Expand Down
2 changes: 0 additions & 2 deletions backend/lcfs/db/models/fuel/FuelType.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import enum
from typing import Optional

from pydantic import computed_field
from sqlalchemy import Column, Integer, Text, Boolean, Enum, Numeric, text
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ async def test_create_fuel_export_success(

# Call the method under test with compliance_period
result = await fuel_export_action_service.create_fuel_export(
fe_data, user_type, "2024"
fe_data, user_type
)
# Assertions
assert result == FuelExportSchema.model_validate(created_export)
Expand Down Expand Up @@ -238,7 +238,7 @@ async def test_update_fuel_export_success_existing_report(

# Call the method under test with compliance_period
result = await fuel_export_action_service.update_fuel_export(
fe_data, user_type, "2024"
fe_data, user_type
)

# Assertions
Expand Down Expand Up @@ -318,7 +318,7 @@ async def test_update_fuel_export_create_new_version(

# Call the method under test with compliance_period
result = await fuel_export_action_service.update_fuel_export(
fe_data, user_type, "2024"
fe_data, user_type
)

# Assertions
Expand Down Expand Up @@ -481,7 +481,6 @@ async def test_compliance_units_calculation(
case, fuel_export_action_service, mock_repo, mock_fuel_code_repo
):
# Mock repository methods
mock_repo.get_compliance_period_id = AsyncMock(return_value=1)
mock_repo.create_fuel_export = AsyncMock()
mock_repo.get_fuel_export_by_id = AsyncMock()

Expand Down Expand Up @@ -558,7 +557,7 @@ async def create_fuel_export_side_effect(fuel_export: FuelExport):

# Call service method
result = await fuel_export_action_service.create_fuel_export(
fe_data, UserTypeEnum.SUPPLIER, compliance_period=fe_data.compliance_period
fe_data, UserTypeEnum.SUPPLIER
)

# Assertions
Expand All @@ -571,9 +570,6 @@ async def create_fuel_export_side_effect(fuel_export: FuelExport):
assert result.quantity == fe_data.quantity

# Verify mock calls
mock_repo.get_compliance_period_id.assert_awaited_once_with(
fe_data.compliance_period
)
mock_repo.create_fuel_export.assert_awaited_once()
mock_fuel_code_repo.get_standardized_fuel_data.assert_awaited_once_with(
fuel_type_id=fe_data.fuel_type_id,
Expand Down
8 changes: 1 addition & 7 deletions backend/lcfs/tests/fuel_export/test_fuel_exports_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,14 @@ async def test_action_create_fuel_export_success(fuel_export_action_service, moc
fuel_category=mock_fuel_category.dict(),
)
mock_repo.create_fuel_export = AsyncMock(return_value=mock_created_export)
mock_repo.get_compliance_period_id = AsyncMock(return_value=1)

result = await fuel_export_action_service.create_fuel_export(
input_data,
UserTypeEnum.SUPPLIER,
compliance_period=input_data.compliance_period,
UserTypeEnum.SUPPLIER
)

assert isinstance(result, FuelExportSchema)
mock_repo.create_fuel_export.assert_awaited_once()
mock_repo.get_compliance_period_id.assert_awaited_once()


@pytest.mark.anyio
Expand Down Expand Up @@ -217,18 +214,15 @@ async def test_action_update_fuel_export_success(fuel_export_action_service, moc
return_value=mock_existing_export
)
mock_repo.update_fuel_export = AsyncMock(return_value=mock_existing_export)
mock_repo.get_compliance_period_id = AsyncMock(return_value=1)

result = await fuel_export_action_service.update_fuel_export(
input_data,
UserTypeEnum.SUPPLIER,
compliance_period=input_data.compliance_period,
)

assert isinstance(result, FuelExportSchema)
mock_repo.get_fuel_export_version_by_user.assert_awaited_once()
mock_repo.update_fuel_export.assert_awaited_once()
mock_repo.get_compliance_period_id.assert_awaited_once()


@pytest.mark.anyio
Expand Down
21 changes: 7 additions & 14 deletions backend/lcfs/web/api/fuel_export/actions_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async def _populate_fuel_export_fields(

@service_handler
async def create_fuel_export(
self, fe_data: FuelExportCreateUpdateSchema, user_type: UserTypeEnum, compliance_period: str,
self, fe_data: FuelExportCreateUpdateSchema, user_type: UserTypeEnum
) -> FuelExportSchema:
"""
Create a new fuel export record.
Expand All @@ -105,8 +105,6 @@ async def create_fuel_export(
Returns the newly created fuel export record as a response schema.
"""
# Get compliance period ID
compliance_period_id = await self.repo.get_compliance_period_id(compliance_period)

# Assign a unique group UUID for the new fuel export
new_group_uuid = str(uuid.uuid4())
Expand All @@ -128,7 +126,9 @@ async def create_fuel_export(

@service_handler
async def update_fuel_export(
self, fe_data: FuelExportCreateUpdateSchema, user_type: UserTypeEnum, compliance_period: str,
self,
fe_data: FuelExportCreateUpdateSchema,
user_type: UserTypeEnum,
) -> FuelExportSchema:
"""
Update an existing fuel export record or create a new version if necessary.
Expand All @@ -140,9 +140,6 @@ async def update_fuel_export(
Returns the updated or new version of the fuel export record.
"""
# Get compliance period ID
compliance_period_id = await self.repo.get_compliance_period_id(compliance_period)

existing_export = await self.repo.get_fuel_export_version_by_user(
fe_data.group_uuid, fe_data.version, user_type
)
Expand All @@ -163,7 +160,6 @@ async def update_fuel_export(
)

updated_export = await self.repo.update_fuel_export(existing_export)

return FuelExportSchema.model_validate(updated_export)

elif existing_export:
Expand All @@ -179,8 +175,7 @@ async def update_fuel_export(
# Copy existing fields, then apply new data
for field in existing_export.__table__.columns.keys():
if field not in FUEL_EXPORT_EXCLUDE_FIELDS:
setattr(fuel_export, field, getattr(
existing_export, field))
setattr(fuel_export, field, getattr(existing_export, field))

for field, value in fe_data.model_dump(
exclude=FUEL_EXPORT_EXCLUDE_FIELDS
Expand All @@ -195,8 +190,7 @@ async def update_fuel_export(

return FuelExportSchema.model_validate(new_export)

raise HTTPException(
status_code=404, detail="Fuel export record not found.")
raise HTTPException(status_code=404, detail="Fuel export record not found.")

@service_handler
async def delete_fuel_export(
Expand Down Expand Up @@ -229,8 +223,7 @@ async def delete_fuel_export(
if existing_export:
for field in existing_export.__table__.columns.keys():
if field not in FUEL_EXPORT_EXCLUDE_FIELDS:
setattr(delete_export, field, getattr(
existing_export, field))
setattr(delete_export, field, getattr(existing_export, field))

delete_export.compliance_report_id = fe_data.compliance_report_id

Expand Down
4 changes: 0 additions & 4 deletions backend/lcfs/web/api/fuel_supply/actions_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@ async def create_fuel_supply(
Returns:
FuelSupplyResponseSchema: The newly created fuel supply record as a response schema.
"""
# Get compliance period ID
compliance_period_id = await self.repo.get_compliance_period_id(compliance_period)

# Assign a unique group UUID for the new fuel supply
new_group_uuid = str(uuid.uuid4())
fuel_supply = FuelSupply(
Expand Down Expand Up @@ -177,7 +174,6 @@ async def update_fuel_supply(
Raises:
HTTPException: If the fuel supply record is not found.
"""
compliance_period_id = await self.repo.get_compliance_period_id(compliance_period)
existing_fuel_supply = await self.repo.get_fuel_supply_version_by_user(
fs_data.group_uuid, fs_data.version, user_type
)
Expand Down

0 comments on commit 162167d

Please sign in to comment.