Category overhaul (#20)
Tests / test (push) Successful in 16s
Tests / lint (push) Successful in 16s

Reviewed-on: #20
Co-authored-by: Mathias <mathias2784@web.de>
This commit was merged in pull request #20.
This commit is contained in:
2026-10-27 18:08:10 +00:00
committed by mathias2784
parent 4efef98c9c
commit 6babc560c7
36 changed files with 3021 additions and 176 deletions
+86 -49
View File
@@ -1,56 +1,93 @@
from src.models import Category
from sqlalchemy import select
from sqlalchemy.orm import Session
from src.models.category import Category, CategoryDb, upsert_category
def test_single_level_category():
c = Category.from_str("single")
assert c
assert c.name == "single"
assert c.parent is None
def test_two_level_category():
c = Category.from_str("first:second")
assert c
assert c.name == "second"
assert isinstance(c.parent, Category)
assert c.parent.name == "first"
assert c.parent.parent is None
def test_three_level_category():
c = Category.from_str("first:second:third")
assert c
assert c.name == "third"
assert isinstance(c.parent, Category)
assert c.parent.name == "second"
assert isinstance(c.parent.parent, Category)
assert c.parent.parent.name == "first"
assert c.parent.parent.parent is None
def test_single_level_category_to_string():
c = Category.from_str("single")
assert c
assert str(c) == "single"
def test_two_level_category_to_string():
c = Category(name="second", parent=Category(name="first"))
assert c.to_str() == "first:second"
def test_three_level_category_to_string():
c = Category(
name="third", parent=Category(name="second", parent=Category(name="first"))
def test_upsert_category_returns_orm_object(db_session: Session):
result = upsert_category(
db_session,
Category.from_str("dummy_parent_1:dummy_parent_2:dummy"),
do_commit=False,
)
assert c.to_str() == "first:second:third"
assert result
assert isinstance(result, CategoryDb)
assert str(result.name) == "dummy"
assert str(result.parent.name) == "dummy_parent_2"
assert str(result.parent.parent.name) == "dummy_parent_1"
def test_category_str():
c = Category.from_str("first:second:third")
assert str(c) == "first:second:third"
def test_upsert_category_writes_new_to_db(db_session: Session):
upsert_category(
db_session,
Category.from_str("grand_father:father:new_category"),
do_commit=True,
)
result = db_session.execute(
select(CategoryDb).filter_by(name="new_category")
).scalar_one()
assert result
assert str(result.name) == "new_category"
assert str(result.parent.name) == "father"
assert str(result.parent.parent.name) == "grand_father"
def test_category_repr():
c = Category.from_str("first:second:third")
assert repr(c) == 'Category.from_str("first:second:third")'
def test_upsert_category_skips_existing(db_session: Session):
for _ in range(3):
upsert_category(
db_session,
Category.from_str("grand_father:father:try_again_and_again"),
do_commit=True,
)
result = db_session.execute(
select(CategoryDb).filter_by(name="try_again_and_again")
).all()
assert len(result) == 1
def test_category_hash_id_computed_field():
"""Test hash_id computed field is present and deterministic."""
c1 = Category.from_str("single")
c2 = Category.from_str("single")
assert hasattr(c1, "hash_id")
assert c1.hash_id == c2.hash_id
def test_category_hash_id_different_inputs():
"""Test that different category 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_category_to_str_single_level():
"""Test to_str() for single-level category."""
c = Category.from_str("single")
assert c.to_str() == "single"
def test_category_to_str_nested():
"""Test to_str() for nested category."""
c = Category.from_str("parent:child:grandchild")
assert c.to_str() == "parent:child:grandchild"
def test_category_str_delegates_to_to_str():
"""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_category_repr_simple():
"""Test repr() for single-level category."""
c = Category.from_str("single")
expected = 'Category.from_str("single")'
assert repr(c) == expected
def test_category_repr_nested():
"""Test repr() for nested category."""
c = Category.from_str("parent:child")
expected = 'Category.from_str("parent:child")'
assert repr(c) == expected