Add category_budget satellite table for time-bounded per-category budgets
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
"""create category_budget table
|
||||
|
||||
Revision ID: fe23064de1c5
|
||||
Revises: 78ec55b9e1d1
|
||||
Create Date: 2026-10-16 10:43:24.952699
|
||||
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
|
||||
from alembic import op
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "fe23064de1c5"
|
||||
down_revision: str | Sequence[str] | None = "78ec55b9e1d1"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""Upgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.create_table(
|
||||
"category_budget",
|
||||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
|
||||
sa.Column("category_id", sa.Integer(), nullable=False),
|
||||
sa.Column("amount_eur", sa.Numeric(precision=12, scale=2), nullable=False),
|
||||
sa.Column("valid_from", sa.Date(), nullable=True),
|
||||
sa.Column("valid_to", sa.Date(), nullable=True),
|
||||
sa.ForeignKeyConstraint(
|
||||
["category_id"],
|
||||
["category.id"],
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
op.create_index(
|
||||
"ix_category_budget_category_id",
|
||||
"category_budget",
|
||||
["category_id"],
|
||||
unique=False,
|
||||
)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""Downgrade schema."""
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_index("ix_category_budget_category_id", table_name="category_budget")
|
||||
op.drop_table("category_budget")
|
||||
# ### end Alembic commands ###
|
||||
@@ -1,5 +1,6 @@
|
||||
from src.models.b4w import B4wExport as B4wExport
|
||||
from src.models.b4w import Category as Category
|
||||
from src.models.category_budget import CategoryBudgetDb as CategoryBudgetDb
|
||||
from src.models.comdirect import ComdirectCreditCard as ComdirectCreditCard
|
||||
from src.models.comdirect import ComdirectDepot as ComdirectDepot
|
||||
from src.models.comdirect import ComdirectGiro as ComdirectGiro
|
||||
|
||||
@@ -135,6 +135,7 @@ class CategoryDb(BaseModelDb):
|
||||
"CategoryDb", remote_side="CategoryDb.id", back_populates="children"
|
||||
)
|
||||
children = relationship("CategoryDb", back_populates="parent")
|
||||
budgets = relationship("CategoryBudgetDb", back_populates="category")
|
||||
|
||||
|
||||
class B4wDb(IngestdbBase):
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
|
||||
from sqlalchemy import Date, ForeignKey, Numeric
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from src.models._common import BaseModelDb
|
||||
|
||||
|
||||
class CategoryBudgetDb(BaseModelDb):
|
||||
"""Budget amount for a category, optionally bounded to a time window.
|
||||
|
||||
Satellite table to ``category``: one category may have several budgets
|
||||
(e.g. one per month). ``valid_from``/``valid_to`` are nullable; ``None``
|
||||
means unbounded on that side.
|
||||
"""
|
||||
|
||||
__tablename__ = "category_budget"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
category_id: Mapped[int] = mapped_column(ForeignKey("category.id"), nullable=False)
|
||||
amount_eur: Mapped[Decimal] = mapped_column(Numeric(12, 2), nullable=False)
|
||||
valid_from: Mapped[date | None] = mapped_column(Date, nullable=True, default=None)
|
||||
valid_to: Mapped[date | None] = mapped_column(Date, nullable=True, default=None)
|
||||
|
||||
# relationship
|
||||
category = relationship("CategoryDb", back_populates="budgets")
|
||||
@@ -0,0 +1,112 @@
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
|
||||
from sqlalchemy import inspect
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.models.b4w import CategoryDb
|
||||
from src.models.category_budget import CategoryBudgetDb
|
||||
|
||||
|
||||
def test_category_budget_schema(db_session: Session):
|
||||
inspector = inspect(db_session.get_bind())
|
||||
columns = {c["name"]: c for c in inspector.get_columns("category_budget")}
|
||||
|
||||
assert set(columns) == {
|
||||
"id",
|
||||
"category_id",
|
||||
"amount_eur",
|
||||
"valid_from",
|
||||
"valid_to",
|
||||
}
|
||||
assert columns["id"]["nullable"] is False
|
||||
assert columns["category_id"]["nullable"] is False
|
||||
assert columns["amount_eur"]["nullable"] is False
|
||||
assert columns["valid_from"]["nullable"] is True
|
||||
assert columns["valid_to"]["nullable"] is True
|
||||
assert str(columns["amount_eur"]["type"]) == "NUMERIC(12, 2)"
|
||||
|
||||
foreign_keys = inspector.get_foreign_keys("category_budget")
|
||||
assert len(foreign_keys) == 1
|
||||
assert foreign_keys[0]["constrained_columns"] == ["category_id"]
|
||||
assert foreign_keys[0]["referred_table"] == "category"
|
||||
|
||||
|
||||
def test_category_budget_defaults_null_dates(db_session: Session):
|
||||
category = CategoryDb(name="Lebenshaltung")
|
||||
db_session.add(category)
|
||||
db_session.flush()
|
||||
|
||||
budget = CategoryBudgetDb(category_id=category.id, amount_eur=Decimal("150.00"))
|
||||
db_session.add(budget)
|
||||
db_session.commit()
|
||||
db_session.refresh(budget)
|
||||
|
||||
assert budget.id is not None
|
||||
assert budget.category_id == category.id
|
||||
assert budget.amount_eur == Decimal("150.00")
|
||||
# Null means unbounded in either direction.
|
||||
assert budget.valid_from is None
|
||||
assert budget.valid_to is None
|
||||
|
||||
|
||||
def test_category_budget_date_window(db_session: Session):
|
||||
category = CategoryDb(name="Freizeit")
|
||||
db_session.add(category)
|
||||
db_session.flush()
|
||||
|
||||
budget = CategoryBudgetDb(
|
||||
category_id=category.id,
|
||||
amount_eur=Decimal("200.50"),
|
||||
valid_from=date(2026, 10, 1),
|
||||
valid_to=date(2026, 10, 31),
|
||||
)
|
||||
db_session.add(budget)
|
||||
db_session.commit()
|
||||
db_session.refresh(budget)
|
||||
|
||||
assert budget.valid_from == date(2026, 10, 1)
|
||||
assert budget.valid_to == date(2026, 10, 31)
|
||||
|
||||
|
||||
def test_category_budget_relationship_both_ways(db_session: Session):
|
||||
category = CategoryDb(name="Beruf")
|
||||
db_session.add(category)
|
||||
db_session.flush()
|
||||
|
||||
budget = CategoryBudgetDb(category_id=category.id, amount_eur=Decimal("1.00"))
|
||||
db_session.add(budget)
|
||||
db_session.commit()
|
||||
|
||||
# Force re-read from the database so the relationship is loaded, not
|
||||
# just resolved from the session identity map.
|
||||
db_session.expire_all()
|
||||
assert [b.id for b in category.budgets] == [budget.id]
|
||||
assert budget.category.id == category.id
|
||||
|
||||
|
||||
def test_category_has_multiple_budgets(db_session: Session):
|
||||
category = CategoryDb(name="Wohnen")
|
||||
db_session.add(category)
|
||||
db_session.flush()
|
||||
|
||||
budgets = [
|
||||
CategoryBudgetDb(
|
||||
category_id=category.id,
|
||||
amount_eur=Decimal("1200.00"),
|
||||
valid_from=date(2026, 10, 1),
|
||||
valid_to=date(2026, 10, 31),
|
||||
),
|
||||
CategoryBudgetDb(
|
||||
category_id=category.id,
|
||||
amount_eur=Decimal("1300.00"),
|
||||
valid_from=date(2026, 11, 1),
|
||||
valid_to=date(2026, 11, 30),
|
||||
),
|
||||
]
|
||||
for budget in budgets:
|
||||
db_session.add(budget)
|
||||
db_session.commit()
|
||||
db_session.expire_all()
|
||||
|
||||
assert [b.id for b in category.budgets] == [b.id for b in budgets]
|
||||
Reference in New Issue
Block a user