Skip to content

Commit

Permalink
Merge branch 'release-0.2.0' into feat/daniel-add-compliance-report-c…
Browse files Browse the repository at this point in the history
…y-1344
  • Loading branch information
dhaselhan authored Dec 6, 2024
2 parents cfe0a68 + da88e9f commit 6232bf1
Show file tree
Hide file tree
Showing 63 changed files with 1,062 additions and 826 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def upgrade():


def downgrade():
# First, remove all existing data that might not match the enum
op.execute("DELETE FROM notification_type;")

# Re-create the old enum type
notification_type_enum_v2 = ENUM(
"TRANSFER_PARTNER_UPDATE",
Expand All @@ -61,7 +64,7 @@ def downgrade():
"name",
type_=notification_type_enum_v2,
existing_type=sa.String(length=255),
postgresql_using="name::text::notification_type_enum_v2",
postgresql_using="name::notification_type_enum_v2",
existing_nullable=False,
)

Expand All @@ -79,9 +82,9 @@ def downgrade():
"""
)

# Reset the sequence for the id column
# Reset the sequence for the id column with correct sequence name
op.execute(
"""
SELECT setval('notification_type_id_seq', (SELECT MAX(notification_type_id) FROM notification_type));
SELECT setval('notification_type_notification_type_id_seq', (SELECT MAX(notification_type_id) FROM notification_type));
"""
)
282 changes: 282 additions & 0 deletions backend/lcfs/db/migrations/versions/2024-12-06-01-23_26ab15f8ab18.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
"""update end use table with new values
Revision ID: 26ab15f8ab18
Revises: d4104af84f2b
Create Date: 2024-12-06 01:23:21.598991
"""

import sqlalchemy as sa
from alembic import op
from datetime import datetime

# revision identifiers, used by Alembic.
revision = "26ab15f8ab18"
down_revision = "d4104af84f2b"
branch_labels = None
depends_on = None


def upgrade() -> None:
current_time = datetime.now()

# Update existing end use types 14-21 to new values
updates = [
(14, 'Aircraft'),
(15, 'Compression-ignition engine- Marine, general'),
(16, 'Compression-ignition engine- Marine, operated within 51 to 75% of load range'),
(17, 'Compression-ignition engine- Marine, operated within 76 to 100% of load range'),
(18, 'Compression-ignition engine- Marine, with methane slip reduction kit- General'),
(19, 'Compression-ignition engine- Marine, with methane slip reduction kit- Operated within 51 to 75% of load range'),
(20, 'Compression-ignition engine- Marine, with methane slip reduction kit- Operated within 76 to 100% of load range'),
(21, 'Compression-ignition engine- Marine, unknown whether kit is installed or average operating load range')
]

for end_use_id, type_name in updates:
op.execute(
"""
UPDATE end_use_type
SET type = '{}',
sub_type = NULL,
intended_use = true,
update_date = '{}',
update_user = 'no_user'
WHERE end_use_type_id = {}
""".format(type_name, current_time, end_use_id)
)

# Update existing UCI values for IDs 1-9
uci_updates = [
(1, 7, 5, None, 0), # Remove end_use_type_id 14
(2, None, 5, None, 0), # Remove fuel_type_id and end_use_type_id
(3, 7, 5, 15, 27.3), # Update with new end_use_type_id and intensity
(4, 7, 5, 16, 17.8),
(5, 7, 5, 17, 12.2),
(6, 7, 5, 18, 10.6),
(7, 7, 5, 19, 8.4),
(8, 7, 5, 20, 8.0),
(9, 7, 5, 21, 27.3)
]

for uci_id, fuel_type_id, uom_id, end_use_type_id, intensity in uci_updates:
if fuel_type_id and end_use_type_id:
op.execute(
"""
UPDATE additional_carbon_intensity
SET fuel_type_id = {},
uom_id = {},
end_use_type_id = {},
intensity = {},
update_date = '{}',
update_user = 'no_user'
WHERE additional_uci_id = {}
""".format(fuel_type_id, uom_id, end_use_type_id, intensity, current_time, uci_id)
)
elif fuel_type_id:
op.execute(
"""
UPDATE additional_carbon_intensity
SET fuel_type_id = {},
uom_id = {},
end_use_type_id = NULL,
intensity = {},
update_date = '{}',
update_user = 'no_user'
WHERE additional_uci_id = {}
""".format(fuel_type_id, uom_id, intensity, current_time, uci_id)
)
else:
op.execute(
"""
UPDATE additional_carbon_intensity
SET fuel_type_id = NULL,
uom_id = {},
end_use_type_id = NULL,
intensity = {},
update_date = '{}',
update_user = 'no_user'
WHERE additional_uci_id = {}
""".format(uom_id, intensity, current_time, uci_id)
)

# Update existing EER values for IDs 14-24
eer_updates = [
(14, 2, 3, 10, 2.8), # Changed to Shore power
(15, 2, 3, 11, 2.4), # Changed to Trolley bus
(16, 2, 3, 2, 1.0), # Changed to Other or unknown
(17, 2, 6, 3, 1.8), # Changed to Fuel cell vehicle
(18, 2, 6, 2, 0.9), # Changed to Other or unknown
(19, 2, 13, None, 0.9), # Changed to default ratio
(20, 3, 3, None, 2.5), # Changed to default ratio
(21, 3, 11, None, 1.0), # Changed to default ratio
(22, 2, 7, 15, 1.0), # Changed to new marine type
(23, 2, 7, 16, 1.0), # Changed to new marine type
(24, 2, 7, 17, 1.0) # Changed to new marine type
]

for eer_id, fuel_category_id, fuel_type_id, end_use_type_id, ratio in eer_updates:
if end_use_type_id:
op.execute(
"""
UPDATE energy_effectiveness_ratio
SET fuel_category_id = {},
fuel_type_id = {},
end_use_type_id = {},
ratio = {},
update_date = '{}',
update_user = 'no_user'
WHERE eer_id = {}
""".format(fuel_category_id, fuel_type_id, end_use_type_id, ratio, current_time, eer_id)
)
else:
op.execute(
"""
UPDATE energy_effectiveness_ratio
SET fuel_category_id = {},
fuel_type_id = {},
end_use_type_id = NULL,
ratio = {},
update_date = '{}',
update_user = 'no_user'
WHERE eer_id = {}
""".format(fuel_category_id, fuel_type_id, ratio, current_time, eer_id)
)


def downgrade() -> None:
current_time = datetime.now()

# Restore original end use types 14-21
original_values = [
(14, 'Marine', 'General'),
(15, 'Marine', 'Operated within 51 to 75% of load range'),
(16, 'Marine', 'Operated within 76 to 100% of load range'),
(17, 'Marine, w/ methane slip reduction kit', 'General'),
(18, 'Marine, w/ methane slip reduction kit',
'Operated within 51 to 75% of load range'),
(19, 'Marine, w/ methane slip reduction kit',
'Operated within 76 to 100% of load range'),
(20, 'Unknown', None),
(21, 'Aircraft', None)
]

for end_use_id, type_name, sub_type in original_values:
if sub_type:
op.execute(
"""
UPDATE end_use_type
SET type = '{}',
sub_type = '{}',
update_date = '{}',
update_user = 'no_user'
WHERE end_use_type_id = {}
""".format(type_name, sub_type, current_time, end_use_id)
)
else:
op.execute(
"""
UPDATE end_use_type
SET type = '{}',
sub_type = NULL,
update_date = '{}',
update_user = 'no_user'
WHERE end_use_type_id = {}
""".format(type_name, current_time, end_use_id)
)

# Restore original UCI values
uci_originals = [
(1, 7, 5, 14, 27.3),
(2, 7, 5, 15, 17.8),
(3, 7, 5, 16, 12.2),
(4, 7, 5, 17, 10.6),
(5, 7, 5, 18, 8.4),
(6, 7, 5, 19, 8.0),
(7, 7, 5, 20, 27.3),
(8, 7, 5, None, 0),
(9, None, 5, None, 0)
]

for uci_id, fuel_type_id, uom_id, end_use_type_id, intensity in uci_originals:
if fuel_type_id and end_use_type_id:
op.execute(
"""
UPDATE additional_carbon_intensity
SET fuel_type_id = {},
uom_id = {},
end_use_type_id = {},
intensity = {},
update_date = '{}',
update_user = 'no_user'
WHERE additional_uci_id = {}
""".format(fuel_type_id, uom_id, end_use_type_id, intensity, current_time, uci_id)
)
elif fuel_type_id:
op.execute(
"""
UPDATE additional_carbon_intensity
SET fuel_type_id = {},
uom_id = {},
end_use_type_id = NULL,
intensity = {},
update_date = '{}',
update_user = 'no_user'
WHERE additional_uci_id = {}
""".format(fuel_type_id, uom_id, intensity, current_time, uci_id)
)
else:
op.execute(
"""
UPDATE additional_carbon_intensity
SET fuel_type_id = NULL,
uom_id = {},
end_use_type_id = NULL,
intensity = {},
update_date = '{}',
update_user = 'no_user'
WHERE additional_uci_id = {}
""".format(uom_id, intensity, current_time, uci_id)
)

# Restore original EER values
eer_originals = [
(14, 2, 3, 14, 2.5), # Restore to Marine
(15, 2, 3, 10, 2.8), # Restore to Shore power
(16, 2, 3, 11, 2.4), # Restore to Trolley bus
(17, 2, 3, 2, 1.0), # Restore to Other or unknown
(18, 2, 6, 3, 1.8), # Restore to Fuel cell vehicle
(19, 2, 6, 2, 0.9), # Restore to Other or unknown
(20, 2, 7, 12, 1.0), # Restore to Compression-ignition engine
(21, 2, 7, 2, 0.9), # Restore to Other or unknown
(22, 2, 13, None, 0.9), # Restore to default ratio
(23, 3, 3, None, 2.5), # Restore to default ratio
(24, 3, 11, None, 1.0) # Restore to default ratio
]

for eer_id, fuel_category_id, fuel_type_id, end_use_type_id, ratio in eer_originals:
if end_use_type_id:
op.execute(
"""
UPDATE energy_effectiveness_ratio
SET fuel_category_id = {},
fuel_type_id = {},
end_use_type_id = {},
ratio = {},
update_date = '{}',
update_user = 'no_user'
WHERE eer_id = {}
""".format(fuel_category_id, fuel_type_id, end_use_type_id, ratio, current_time, eer_id)
)
else:
op.execute(
"""
UPDATE energy_effectiveness_ratio
SET fuel_category_id = {},
fuel_type_id = {},
end_use_type_id = NULL,
ratio = {},
update_date = '{}',
update_user = 'no_user'
WHERE eer_id = {}
""".format(fuel_category_id, fuel_type_id, ratio, current_time, eer_id)
)
2 changes: 1 addition & 1 deletion backend/lcfs/db/models/fuel/FuelCode.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class FuelCode(BaseModel, Auditable, EffectiveDates):
fuel_code_prefix = relationship(
"FuelCodePrefix", back_populates="fuel_codes", lazy="joined"
)
fuel_code_type = relationship(
fuel_type = relationship(
"FuelType", back_populates="fuel_codes", lazy="joined"
)

Expand Down
2 changes: 1 addition & 1 deletion backend/lcfs/db/models/fuel/FuelType.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class FuelType(BaseModel, Auditable, DisplayOrder):
)

# Relationships
fuel_codes = relationship("FuelCode", back_populates="fuel_code_type")
fuel_codes = relationship("FuelCode", back_populates="fuel_type")
energy_density = relationship(
"EnergyDensity", back_populates="fuel_type", uselist=False
) # One energy density per fuel type
Expand Down
2 changes: 1 addition & 1 deletion backend/lcfs/tests/fuel_code/test_fuel_code_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def test_export_success():
approval_date="2023-11-01",
effective_date="2023-12-01",
expiration_date="2024-01-01",
fuel_code_type=MagicMock(fuel_type="Diesel"),
fuel_type=MagicMock(fuel_type="Diesel"),
feedstock="Corn oil",
feedstock_location="Canada",
feedstock_misc=None,
Expand Down
10 changes: 5 additions & 5 deletions backend/lcfs/tests/fuel_code/test_fuel_code_repo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from unittest.mock import AsyncMock
from unittest.mock import AsyncMock, MagicMock
from lcfs.web.api.fuel_code.repo import FuelCodeRepository
from lcfs.db.models.fuel.TransportMode import TransportMode

Expand All @@ -23,10 +23,10 @@ async def test_get_transport_mode_by_name(fuel_code_repo, mock_db):
# Define the test transport mode
transport_mode_name = "Truck"
mock_transport_mode = TransportMode(transport_mode_id=1, transport_mode="Truck")

# Mock the database query result
mock_db.execute.return_value.scalar_one_or_none = AsyncMock()
mock_db.execute.return_value.scalar_one_or_none.return_value = mock_transport_mode
mock_db.execute.return_value.scalar_one = MagicMock()
mock_db.execute.return_value.scalar_one.return_value = mock_transport_mode

# Call the repository method
result = await fuel_code_repo.get_transport_mode_by_name(transport_mode_name)
Expand All @@ -35,4 +35,4 @@ async def test_get_transport_mode_by_name(fuel_code_repo, mock_db):
assert result == mock_transport_mode

# Ensure the database query was called
mock_db.execute.assert_called_once()
mock_db.execute.assert_called_once()
2 changes: 1 addition & 1 deletion backend/lcfs/tests/fuel_code/test_fuel_code_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def test_get_fuel_codes_success():
pagination = PaginationRequestSchema(page=1, size=10)

# Act
result = await service.get_fuel_codes(pagination)
result = await service.search_fuel_codes(pagination)

# Assert
assert isinstance(result.pagination, PaginationResponseSchema)
Expand Down
Loading

0 comments on commit 6232bf1

Please sign in to comment.