Skip to content

Commit

Permalink
Merge branch 'release-0.2.0' into feat/daniel-add-is-legacy-1550
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaselhan authored Jan 6, 2025
2 parents fe52b18 + 23eec01 commit b9260b5
Show file tree
Hide file tree
Showing 18 changed files with 218 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ def upgrade() -> None:
(20, 'Compression-ignition engine- Marine, with methane slip reduction kit- Operated within 76 to 100% of load range', TRUE),
(21, 'Compression-ignition engine- Marine, unknown whether kit is installed or average operating load range', TRUE),
(22, 'Unknown engine type', TRUE),
(23, 'Other (i.e. road transportation)', TRUE)
(23, 'Other (i.e. road transportation)', TRUE),
(24, 'Any', TRUE)
ON CONFLICT (end_use_type_id) DO NOTHING;
""")
Expand Down Expand Up @@ -224,13 +225,13 @@ def upgrade() -> None:
eer_id, fuel_category_id, fuel_type_id, end_use_type_id, ratio, effective_status
)
VALUES
(1, 1, 2, NULL, 0.9, TRUE),
(1, 1, 2, 24, 0.9, TRUE),
(2, 1, 3, 1, 3.5, TRUE),
(3, 1, 3, 2, 1.0, TRUE),
(4, 1, 6, 3, 2.4, TRUE),
(5, 1, 6, 2, 0.9, TRUE),
(6, 1, 13, NULL, 0.9, TRUE),
(7, 2, 2, NULL, 0.9, TRUE),
(6, 1, 13, 24, 0.9, TRUE),
(7, 2, 2, 24, 0.9, TRUE),
(8, 2, 3, 4, 3.8, TRUE),
(9, 2, 3, 5, 3.2, TRUE),
(10, 2, 3, 6, 2.5, TRUE),
Expand All @@ -242,9 +243,9 @@ def upgrade() -> None:
(16, 2, 3, 2, 1.0, TRUE),
(17, 2, 6, 3, 1.8, TRUE),
(18, 2, 6, 2, 0.9, TRUE),
(19, 2, 13, NULL, 0.9, TRUE),
(20, 3, 3, NULL, 2.5, TRUE),
(21, 3, 11, NULL, 1.0, TRUE),
(19, 2, 13, 24, 0.9, TRUE),
(20, 3, 3, 24, 2.5, TRUE),
(21, 3, 11, 24, 1.0, TRUE),
(22, 2, 7, 15, 1.0, TRUE),
(23, 2, 7, 16, 1.0, TRUE),
(24, 2, 7, 17, 1.0, TRUE),
Expand All @@ -253,7 +254,20 @@ def upgrade() -> None:
(27, 2, 7, 20, 1.0, TRUE),
(28, 2, 7, 21, 1.0, TRUE),
(29, 2, 7, 22, 0.9, TRUE),
(30, 2, 7, 23, 0.9, TRUE)
(30, 2, 7, 23, 0.9, TRUE),
(31, 2, 1, 24, 1.0, TRUE),
(32, 2, 5, 24, 1.0, TRUE),
(33, 3, 6, 24, 1.0, TRUE),
(34, 1, 14, 24, 1.0, TRUE),
(35, 1, 15, 24, 1.0, TRUE),
(36, 2, 16, 24, 1.0, TRUE),
(37, 1, 17, 24, 1.0, TRUE),
(38, 3, 18, 24, 1.0, TRUE),
(39, 1, 19, 24, 1.0, TRUE),
(40, 2, 19, 24, 1.0, TRUE),
(41, 3, 7, 24, 1.0, TRUE),
(42, 2, 20, 24, 1.0, TRUE),
(43, 1, 4, 24, 1.0, TRUE)
ON CONFLICT (eer_id) DO NOTHING;
""")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Rename Reassessed
Revision ID: e883ad1f0f60
Revises: 9329e38396e1
Create Date: 2025-01-03 23:31:19.098618
"""

import sqlalchemy as sa
from alembic import op
from alembic_postgresql_enum import TableReference

# revision identifiers, used by Alembic.
revision = "e883ad1f0f60"
down_revision = "9329e38396e1"
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.sync_enum_values(
"public",
"compliancereportstatusenum",
[
"Draft",
"Submitted",
"Recommended_by_analyst",
"Recommended_by_manager",
"Assessed",
"Reassessed",
],
[
TableReference(
table_schema="public",
table_name="compliance_report_status",
column_name="status",
)
],
enum_values_to_rename=[("ReAssessed", "Reassessed")],
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.sync_enum_values(
"public",
"compliancereportstatusenum",
[
"Draft",
"Submitted",
"Recommended_by_analyst",
"Recommended_by_manager",
"Assessed",
"ReAssessed",
],
[
TableReference(
table_schema="public",
table_name="compliance_report_status",
column_name="status",
)
],
enum_values_to_rename=[("Reassessed", "ReAssessed")],
)
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Update fuel type name
Revision ID: bfa7bbb1eea3
Revises: 9329e38396e1
Create Date: 2025-01-03 13:24:19.525006
"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "bfa7bbb1eea3"
down_revision = "e883ad1f0f60"
branch_labels = None
depends_on = None


def upgrade() -> None:
# Update fuel type name
op.execute("""
UPDATE fuel_type
SET fuel_type = 'Other diesel fuel',
provision_1_id = 3 -- Change from prescribed (1) to default (3)
WHERE fuel_type = 'Other diesel';
""")


def downgrade() -> None:
# Revert fuel type name update
op.execute("""
UPDATE fuel_type
SET fuel_type = 'Other diesel',
provision_1_id = 1 -- Change from default (3) to prescribed (1)
WHERE fuel_type = 'Other diesel fuel';
""")

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ComplianceReportStatusEnum(enum.Enum):
Recommended_by_analyst = "Recommended by analyst"
Recommended_by_manager = "Recommended by manager"
Assessed = "Assessed"
ReAssessed = "ReAssessed"
Reassessed = "Reassessed"


class ComplianceReportStatus(BaseModel, EffectiveDates):
Expand Down
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
26 changes: 13 additions & 13 deletions backend/lcfs/web/api/compliance_report/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ def apply_filters(self, pagination, conditions):
filter_option = filter.type
filter_type = filter.filter_type
if filter.field == "status":
field = get_field_for_filter(ComplianceReportStatus, "status")
normalized_value = filter_value.lower()
enum_value_map = {
enum_val.value.lower(): enum_val
for enum_val in ComplianceReportStatusEnum
}
filter_value = enum_value_map.get(normalized_value)
field = get_field_for_filter(ComplianceReportStatus, filter.field)
if isinstance(filter_value, list):
filter_value = [
ComplianceReportStatusEnum(value) for value in filter_value
]
filter_type = "set"
else:
filter_value = ComplianceReportStatusEnum(filter_value)
elif filter.field == "organization":
field = get_field_for_filter(Organization, "name")
elif filter.field == "type":
Expand Down Expand Up @@ -359,12 +360,11 @@ async def get_reports_paginated(
.group_by(ComplianceReport.compliance_report_group_uuid)
)

if not organization_id:
subquery = subquery.join(
ComplianceReportStatus,
ComplianceReport.current_status_id
== ComplianceReportStatus.compliance_report_status_id,
)
subquery = subquery.join(
ComplianceReportStatus,
ComplianceReport.current_status_id
== ComplianceReportStatus.compliance_report_status_id,
)

subquery = subquery.subquery()
# Join the main ComplianceReport table with the subquery to get the latest version per group
Expand Down
2 changes: 1 addition & 1 deletion backend/lcfs/web/api/compliance_report/update_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ async def handle_status_change(
ComplianceReportStatusEnum.Recommended_by_analyst: self.handle_recommended_by_analyst_status,
ComplianceReportStatusEnum.Recommended_by_manager: self.handle_recommended_by_manager_status,
ComplianceReportStatusEnum.Assessed: self.handle_assessed_status,
ComplianceReportStatusEnum.ReAssessed: self.handle_reassessed_status,
ComplianceReportStatusEnum.Reassessed: self.handle_reassessed_status,
}

handler = status_handlers.get(new_status)
Expand Down
2 changes: 1 addition & 1 deletion backend/lcfs/web/api/fuel_supply/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class FuelSupplyCreateUpdateSchema(BaseSchema):
version: Optional[int] = None
fuel_type_id: int
fuel_category_id: int
end_use_id: Optional[int] = None
end_use_id: int
provision_of_the_act_id: int
quantity: int
units: str
Expand Down
Loading

0 comments on commit b9260b5

Please sign in to comment.