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

fix: adjust line 1 and 2 calculations #1538

Merged
merged 3 commits into from
Dec 21, 2024
Merged
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
@@ -0,0 +1,40 @@
"""update other diesel

Revision ID: 59873cafbcd8
Revises: 851e09cf8661
Create Date: 2024-12-20 05:17:40.638826

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "59873cafbcd8"
down_revision = "851e09cf8661"
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.execute(
"""
UPDATE fuel_type
SET fossil_derived = false, other_uses_fossil_derived = false
WHERE fuel_type = 'Other diesel'
"""
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.execute(
"""
UPDATE fuel_type
SET fossil_derived = true, other_uses_fossil_derived = true
WHERE fuel_type = 'Other diesel'
"""
)
# ### end Alembic commands ###
6 changes: 3 additions & 3 deletions backend/lcfs/db/seeders/common/seed_fuel_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@
{
"fuel_type_id": 20,
"fuel_type": "Other diesel",
"fossil_derived": true,
"other_uses_fossil_derived": true,
"fossil_derived": false,
"other_uses_fossil_derived": false,
"provision_1_id": 1,
"default_carbon_intensity": 100.21,
"units": "L",
Expand Down Expand Up @@ -1095,4 +1095,4 @@
"display_order": 4
}
]
}
}
22 changes: 15 additions & 7 deletions backend/lcfs/tests/compliance_report/test_summary_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ async def test_calculate_low_carbon_fuel_target_summary(

# Assertions
assert isinstance(summary, list)
assert all(isinstance(item, ComplianceReportSummaryRowSchema) for item in summary)
assert all(isinstance(item, ComplianceReportSummaryRowSchema)
for item in summary)
assert len(summary) == 11 # Ensure all 11 lines are present

# Check specific line values
Expand Down Expand Up @@ -510,7 +511,8 @@ async def test_calculate_renewable_fuel_target_summary_no_renewables(

assert len(result) == 11
assert isinstance(result[0], ComplianceReportSummaryRowSchema)
assert result[10].gasoline == 15.0 # Penalty should be applied due to no renewables
# Penalty should be applied due to no renewables
assert result[10].gasoline == 15.0
assert result[10].diesel == 36.0
assert result[10].jet_fuel == 45.0
assert result[10].total_value == 96.0
Expand Down Expand Up @@ -548,14 +550,12 @@ async def test_calculate_renewable_fuel_target_summary_high_renewables(

assert len(result) == 11
assert isinstance(result[0], ComplianceReportSummaryRowSchema)
assert result[10].gasoline == 0 # No penalty since renewables exceed requirements
# No penalty since renewables exceed requirements
assert result[10].gasoline == 0
assert result[10].diesel == 0
assert result[10].jet_fuel == 0


import pytest


@pytest.mark.anyio
async def test_calculate_renewable_fuel_target_summary_copy_lines_6_and_8(
compliance_report_summary_service,
Expand Down Expand Up @@ -710,6 +710,13 @@ async def test_can_sign_flag_logic(
"jet_fuel": 25,
}
)
mock_repo.aggregate_other_uses = AsyncMock(
return_value={
"gasoline": 50,
"diesel": 25,
"jet_fuel": 10,
}
)
mock_repo.get_assessed_compliance_report_by_period = AsyncMock(
return_value=MagicMock(
summary=MagicMock(
Expand Down Expand Up @@ -810,7 +817,8 @@ async def test_calculate_fuel_quantities_renewable(
):
# Create a mock repository
mock_repo.aggregate_fuel_supplies.return_value = {"gasoline": 200.0}
mock_repo.aggregate_other_uses.return_value = {"diesel": 75.0, "jet-fuel": 25.0}
mock_repo.aggregate_other_uses.return_value = {
"diesel": 75.0, "jet-fuel": 25.0}

# Define test inputs
compliance_report_id = 2
Expand Down
35 changes: 23 additions & 12 deletions backend/lcfs/tests/fuel_code/test_fuel_code_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ async def test_get_fuel_categories(fuel_code_repo, mock_db):


@pytest.mark.anyio
async def test_get_fuel_category_by_name(fuel_code_repo, mock_db):
async def test_get_fuel_category_by(fuel_code_repo, mock_db):
mock_fc = FuelCategory(
fuel_category_id=2, category="Fossil", default_carbon_intensity=0
)
Expand Down Expand Up @@ -327,7 +327,8 @@ async def test_get_fuel_codes_paginated(fuel_code_repo, mock_db):
unique=MagicMock(
return_value=MagicMock(
scalars=MagicMock(
return_value=MagicMock(all=MagicMock(return_value=[fc]))
return_value=MagicMock(
all=MagicMock(return_value=[fc]))
)
)
)
Expand All @@ -342,7 +343,8 @@ async def test_get_fuel_codes_paginated(fuel_code_repo, mock_db):

@pytest.mark.anyio
async def test_get_fuel_code_statuses(fuel_code_repo, mock_db):
fcs = FuelCodeStatus(fuel_code_status_id=1, status=FuelCodeStatusEnum.Approved)
fcs = FuelCodeStatus(fuel_code_status_id=1,
status=FuelCodeStatusEnum.Approved)
mock_result = MagicMock()
mock_result.scalars.return_value.all.return_value = [fcs]
mock_db.execute.return_value = mock_result
Expand Down Expand Up @@ -371,7 +373,8 @@ async def test_get_fuel_code(fuel_code_repo, mock_db, valid_fuel_code):

@pytest.mark.anyio
async def test_get_fuel_code_status_enum(fuel_code_repo, mock_db):
fcs = FuelCodeStatus(fuel_code_status_id=2, status=FuelCodeStatusEnum.Deleted)
fcs = FuelCodeStatus(fuel_code_status_id=2,
status=FuelCodeStatusEnum.Deleted)
mock_db.scalar.return_value = fcs
result = await fuel_code_repo.get_fuel_code_status(FuelCodeStatusEnum.Deleted)
assert result == fcs
Expand Down Expand Up @@ -403,7 +406,8 @@ async def test_delete_fuel_code(fuel_code_repo, mock_db):
@pytest.mark.anyio
async def test_get_distinct_company_names(fuel_code_repo, mock_db):
mock_result = MagicMock()
mock_result.scalars.return_value.all.return_value = ["CompanyA", "CompanyB"]
mock_result.scalars.return_value.all.return_value = [
"CompanyA", "CompanyB"]
mock_db.execute.return_value = mock_result

result = await fuel_code_repo.get_distinct_company_names("Com")
Expand All @@ -413,7 +417,8 @@ async def test_get_distinct_company_names(fuel_code_repo, mock_db):
@pytest.mark.anyio
async def test_get_contact_names_by_company(fuel_code_repo, mock_db):
mock_result = MagicMock()
mock_result.scalars.return_value.all.return_value = ["John Doe", "Jane Doe"]
mock_result.scalars.return_value.all.return_value = [
"John Doe", "Jane Doe"]
mock_db.execute.return_value = mock_result

result = await fuel_code_repo.get_contact_names_by_company("CompanyA", "J")
Expand Down Expand Up @@ -446,7 +451,8 @@ async def test_get_distinct_fuel_codes_by_code(fuel_code_repo, mock_db):
async def test_get_fuel_code_by_code_prefix(fuel_code_repo, mock_db):
fc = FuelCode(fuel_code_id=10, fuel_suffix="200.0")
mock_result = MagicMock()
mock_result.unique.return_value.scalars.return_value.all.return_value = [fc]
mock_result.unique.return_value.scalars.return_value.all.return_value = [
fc]
mock_db.execute.return_value = mock_result

# Mock the next available suffix
Expand Down Expand Up @@ -612,7 +618,8 @@ async def test_get_standardized_fuel_data(fuel_code_repo, mock_db):
MagicMock(
scalars=MagicMock(
return_value=MagicMock(
first=MagicMock(return_value=EnergyEffectivenessRatio(ratio=2.0))
first=MagicMock(
return_value=EnergyEffectivenessRatio(ratio=2.0))
)
)
),
Expand Down Expand Up @@ -669,7 +676,8 @@ async def test_get_standardized_fuel_data_unrecognized(fuel_code_repo, mock_db):
mock_db.get_one.return_value = mock_fuel_type

# Mock the repo method to get the fuel category
fuel_code_repo.get_fuel_category_by = AsyncMock(return_value=mock_fuel_category)
fuel_code_repo.get_fuel_category_by = AsyncMock(
return_value=mock_fuel_category)

# Setup side effects for subsequent queries:
# Energy Density
Expand All @@ -684,7 +692,8 @@ async def test_get_standardized_fuel_data_unrecognized(fuel_code_repo, mock_db):
eer_result = MagicMock(
scalars=MagicMock(
return_value=MagicMock(
first=MagicMock(return_value=EnergyEffectivenessRatio(ratio=2.0))
first=MagicMock(
return_value=EnergyEffectivenessRatio(ratio=2.0))
)
)
)
Expand All @@ -693,7 +702,8 @@ async def test_get_standardized_fuel_data_unrecognized(fuel_code_repo, mock_db):
scalars=MagicMock(
return_value=MagicMock(
all=MagicMock(
return_value=[TargetCarbonIntensity(target_carbon_intensity=50.0)]
return_value=[TargetCarbonIntensity(
target_carbon_intensity=50.0)]
)
)
)
Expand Down Expand Up @@ -729,7 +739,8 @@ async def test_get_standardized_fuel_data_unrecognized(fuel_code_repo, mock_db):
assert result.uci == 5.0

# Ensure get_fuel_category_by was called once with the correct parameter
fuel_code_repo.get_fuel_category_by.assert_awaited_once_with(fuel_category_id=2)
fuel_code_repo.get_fuel_category_by.assert_awaited_once_with(
fuel_category_id=2)


@pytest.mark.anyio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
def notional_transfer_service():
mock_repo = MagicMock(spec=NotionalTransferRepository)
mock_fuel_repo = MagicMock()
service = NotionalTransferServices(repo=mock_repo, fuel_repo=mock_fuel_repo)
service = NotionalTransferServices(
repo=mock_repo, fuel_repo=mock_fuel_repo)
return service, mock_repo, mock_fuel_repo


Expand All @@ -47,12 +48,13 @@ async def test_get_table_options(notional_transfer_service):
async def test_create_notional_transfer(notional_transfer_service):
service, mock_repo, mock_fuel_repo = notional_transfer_service
notional_transfer_data = create_mock_schema({})
mock_fuel_repo.get_fuel_category_by_name = AsyncMock(
mock_fuel_repo.get_fuel_category_by = AsyncMock(
return_value=MagicMock(fuel_category_id=1)
)

mock_created_transfer = create_mock_entity({})
mock_repo.create_notional_transfer = AsyncMock(return_value=mock_created_transfer)
mock_repo.create_notional_transfer = AsyncMock(
return_value=mock_created_transfer)

response = await service.create_notional_transfer(
notional_transfer_data, UserTypeEnum.SUPPLIER
Expand Down Expand Up @@ -93,7 +95,7 @@ async def test_update_notional_transfer(notional_transfer_service):
mock_repo.get_latest_notional_transfer_by_group_uuid = AsyncMock(
return_value=mock_existing_transfer
)
mock_fuel_repo.get_fuel_category_by_name = AsyncMock(
mock_fuel_repo.get_fuel_category_by = AsyncMock(
return_value=MagicMock(category="Gasoline")
)

Expand All @@ -112,7 +114,8 @@ async def test_update_notional_transfer(notional_transfer_service):
action_type=ActionTypeEnum.UPDATE,
)
# Set the return value for update_notional_transfer
mock_repo.update_notional_transfer = AsyncMock(return_value=mock_updated_transfer)
mock_repo.update_notional_transfer = AsyncMock(
return_value=mock_updated_transfer)

# Execute the update function and capture the response
response = await service.update_notional_transfer(
Expand All @@ -131,7 +134,8 @@ async def test_update_notional_transfer_not_found(notional_transfer_service):
service, mock_repo, _ = notional_transfer_service
notional_transfer_data = create_mock_schema({})

mock_repo.get_notional_transfer_version_by_user = AsyncMock(return_value=None)
mock_repo.get_notional_transfer_version_by_user = AsyncMock(
return_value=None)

with pytest.raises(ServiceException):
await service.update_notional_transfer(
Expand Down Expand Up @@ -171,7 +175,8 @@ async def test_delete_notional_transfer(notional_transfer_service):
mock_repo.get_latest_notional_transfer_by_group_uuid = AsyncMock(
return_value=mock_existing_transfer
)
mock_repo.create_notional_transfer = AsyncMock(return_value=mock_existing_transfer)
mock_repo.create_notional_transfer = AsyncMock(
return_value=mock_existing_transfer)

# Call the delete service
response = await service.delete_notional_transfer(
Expand Down
31 changes: 20 additions & 11 deletions backend/lcfs/tests/other_uses/test_other_uses_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,16 @@ async def test_create_other_use(other_uses_service):
mock_fuel_code.fuel_code = "FuelCode123"

# Mock fuel repository methods
mock_fuel_repo.get_fuel_category_by_name = AsyncMock(return_value=mock_fuel_category)
mock_fuel_repo.get_fuel_type_by_name = AsyncMock(return_value=mock_fuel_type)
mock_fuel_repo.get_expected_use_type_by_name = AsyncMock(return_value=mock_expected_use)
mock_fuel_repo.get_provision_of_the_act_by_name = AsyncMock(return_value=mock_provision_of_the_act)
mock_fuel_repo.get_fuel_code_by_name = AsyncMock(return_value=mock_fuel_code)
mock_fuel_repo.get_fuel_category_by = AsyncMock(
return_value=mock_fuel_category)
mock_fuel_repo.get_fuel_type_by_name = AsyncMock(
return_value=mock_fuel_type)
mock_fuel_repo.get_expected_use_type_by_name = AsyncMock(
return_value=mock_expected_use)
mock_fuel_repo.get_provision_of_the_act_by_name = AsyncMock(
return_value=mock_provision_of_the_act)
mock_fuel_repo.get_fuel_code_by_name = AsyncMock(
return_value=mock_fuel_code)

# Create a mock for the created other use
mock_created_use = create_mock_entity({})
Expand Down Expand Up @@ -115,7 +120,8 @@ async def test_update_other_use(other_uses_service):
mock_existing_use = create_mock_entity({})

# Configure repository methods to return these mocked objects
mock_repo.get_other_use_version_by_user = AsyncMock(return_value=mock_existing_use)
mock_repo.get_other_use_version_by_user = AsyncMock(
return_value=mock_existing_use)

# Mock related entities with proper string attributes
mock_fuel_type = MagicMock()
Expand All @@ -134,13 +140,17 @@ async def test_update_other_use(other_uses_service):
mock_fuel_code.fuel_code = "NewFuelCode"

# Mock fuel repository methods
mock_fuel_repo.get_fuel_type_by_name = AsyncMock(return_value=mock_fuel_type)
mock_fuel_repo.get_fuel_category_by_name = AsyncMock(return_value=mock_fuel_category)
mock_fuel_repo.get_expected_use_type_by_name = AsyncMock(return_value=mock_expected_use)
mock_fuel_repo.get_fuel_type_by_name = AsyncMock(
return_value=mock_fuel_type)
mock_fuel_repo.get_fuel_category_by = AsyncMock(
return_value=mock_fuel_category)
mock_fuel_repo.get_expected_use_type_by_name = AsyncMock(
return_value=mock_expected_use)
mock_fuel_repo.get_provision_of_the_act_by_name = AsyncMock(
return_value=mock_provision_of_the_act
)
mock_fuel_repo.get_fuel_code_by_name = AsyncMock(return_value=mock_fuel_code)
mock_fuel_repo.get_fuel_code_by_name = AsyncMock(
return_value=mock_fuel_code)

# Mock the updated use returned after the update
mock_updated_use = MagicMock()
Expand Down Expand Up @@ -181,7 +191,6 @@ async def test_update_other_use(other_uses_service):
mock_repo.update_other_use.assert_awaited_once()



@pytest.mark.anyio
async def test_update_other_use_not_found(other_uses_service):
service, mock_repo, _ = other_uses_service
Expand Down
12 changes: 12 additions & 0 deletions backend/lcfs/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,15 @@ class FILE_MEDIA_TYPE(Enum):
}

default_ci = {"Gasoline": 93.67, "Diesel": 100.21, "Jet fuel": 88.83}


RENEWABLE_FUEL_TYPES = [
"Renewable gasoline",
"Ethanol",
"Renewable naphtha",
"Biodiesel",
"HDRD",
"Other diesel",
"Alternative jet fuel",
"Other"
]
Loading
Loading