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: Use compliance period from the Compliance Report #1526

Merged
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,36 @@
"""Add UQ to TCI

Revision ID: ab04810d4d7c
Revises: 5fbcb508c1be
Create Date: 2024-12-22 23:46:37.505166

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "ab04810d4d7c"
down_revision = "5fbcb508c1be"
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_unique_constraint(
"uq_target_carbon_intensity_compliance_fuel",
"target_carbon_intensity",
["compliance_period_id", "fuel_category_id"],
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(
"uq_target_carbon_intensity_compliance_fuel",
"target_carbon_intensity",
type_="unique",
)
# ### end Alembic commands ###
3 changes: 1 addition & 2 deletions backend/lcfs/db/models/admin_adjustment/AdminAdjustment.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from sqlalchemy import Column, Integer, BigInteger, ForeignKey, DateTime, String
from sqlalchemy.orm import relationship
from sqlalchemy import UniqueConstraint

from lcfs.db.base import BaseModel, Auditable, EffectiveDates


class AdminAdjustment(BaseModel, Auditable, EffectiveDates):
__tablename__ = "admin_adjustment"
__table_args__ = (
UniqueConstraint("admin_adjustment_id"),
{"comment": "Goverment to organization compliance units admin_adjustment"},
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from sqlalchemy import Column, Integer, BigInteger, ForeignKey, DateTime
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy import UniqueConstraint

from lcfs.db.base import BaseModel, Auditable, EffectiveDates


class AdminAdjustmentHistory(BaseModel, Auditable, EffectiveDates):
__tablename__ = "admin_adjustment_history"
__table_args__ = (
UniqueConstraint("admin_adjustment_history_id"),
{"comment": "History record for admin_adjustment status change."},
)

Expand Down
14 changes: 9 additions & 5 deletions backend/lcfs/db/models/comment/InternalComment.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from sqlalchemy import Column, Integer, Text, UniqueConstraint
from sqlalchemy.orm import relationship
from sqlalchemy import Column, Integer, Text
from sqlalchemy.dialects.postgresql import ENUM
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable

# ENUM for audience scope
audience_scope_enum = ENUM(
"Director", "Analyst", "Compliance Manager", name="audience_scope", create_type=False
"Director",
"Analyst",
"Compliance Manager",
name="audience_scope",
create_type=False,
)


class InternalComment(BaseModel, Auditable):
__tablename__ = "internal_comment"
__table_args__ = (
UniqueConstraint("internal_comment_id"),
{"comment": "Stores internal comments with scope and related metadata."},
)

Expand Down Expand Up @@ -42,4 +46,4 @@ class InternalComment(BaseModel, Auditable):
)
compliance_report_internal_comments = relationship(
"ComplianceReportInternalComment", back_populates="internal_comment"
)
)
3 changes: 2 additions & 1 deletion backend/lcfs/db/models/compliance/CompliancePeriod.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from sqlalchemy import Column, Integer, String, Date
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, EffectiveDates


Expand Down
6 changes: 3 additions & 3 deletions backend/lcfs/db/models/compliance/ComplianceReport.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import uuid
import enum
import uuid

from pydantic import computed_field
from sqlalchemy import (
Column,
Integer,
String,
ForeignKey,
Enum,
Table,
ForeignKey,
)
from sqlalchemy.orm import relationship, backref
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable


Expand Down
4 changes: 1 addition & 3 deletions backend/lcfs/db/models/document/Document.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy import UniqueConstraint
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable
Expand All @@ -11,7 +10,6 @@
class Document(BaseModel, Auditable):
__tablename__ = "document"
__table_args__ = (
UniqueConstraint("document_id"),
{"comment": "Main document table for storing base document information"},
)

Expand Down
5 changes: 3 additions & 2 deletions backend/lcfs/db/models/fuel/FuelCategory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from sqlalchemy import Column, Integer, Text, Enum, Float, Numeric
from lcfs.db.base import BaseModel, Auditable, DisplayOrder, EffectiveDates
from sqlalchemy import Column, Integer, Text, Enum, Numeric
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable, DisplayOrder, EffectiveDates


class FuelCategory(BaseModel, Auditable, DisplayOrder, EffectiveDates):

Expand Down
10 changes: 6 additions & 4 deletions backend/lcfs/db/models/fuel/FuelType.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from sqlalchemy import Column, Integer, Text, Boolean, Float, Enum, Numeric
from lcfs.db.base import BaseModel, Auditable, DisplayOrder
from sqlalchemy.orm import relationship
from sqlalchemy import ForeignKey
import enum

from sqlalchemy import Column, Integer, Text, Boolean, Enum, Numeric
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable, DisplayOrder


# Enum for fuel quantity units
class QuantityUnitsEnum(enum.Enum):
Expand Down
13 changes: 9 additions & 4 deletions backend/lcfs/db/models/fuel/TargetCarbonIntensity.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from sqlalchemy import Column, Integer, Float, ForeignKey, Numeric
from sqlalchemy import Column, Integer, Float, ForeignKey, Numeric, UniqueConstraint
from lcfs.db.base import BaseModel, Auditable, EffectiveDates
from sqlalchemy.orm import relationship


class TargetCarbonIntensity(BaseModel, Auditable, EffectiveDates):
__tablename__ = "target_carbon_intensity"
__table_args__ = {
"comment": "Target carbon intensity values for various fuel categories"
}
__table_args__ = (
UniqueConstraint(
"compliance_period_id",
"fuel_category_id",
name="uq_target_carbon_intensity_compliance_fuel",
),
{"comment": "Target carbon intensity values for various fuel categories"},
)

target_carbon_intensity_id = Column(Integer, primary_key=True, autoincrement=True)
compliance_period_id = Column(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from sqlalchemy import Column, Integer, BigInteger, ForeignKey, DateTime, String
from sqlalchemy.orm import relationship
from sqlalchemy import UniqueConstraint

from lcfs.db.base import BaseModel, Auditable, EffectiveDates


class InitiativeAgreement(BaseModel, Auditable, EffectiveDates):
__tablename__ = "initiative_agreement"
__table_args__ = (
UniqueConstraint("initiative_agreement_id"),
{"comment": "Goverment to organization compliance units initiative agreement"},
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from sqlalchemy import Column, Integer, BigInteger, ForeignKey, DateTime
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy import UniqueConstraint

from lcfs.db.base import BaseModel, Auditable, EffectiveDates


class InitiativeAgreementHistory(BaseModel, Auditable, EffectiveDates):
__tablename__ = "initiative_agreement_history"
__table_args__ = (
UniqueConstraint("initiative_agreement_history_id"),
{"comment": "History record for initiative agreement status change."},
)

Expand Down
2 changes: 0 additions & 2 deletions backend/lcfs/db/models/transaction/Transaction.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import enum

from sqlalchemy import Column, Integer, BigInteger, ForeignKey, Enum
from sqlalchemy import UniqueConstraint
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable, EffectiveDates
Expand All @@ -16,7 +15,6 @@ class TransactionActionEnum(enum.Enum):
class Transaction(BaseModel, Auditable, EffectiveDates):
__tablename__ = "transaction"
__table_args__ = (
UniqueConstraint("transaction_id"),
{
"comment": "Contains a list of all of the government to organization and Organization to Organization transaction."
},
Expand Down
4 changes: 2 additions & 2 deletions backend/lcfs/db/models/transfer/Transfer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import enum

from sqlalchemy import Column, Integer, ForeignKey, DateTime, Enum, String, Numeric
from sqlalchemy.orm import relationship
from sqlalchemy import UniqueConstraint

from lcfs.db.base import BaseModel, Auditable, EffectiveDates


Expand All @@ -13,7 +14,6 @@ class TransferRecommendationEnum(enum.Enum):
class Transfer(BaseModel, Auditable, EffectiveDates):
__tablename__ = "transfer"
__table_args__ = (
UniqueConstraint("transfer_id"),
{"comment": "Records of tranfer from Organization to Organization"},
)

Expand Down
5 changes: 1 addition & 4 deletions backend/lcfs/db/models/transfer/TransferCategory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ class TransferCategoryEnum(enum.Enum):

class TransferCategory(BaseModel, Auditable, EffectiveDates):
__tablename__ = "transfer_category"
__table_args__ = (
UniqueConstraint("transfer_category_id"),
{"comment": "Transfer Category"},
)
__table_args__ = ({"comment": "Transfer Category"},)

transfer_category_id = Column(
Integer,
Expand Down
3 changes: 2 additions & 1 deletion backend/lcfs/db/models/transfer/TransferHistory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from sqlalchemy import Column, Integer, ForeignKey, DateTime
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable, EffectiveDates


Expand Down
50 changes: 17 additions & 33 deletions backend/lcfs/tests/fuel_code/test_fuel_code_repo.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
from datetime import date
from unittest import mock
from unittest.mock import AsyncMock, MagicMock

import pytest
from unittest.mock import AsyncMock, MagicMock
from sqlalchemy.exc import NoResultFound
from sqlalchemy.orm import joinedload

from lcfs.web.api.fuel_code.repo import FuelCodeRepository
from lcfs.db.models.fuel.TransportMode import TransportMode
from lcfs.db.models.fuel.FuelType import FuelType
from lcfs.db.models.fuel.FuelCategory import FuelCategory
from lcfs.db.models.fuel.UnitOfMeasure import UnitOfMeasure
from lcfs.db.models.fuel.AdditionalCarbonIntensity import AdditionalCarbonIntensity
from lcfs.db.models.fuel.EnergyDensity import EnergyDensity
from lcfs.db.models.fuel.EnergyEffectivenessRatio import EnergyEffectivenessRatio
from lcfs.db.models.fuel.ExpectedUseType import ExpectedUseType
from lcfs.db.models.fuel.FuelCategory import FuelCategory
from lcfs.db.models.fuel.FuelCode import FuelCode
from lcfs.db.models.fuel.FuelCodePrefix import FuelCodePrefix
from lcfs.db.models.fuel.FuelCodeStatus import FuelCodeStatus, FuelCodeStatusEnum
from lcfs.db.models.fuel.EnergyDensity import EnergyDensity
from lcfs.db.models.fuel.EnergyEffectivenessRatio import EnergyEffectivenessRatio
from lcfs.db.models.fuel.FuelType import FuelType
from lcfs.db.models.fuel.ProvisionOfTheAct import ProvisionOfTheAct
from lcfs.db.models.fuel.AdditionalCarbonIntensity import AdditionalCarbonIntensity
from lcfs.db.models.fuel.TargetCarbonIntensity import TargetCarbonIntensity
from lcfs.db.models.compliance.CompliancePeriod import CompliancePeriod
from lcfs.db.models.fuel.TransportMode import TransportMode
from lcfs.db.models.fuel.UnitOfMeasure import UnitOfMeasure
from lcfs.web.api.fuel_code.repo import FuelCodeRepository
from lcfs.web.exception.exceptions import DatabaseException


Expand Down Expand Up @@ -585,17 +582,16 @@ async def test_get_energy_effectiveness_ratio(fuel_code_repo, mock_db):


@pytest.mark.anyio
async def test_get_target_carbon_intensities(fuel_code_repo, mock_db):
async def test_get_target_carbon_intensity(fuel_code_repo, mock_db):
tci = TargetCarbonIntensity(
target_carbon_intensity_id=1, target_carbon_intensity=50.0
)
mock_result = MagicMock()
mock_result.scalars.return_value.all.return_value = [tci]
mock_result.scalar_one.return_value = tci
mock_db.execute.return_value = mock_result

result = await fuel_code_repo.get_target_carbon_intensities(1, "2024")
assert len(result) == 1
assert result[0] == tci
result = await fuel_code_repo.get_target_carbon_intensity(1, "2024")
assert result == tci


@pytest.mark.anyio
Expand Down Expand Up @@ -625,14 +621,8 @@ async def test_get_standardized_fuel_data(fuel_code_repo, mock_db):
),
# target carbon intensities
MagicMock(
scalars=MagicMock(
return_value=MagicMock(
all=MagicMock(
return_value=[
TargetCarbonIntensity(target_carbon_intensity=50.0)
]
)
)
scalar_one=MagicMock(
return_value=TargetCarbonIntensity(target_carbon_intensity=50.0)
)
),
# additional carbon intensity
Expand Down Expand Up @@ -663,7 +653,6 @@ async def test_get_standardized_fuel_data_unrecognized(fuel_code_repo, mock_db):
mock_fuel_type = FuelType(
fuel_type_id=1,
fuel_type="UnknownFuel",
default_carbon_intensity=None,
unrecognized=True,
)

Expand Down Expand Up @@ -699,13 +688,8 @@ async def test_get_standardized_fuel_data_unrecognized(fuel_code_repo, mock_db):
)
# Target Carbon Intensities
tci_result = MagicMock(
scalars=MagicMock(
return_value=MagicMock(
all=MagicMock(
return_value=[TargetCarbonIntensity(
target_carbon_intensity=50.0)]
)
)
scalar_one=MagicMock(
return_value=TargetCarbonIntensity(target_carbon_intensity=50.0)
)
)
# Additional Carbon Intensity
Expand Down
Loading