Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
696823406e | ||
|
|
c7e4b68dfb | ||
|
|
435f7b92bf |
@@ -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 ###
|
||||||
+2
-1
@@ -4,7 +4,8 @@ from sqlalchemy.orm import Session
|
|||||||
|
|
||||||
from src.ingest._common import _reader_raw, _upsert
|
from src.ingest._common import _reader_raw, _upsert
|
||||||
from src.ingest.transactions import consolidate_transactions, map_b4w_to_transaction
|
from src.ingest.transactions import consolidate_transactions, map_b4w_to_transaction
|
||||||
from src.models.b4w import B4wDb, B4wExport, Category, CategoryDb
|
from src.models.b4w import B4wDb, B4wExport, Category
|
||||||
|
from src.models.category import CategoryDb
|
||||||
|
|
||||||
|
|
||||||
def reader_b4w(path: Path) -> list[B4wExport]:
|
def reader_b4w(path: Path) -> list[B4wExport]:
|
||||||
|
|||||||
+15
-6
@@ -73,6 +73,10 @@ def _reader_raw_comdirect(path: Path) -> CsV2DictMulti:
|
|||||||
account_name: str | None = None
|
account_name: str | None = None
|
||||||
header: list[str] = []
|
header: list[str] = []
|
||||||
result: CsV2DictMulti = {}
|
result: CsV2DictMulti = {}
|
||||||
|
# Comdirect groups "new" transactions (marked with "neu") under a header
|
||||||
|
# line that carries only their Buchungstag; remember it until the next
|
||||||
|
# transaction row for the same account.
|
||||||
|
pending_book_date: dict[str, str] = {}
|
||||||
for line in lines:
|
for line in lines:
|
||||||
if expect_next == EXPECT_LINE.ACCOUNT_NAME:
|
if expect_next == EXPECT_LINE.ACCOUNT_NAME:
|
||||||
matches = re.findall(PATTERN_ACCOUNT_NAME, line)
|
matches = re.findall(PATTERN_ACCOUNT_NAME, line)
|
||||||
@@ -91,13 +95,18 @@ def _reader_raw_comdirect(path: Path) -> CsV2DictMulti:
|
|||||||
expect_next = EXPECT_LINE.DATA
|
expect_next = EXPECT_LINE.DATA
|
||||||
elif expect_next == EXPECT_LINE.DATA:
|
elif expect_next == EXPECT_LINE.DATA:
|
||||||
data = re.findall(PATTERN_DATA, line)
|
data = re.findall(PATTERN_DATA, line)
|
||||||
if data:
|
if not data:
|
||||||
assert account_name is not None
|
|
||||||
result[account_name].append(
|
|
||||||
{column: value for column, value in zip(header, data)}
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
expect_next = EXPECT_LINE.ACCOUNT_NAME
|
expect_next = EXPECT_LINE.ACCOUNT_NAME
|
||||||
|
elif len(data) == 1:
|
||||||
|
# Group header line (single Buchungstag for the "neu" rows below).
|
||||||
|
assert account_name is not None
|
||||||
|
pending_book_date[account_name] = data[0]
|
||||||
|
else:
|
||||||
|
assert account_name is not None
|
||||||
|
row = {column: value for column, value in zip(header, data)}
|
||||||
|
if data[0] == "neu" and account_name in pending_book_date:
|
||||||
|
row["Buchungstag"] = pending_book_date.pop(account_name)
|
||||||
|
result[account_name].append(row)
|
||||||
else:
|
else:
|
||||||
expect_next = EXPECT_LINE.ACCOUNT_NAME
|
expect_next = EXPECT_LINE.ACCOUNT_NAME
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from src.models.b4w import B4wExport as B4wExport
|
from src.models.b4w import B4wExport as B4wExport
|
||||||
from src.models.b4w import Category as Category
|
from src.models.category 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 ComdirectCreditCard as ComdirectCreditCard
|
||||||
from src.models.comdirect import ComdirectDepot as ComdirectDepot
|
from src.models.comdirect import ComdirectDepot as ComdirectDepot
|
||||||
from src.models.comdirect import ComdirectGiro as ComdirectGiro
|
from src.models.comdirect import ComdirectGiro as ComdirectGiro
|
||||||
|
|||||||
+2
-52
@@ -2,18 +2,18 @@ from datetime import date
|
|||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
from pydantic import Field, computed_field, field_validator
|
from pydantic import Field, computed_field, field_validator
|
||||||
from sqlalchemy import Date, ForeignKey, Integer, String, UniqueConstraint
|
from sqlalchemy import Date, ForeignKey, Integer, String
|
||||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
from src.constants import IBAN_PATTERN, Currency
|
from src.constants import IBAN_PATTERN, Currency
|
||||||
from src.models._common import (
|
from src.models._common import (
|
||||||
BaseModelDb,
|
|
||||||
ExportBaseModel,
|
ExportBaseModel,
|
||||||
IngestdbBase,
|
IngestdbBase,
|
||||||
Sign,
|
Sign,
|
||||||
_hash,
|
_hash,
|
||||||
date2str,
|
date2str,
|
||||||
)
|
)
|
||||||
|
from src.models.category import Category
|
||||||
|
|
||||||
|
|
||||||
class B4wExport(ExportBaseModel):
|
class B4wExport(ExportBaseModel):
|
||||||
@@ -87,56 +87,6 @@ class B4wExport(ExportBaseModel):
|
|||||||
return category
|
return category
|
||||||
|
|
||||||
|
|
||||||
class Category(ExportBaseModel):
|
|
||||||
name: str = Field(pattern=r"[^:\r\n]+")
|
|
||||||
parent: Category | None = None
|
|
||||||
|
|
||||||
@computed_field # type: ignore[prop-decorator]
|
|
||||||
@property
|
|
||||||
def hash_id(self) -> str:
|
|
||||||
return _hash(self.name, self.parent)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_str(cls, s: str) -> Category | None:
|
|
||||||
if not s:
|
|
||||||
return None
|
|
||||||
components = s.split(":")
|
|
||||||
name = components[-1]
|
|
||||||
rest = ":".join(components[:-1])
|
|
||||||
return cls(name=name, parent=cls.from_str(rest))
|
|
||||||
|
|
||||||
def to_str(self) -> str | None:
|
|
||||||
if not self.parent:
|
|
||||||
return self.name
|
|
||||||
else:
|
|
||||||
return f"{self.parent.to_str()}:{self.name}"
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return self.to_str()
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return f'Category.from_str("{self.__str__()}")'
|
|
||||||
|
|
||||||
|
|
||||||
class CategoryDb(BaseModelDb):
|
|
||||||
__tablename__ = "category"
|
|
||||||
__table_args__ = (
|
|
||||||
UniqueConstraint("name", "parent_id", name="uq_category_name_parent"),
|
|
||||||
)
|
|
||||||
|
|
||||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
||||||
name: Mapped[str] = mapped_column(String, nullable=False)
|
|
||||||
parent_id: Mapped[int | None] = mapped_column(
|
|
||||||
ForeignKey("category.id"), nullable=True
|
|
||||||
)
|
|
||||||
|
|
||||||
# Relationships
|
|
||||||
parent = relationship(
|
|
||||||
"CategoryDb", remote_side="CategoryDb.id", back_populates="children"
|
|
||||||
)
|
|
||||||
children = relationship("CategoryDb", back_populates="parent")
|
|
||||||
|
|
||||||
|
|
||||||
class B4wDb(IngestdbBase):
|
class B4wDb(IngestdbBase):
|
||||||
__tablename__ = "import_b4w"
|
__tablename__ = "import_b4w"
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
from pydantic import Field, computed_field
|
||||||
|
from sqlalchemy import ForeignKey, String, UniqueConstraint
|
||||||
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||||
|
|
||||||
|
from src.models._common import BaseModelDb, ExportBaseModel, _hash
|
||||||
|
|
||||||
|
|
||||||
|
class Category(ExportBaseModel):
|
||||||
|
name: str = Field(pattern=r"[^:\r\n]+")
|
||||||
|
parent: Category | None = None
|
||||||
|
|
||||||
|
@computed_field # type: ignore[prop-decorator]
|
||||||
|
@property
|
||||||
|
def hash_id(self) -> str:
|
||||||
|
return _hash(self.name, self.parent)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_str(cls, s: str) -> Category | None:
|
||||||
|
if not s:
|
||||||
|
return None
|
||||||
|
components = s.split(":")
|
||||||
|
name = components[-1]
|
||||||
|
rest = ":".join(components[:-1])
|
||||||
|
return cls(name=name, parent=cls.from_str(rest))
|
||||||
|
|
||||||
|
def to_str(self) -> str | None:
|
||||||
|
if not self.parent:
|
||||||
|
return self.name
|
||||||
|
else:
|
||||||
|
return f"{self.parent.to_str()}:{self.name}"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.to_str()
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'Category.from_str("{self.__str__()}")'
|
||||||
|
|
||||||
|
|
||||||
|
class CategoryDb(BaseModelDb):
|
||||||
|
__tablename__ = "category"
|
||||||
|
__table_args__ = (
|
||||||
|
UniqueConstraint("name", "parent_id", name="uq_category_name_parent"),
|
||||||
|
)
|
||||||
|
|
||||||
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||||
|
name: Mapped[str] = mapped_column(String, nullable=False)
|
||||||
|
parent_id: Mapped[int | None] = mapped_column(
|
||||||
|
ForeignKey("category.id"), nullable=True
|
||||||
|
)
|
||||||
|
|
||||||
|
# Relationships
|
||||||
|
parent = relationship(
|
||||||
|
"CategoryDb", remote_side="CategoryDb.id", back_populates="children"
|
||||||
|
)
|
||||||
|
children = relationship("CategoryDb", back_populates="parent")
|
||||||
|
budgets = relationship("CategoryBudgetDb", back_populates="category")
|
||||||
@@ -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.category 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]
|
||||||
@@ -3,7 +3,7 @@ from sqlalchemy.orm import Session
|
|||||||
|
|
||||||
from src.ingest.b4w import upsert_category
|
from src.ingest.b4w import upsert_category
|
||||||
from src.models import Category
|
from src.models import Category
|
||||||
from src.models.b4w import CategoryDb
|
from src.models.category import CategoryDb
|
||||||
|
|
||||||
|
|
||||||
def test_upsert_category_returns_orm_object(db_session: Session):
|
def test_upsert_category_returns_orm_object(db_session: Session):
|
||||||
|
|||||||
Reference in New Issue
Block a user