Skip to content

Commit

Permalink
Merge branch 'release-0.2.0' into fix/alex-marine-end-use-1498
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexZorkin authored Jan 3, 2025
2 parents d7e264f + 85b7147 commit dc03063
Show file tree
Hide file tree
Showing 66 changed files with 1,066 additions and 368 deletions.
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
6 changes: 0 additions & 6 deletions backend/lcfs/db/models/compliance/listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,3 @@ def prevent_update_if_locked(mapper, connection, target):
raise InvalidRequestError("Cannot update a locked ComplianceReportSummary")


@event.listens_for(ComplianceReportSummary.is_locked, "set")
def prevent_unlock(target, value, oldvalue, initiator):
if oldvalue and not value:
raise InvalidRequestError(
"Cannot unlock a ComplianceReportSummary once it's locked"
)
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
12 changes: 9 additions & 3 deletions backend/lcfs/tests/audit_log/test_audit_log_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ async def test_get_audit_logs_paginated_no_data(audit_log_service, mock_repo):
pagination = PaginationRequestSchema(page=1, size=10, filters=[], sort_orders=[])
mock_repo.get_audit_logs_paginated.return_value = ([], 0)

# Act & Assert
with pytest.raises(DataNotFoundException):
await audit_log_service.get_audit_logs_paginated(pagination)
# Act
result = await audit_log_service.get_audit_logs_paginated(pagination)

# Assert
assert result.audit_logs == [], "Should return an empty list of audit logs"
assert result.pagination.total == 0, "Total should be zero if no records"
assert result.pagination.page == pagination.page
assert result.pagination.size == pagination.size
assert result.pagination.total_pages == 0


@pytest.mark.anyio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,30 @@ async def test_get_compliance_reports_paginated_success(
async def test_get_compliance_reports_paginated_not_found(
compliance_report_service, mock_repo
):
# Arrange
pagination_mock = AsyncMock()
pagination_mock.page = 1
pagination_mock.size = 10
pagination_mock.filters = []
pagination_mock.sort_orders = []

# Mock the repository to return no records
mock_repo.get_reports_paginated.return_value = ([], 0)

with pytest.raises(DataNotFoundException):
await compliance_report_service.get_compliance_reports_paginated(
pagination_mock
)
# Act
result = await compliance_report_service.get_compliance_reports_paginated(
pagination_mock
)

# Assert: Verify the service returns an empty list and correct pagination metadata
assert result.reports == [], "Expected no compliance reports to be returned"
assert result.pagination.total == 0, "Expected total=0 when there are no records"
assert result.pagination.page == 1, "Page should match the requested page"
assert result.pagination.size == 10, "Size should match the requested size"
assert result.pagination.total_pages == 0, "0 records should yield 0 total_pages"

# Also verify our repo was called exactly once
mock_repo.get_reports_paginated.assert_called_once()


@pytest.mark.anyio
Expand Down
10 changes: 0 additions & 10 deletions backend/lcfs/tests/compliance_report/test_update_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,16 +281,6 @@ async def test_handle_submitted_status_with_existing_summary(
report_id
)

# Ensure the adjust_balance method is called with the correct parameters
mock_org_service.adjust_balance.assert_called_once_with(
transaction_action=TransactionActionEnum.Reserved,
compliance_units=mock_report.summary.line_20_surplus_deficit_units,
organization_id=mock_report.organization_id,
)

# Check if the report was updated with the result of adjust_balance
assert mock_report.transaction == mock_org_service.adjust_balance.return_value

# Check if the summary is locked
saved_summary = mock_repo.save_compliance_report_summary.call_args[0][0]
assert saved_summary.is_locked == True
Expand Down
Loading

0 comments on commit dc03063

Please sign in to comment.