add interest accural test, improve migration tests
All checks were successful
Backend CI / unit-test (push) Successful in 37s

This commit is contained in:
2025-09-25 22:16:24 +02:00
parent 27b4adaca4
commit 6a5f160d83
5 changed files with 366 additions and 9 deletions

View File

@@ -1,6 +1,6 @@
from __future__ import annotations
from datetime import datetime, timedelta, timezone
from datetime import date, datetime, timedelta, timezone
from typing import TYPE_CHECKING, Any, TypeVar, cast
from pydantic import BaseModel
@@ -13,6 +13,8 @@ if TYPE_CHECKING:
from collections.abc import Mapping
from enum import Enum
from sqlalchemy.sql.elements import ColumnElement
# Generic enum member type
T = TypeVar("T", bound="Enum")
@@ -301,6 +303,93 @@ def update_cycle(session: Session, cycle_id: int, update_data: Mapping[str, Any]
return cycle
# Cycle loan and interest
def create_cycle_loan_event(session: Session, loan_data: Mapping[str, Any] | BaseModel) -> models.CycleLoanChangeEvents:
data = _data_to_dict(loan_data)
allowed = _allowed_columns(models.CycleLoanChangeEvents)
payload = {k: v for k, v in data.items() if k in allowed}
if "cycle_id" not in payload:
raise ValueError("cycle_id is required")
cycle = session.get(models.Cycles, payload["cycle_id"])
if cycle is None:
raise ValueError("cycle_id does not exist")
payload["effective_date"] = payload.get("effective_date") or datetime.now(timezone.utc).date()
payload["created_at"] = datetime.now(timezone.utc)
cle = models.CycleLoanChangeEvents(**payload)
session.add(cle)
try:
session.flush()
except IntegrityError as e:
session.rollback()
raise ValueError("create_cycle_loan_event integrity error") from e
session.refresh(cle)
return cle
def get_loan_events_by_cycle_id(session: Session, cycle_id: int) -> list[models.CycleLoanChangeEvents]:
eff_col = cast("ColumnElement", models.CycleLoanChangeEvents.effective_date)
id_col = cast("ColumnElement", models.CycleLoanChangeEvents.id)
statement = (
select(models.CycleLoanChangeEvents)
.where(
models.CycleLoanChangeEvents.cycle_id == cycle_id,
)
.order_by(eff_col, id_col.asc())
)
return list(session.exec(statement).all())
def create_cycle_daily_accrual(session: Session, cycle_id: int, accrual_date: date, accrual_amount_cents: int) -> models.CycleDailyAccrual:
cycle = session.get(models.Cycles, cycle_id)
if cycle is None:
raise ValueError("cycle_id does not exist")
existing = session.exec(
select(models.CycleDailyAccrual).where(
models.CycleDailyAccrual.cycle_id == cycle_id,
models.CycleDailyAccrual.accrual_date == accrual_date,
),
).first()
if existing:
return existing
if accrual_amount_cents < 0:
raise ValueError("accrual_amount_cents must be non-negative")
row = models.CycleDailyAccrual(
cycle_id=cycle_id,
accrual_date=accrual_date,
accrual_amount_cents=accrual_amount_cents,
created_at=datetime.now(timezone.utc),
)
session.add(row)
try:
session.flush()
except IntegrityError as e:
session.rollback()
raise ValueError("create_cycle_daily_accrual integrity error") from e
session.refresh(row)
return row
def get_cycle_daily_accruals_by_cycle_id(session: Session, cycle_id: int) -> list[models.CycleDailyAccrual]:
date_col = cast("ColumnElement", models.CycleDailyAccrual.accrual_date)
statement = (
select(models.CycleDailyAccrual)
.where(
models.CycleDailyAccrual.cycle_id == cycle_id,
)
.order_by(date_col.asc())
)
return list(session.exec(statement).all())
def get_cycle_daily_accrual_by_cycle_id_and_date(session: Session, cycle_id: int, accrual_date: date) -> models.CycleDailyAccrual | None:
statement = select(models.CycleDailyAccrual).where(
models.CycleDailyAccrual.cycle_id == cycle_id,
models.CycleDailyAccrual.accrual_date == accrual_date,
)
return session.exec(statement).first()
# Exchanges
IMMUTABLE_EXCHANGE_FIELDS = {"id"}

View File

@@ -18,8 +18,10 @@ from sqlmodel import (
class TradeType(str, Enum):
SELL_PUT = "SELL_PUT"
CLOSE_SELL_PUT = "CLOSE_SELL_PUT"
ASSIGNMENT = "ASSIGNMENT"
SELL_CALL = "SELL_CALL"
CLOSE_SELL_CALL = "CLOSE_SELL_CALL"
EXERCISE_CALL = "EXERCISE_CALL"
LONG_SPOT = "LONG_SPOT"
CLOSE_LONG_SPOT = "CLOSE_LONG_SPOT"
@@ -117,13 +119,17 @@ class Cycles(SQLModel, table=True):
status: CycleStatus = Field(sa_column=Column(Text, nullable=False))
funding_source: FundingSource = Field(sa_column=Column(Text, nullable=True))
capital_exposure_cents: int | None = Field(default=None, nullable=True)
loan_amount_cents: int | None = Field(default=None, nullable=True)
loan_interest_rate_tenth_bps: int | None = Field(default=None, nullable=True)
start_date: date = Field(sa_column=Column(Date, nullable=False))
end_date: date | None = Field(default=None, sa_column=Column(Date, nullable=True))
trades: list["Trades"] = Relationship(back_populates="cycle")
loan_amount_cents: int | None = Field(default=None, nullable=True)
loan_interest_rate_tenth_bps: int | None = Field(default=None, nullable=True)
latest_interest_accrued_date: date | None = Field(default=None, sa_column=Column(Date, nullable=True))
total_accrued_amount_cents: int = Field(default=0, sa_column=Column(Integer, nullable=False))
loan_change_events: list["CycleLoanChangeEvents"] = Relationship(back_populates="cycle")
daily_accruals: list["CycleDailyAccrual"] = Relationship(back_populates="cycle")
@@ -131,7 +137,7 @@ class Cycles(SQLModel, table=True):
class CycleLoanChangeEvents(SQLModel, table=True):
__tablename__ = "cycle_loan_change_events" # type: ignore[attr-defined]
id: int | None = Field(default=None, primary_key=True)
cycle_id: int = Field(foreign_key="cycles.id", nullable=False, index=True)
cycle_id: int = Field(sa_column=Column(Integer, ForeignKey("cycles.id", ondelete="CASCADE"), nullable=False, index=True))
effective_date: date = Field(sa_column=Column(Date, nullable=False))
loan_amount_cents: int | None = Field(default=None, sa_column=Column(Integer, nullable=True))
loan_interest_rate_tenth_bps: int | None = Field(default=None, sa_column=Column(Integer, nullable=True))
@@ -148,7 +154,7 @@ class CycleDailyAccrual(SQLModel, table=True):
__table_args__ = (UniqueConstraint("cycle_id", "accrual_date", name="uq_cycle_daily_accruals_cycle_date"),)
id: int | None = Field(default=None, primary_key=True)
cycle_id: int = Field(foreign_key="cycles.id", nullable=False, index=True)
cycle_id: int = Field(sa_column=Column(Integer, ForeignKey("cycles.id", ondelete="CASCADE"), nullable=False, index=True))
accrual_date: date = Field(sa_column=Column(Date, nullable=False))
accrual_amount_cents: int = Field(sa_column=Column(Integer, nullable=False))
created_at: datetime = Field(sa_column=Column(DateTime(timezone=True), nullable=False))

View File

@@ -18,8 +18,10 @@ from sqlmodel import (
class TradeType(str, Enum):
SELL_PUT = "SELL_PUT"
CLOSE_SELL_PUT = "CLOSE_SELL_PUT"
ASSIGNMENT = "ASSIGNMENT"
SELL_CALL = "SELL_CALL"
CLOSE_SELL_CALL = "CLOSE_SELL_CALL"
EXERCISE_CALL = "EXERCISE_CALL"
LONG_SPOT = "LONG_SPOT"
CLOSE_LONG_SPOT = "CLOSE_LONG_SPOT"
@@ -117,13 +119,17 @@ class Cycles(SQLModel, table=True):
status: CycleStatus = Field(sa_column=Column(Text, nullable=False))
funding_source: FundingSource = Field(sa_column=Column(Text, nullable=True))
capital_exposure_cents: int | None = Field(default=None, nullable=True)
loan_amount_cents: int | None = Field(default=None, nullable=True)
loan_interest_rate_tenth_bps: int | None = Field(default=None, nullable=True)
start_date: date = Field(sa_column=Column(Date, nullable=False))
end_date: date | None = Field(default=None, sa_column=Column(Date, nullable=True))
trades: list["Trades"] = Relationship(back_populates="cycle")
loan_amount_cents: int | None = Field(default=None, nullable=True)
loan_interest_rate_tenth_bps: int | None = Field(default=None, nullable=True)
latest_interest_accrued_date: date | None = Field(default=None, sa_column=Column(Date, nullable=True))
total_accrued_amount_cents: int = Field(default=0, sa_column=Column(Integer, nullable=False))
loan_change_events: list["CycleLoanChangeEvents"] = Relationship(back_populates="cycle")
daily_accruals: list["CycleDailyAccrual"] = Relationship(back_populates="cycle")
@@ -131,7 +137,7 @@ class Cycles(SQLModel, table=True):
class CycleLoanChangeEvents(SQLModel, table=True):
__tablename__ = "cycle_loan_change_events" # type: ignore[attr-defined]
id: int | None = Field(default=None, primary_key=True)
cycle_id: int = Field(foreign_key="cycles.id", nullable=False, index=True)
cycle_id: int = Field(sa_column=Column(Integer, ForeignKey("cycles.id", ondelete="CASCADE"), nullable=False, index=True))
effective_date: date = Field(sa_column=Column(Date, nullable=False))
loan_amount_cents: int | None = Field(default=None, sa_column=Column(Integer, nullable=True))
loan_interest_rate_tenth_bps: int | None = Field(default=None, sa_column=Column(Integer, nullable=True))
@@ -148,7 +154,7 @@ class CycleDailyAccrual(SQLModel, table=True):
__table_args__ = (UniqueConstraint("cycle_id", "accrual_date", name="uq_cycle_daily_accruals_cycle_date"),)
id: int | None = Field(default=None, primary_key=True)
cycle_id: int = Field(foreign_key="cycles.id", nullable=False, index=True)
cycle_id: int = Field(sa_column=Column(Integer, ForeignKey("cycles.id", ondelete="CASCADE"), nullable=False, index=True))
accrual_date: date = Field(sa_column=Column(Date, nullable=False))
accrual_amount_cents: int = Field(sa_column=Column(Integer, nullable=False))
created_at: datetime = Field(sa_column=Column(DateTime(timezone=True), nullable=False))