Skip to content

Commit

Permalink
Cleanup UQs and Add Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaselhan committed Dec 19, 2024
1 parent b6d0355 commit c4edf63
Show file tree
Hide file tree
Showing 24 changed files with 123 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Add UQ to TCI
Revision ID: ab04810d4d7c
Revises: 851e09cf8661
Create Date: 2024-12-19 23:46:37.505166
"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "ab04810d4d7c"
down_revision = "851e09cf8661"
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
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
21 changes: 9 additions & 12 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# test_fuel_supply.py

from datetime import datetime
from unittest.mock import AsyncMock
from uuid import uuid4

Expand Down
12 changes: 5 additions & 7 deletions backend/lcfs/tests/fuel_supply/test_fuel_supplies_services.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import uuid
from unittest.mock import MagicMock, AsyncMock

import pytest
from unittest.mock import MagicMock, AsyncMock, patch
from fastapi import HTTPException

from lcfs.db.base import UserTypeEnum, ActionTypeEnum
from lcfs.db.models import (
FuelType,
EnergyEffectivenessRatio,
EnergyDensity,
FuelCategory,
)
from lcfs.db.base import UserTypeEnum, ActionTypeEnum
from lcfs.web.api.fuel_supply.actions_service import FuelSupplyActionService
from lcfs.db.models.compliance.FuelSupply import FuelSupply
from lcfs.web.api.fuel_code.repo import FuelCodeRepository
from lcfs.web.api.fuel_supply.actions_service import FuelSupplyActionService
from lcfs.web.api.fuel_supply.repo import FuelSupplyRepository
from lcfs.web.api.fuel_supply.schema import (
FuelSupplyCreateUpdateSchema,
Expand All @@ -21,10 +21,8 @@
FuelTypeSchema,
FuelCategoryResponseSchema,
)
from lcfs.db.models.compliance.FuelSupply import FuelSupply
from lcfs.web.api.fuel_supply.services import FuelSupplyServices


# Fixture to set up the FuelSupplyServices with mocked dependencies
# Mock common fuel type and fuel category for reuse
fuel_type = FuelTypeSchema(
Expand Down
Loading

0 comments on commit c4edf63

Please sign in to comment.