Files
finanz-cockpit/tests/test_category_edge_cases.py
T
mathias2784 2c7f8f4a67
Tests / test (push) Successful in 10s
Tests / lint (push) Failing after 7s
Add tests for category budgets and new functionality
New test files added:
- test_category_edge_cases.py: Tests Category computed fields, hash_id, __str__, __repr__
- test_db_utils_update_path.py: Tests for updated upsert() path handling
- test_models_common.py: Tests Sign enum, date2str, ExportBaseModel methods
- test_models_init.py: Tests for lazy loading from models.__init__.py

Code changes:
- Fixed CategoryBudget to have default=None for optional valid_from/valid_to fields
- Updated src/models/__init__.py to export CategoryBudget and CategoryDb
- Fixed CategoryBudgetDb module path bug (was pointing to non-existent .category_budget)
- Improved db_utils.upsert() to handle partial overlaps correctly

Coverage improvements:
- New functionality coverage: 100%
- Overall codebase coverage: 86% (up from 83%)

Notes:
- Some existing domain_utils.py overlap handling has bugs that prevent full coverage
- CLI entrypoint (main.py) and some enum methods remain uncovered but are optional
2026-10-16 21:53:38 +02:00

152 lines
5.2 KiB
Python

import pytest
from datetime import date
from src.models.category import Category, CategoryBudget
class TestCategoryComputedFields:
"""Tests for Category's computed fields and special methods."""
def test_hash_id_computed_field_simple(self):
"""Test hash_id for single-level category."""
c = Category.from_str("single")
assert hasattr(c, "hash_id")
assert isinstance(c.hash_id, str)
# Hash should be deterministic for same inputs
c2 = Category.from_str("single")
assert c.hash_id == c2.hash_id
def test_hash_id_computed_field_nested(self):
"""Test hash_id for multi-level category hierarchy."""
c1 = Category.from_str("level1:level2:level3")
assert hasattr(c1, "hash_id")
c2 = Category.from_str("level1:level2:level3")
assert c1.hash_id == c2.hash_id
def test_hash_id_different_hierarchy(self):
"""Test that different hierarchies have different hash_ids."""
c1 = Category.from_str("A:B")
c2 = Category.from_str("A:C")
# Different last level -> different hash_id
assert c1.hash_id != c2.hash_id
def test_hash_id_same_name_different_parent(self):
"""Test that same name but different parent gives different hash."""
root = Category(name="Budget")
cat1 = Category(name="Budget", parent=root)
# Create another with same name but no parent
orphan = Category.from_str("Budget")
assert cat1.hash_id != orphan.hash_id
def test_to_str_single_level(self):
"""Test to_str() for single-level category."""
c = Category.from_str("single")
assert c.to_str() == "single"
def test_to_str_nested(self):
"""Test to_str() for nested category."""
c = Category.from_str("parent:child:grandchild")
assert c.to_str() == "parent:child:grandchild"
def test_str_uses_to_str(self):
"""Test that __str__ delegates to to_str()."""
c = Category.from_str("A:B:C")
assert str(c) == c.to_str() == "A:B:C"
def test_repr_simple(self):
"""Test repr() for single-level category."""
c = Category.from_str("single")
expected = 'Category.from_str("single")'
assert repr(c) == expected
def test_repr_nested(self):
"""Test repr() for nested category."""
c = Category.from_str("parent:child")
expected = 'Category.from_str("parent:child")'
assert repr(c) == expected
class TestCategoryBudgetValidation:
"""Tests for CategoryBudget's date range validation."""
def test_valid_budget_no_dates(self):
"""Budget with None dates is valid."""
cat = Category.from_str("test")
budget = CategoryBudget(
category=cat,
amount_eur=100.00,
)
assert budget.valid_from is None
assert budget.valid_to is None
def test_valid_budget_with_dates(self):
"""Budget with valid date range is accepted."""
from datetime import date
cat = Category.from_str("test")
budget = CategoryBudget(
category=cat,
amount_eur=100.00,
valid_from=date(2026, 10, 1),
valid_to=date(2026, 10, 31),
)
assert budget.valid_from == date(2026, 10, 1)
assert budget.valid_to == date(2026, 10, 31)
def test_invalid_date_range_raises(self):
"""Budget with valid_from > valid_to raises ValueError."""
from datetime import date
cat = Category.from_str("test")
with pytest.raises(ValueError) as exc_info:
CategoryBudget(
category=cat,
amount_eur=100.00,
valid_from=date(2026, 10, 31),
valid_to=date(2026, 10, 1),
)
assert "valid_from" in str(exc_info.value)
assert "valid_to" in str(exc_info.value)
assert "nor in order" in str(exc_info.value)
def test_valid_budget_exact_boundary(self):
"""Budget with exact same from and to is valid (single day)."""
from datetime import date
cat = Category.from_str("test")
budget = CategoryBudget(
category=cat,
amount_eur=100.00,
valid_from=date(2026, 10, 1),
valid_to=date(2026, 10, 1),
)
assert budget.valid_from == date(2026, 10, 1)
assert budget.valid_to == date(2026, 10, 1)
def test_validation_preserves_original_values(self):
"""Validation doesn't change the model's original values."""
from datetime import date
cat = Category.from_str("test")
budget = CategoryBudget(
category=cat,
amount_eur=150.75,
valid_from=date(2026, 9, 1),
valid_to=date(2026, 9, 30),
)
# Create an invalid one and verify validation catches it
try:
bad_budget = CategoryBudget(
category=cat,
amount_eur=200.00,
valid_from=date(2026, 12, 31),
valid_to=date(2026, 12, 30),
)
except ValueError:
pass # Expected
# Valid one should still have original values
assert budget.amount_eur == 150.75