This commit is contained in:
2026-09-06 12:26:53 +02:00
parent bbe1406afb
commit d2d7c7fa74
8 changed files with 179 additions and 51 deletions
+2 -3
View File
@@ -1,9 +1,8 @@
from polyfactory.factories.pydantic_factory import ModelFactory
from src.models import B4wExport
DEFAULT_CATEGORY = "root_category:parent_category:leaf_category"
class B4wExportFactory(ModelFactory[B4wExport]):
...
class B4wExportFactory(ModelFactory[B4wExport]): ...
+26 -11
View File
@@ -7,26 +7,41 @@ from src.models_db import CategoryDb
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)
result = upsert_category(
db_session,
Category.from_str("dummy_parent_1:dummy_parent_2:dummy"),
do_commit=False,
)
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_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()
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"
assert str(result.name) == "new_category"
assert str(result.parent.name) == "father"
assert str(result.parent.parent.name) == "grand_father"
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()
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
+12 -4
View File
@@ -7,6 +7,7 @@ def test_single_level_category():
assert c.name == "single"
assert c.parent is None
def test_two_level_category():
c = Category.from_str("first:second")
assert c
@@ -15,6 +16,7 @@ def test_two_level_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
@@ -25,24 +27,30 @@ def test_three_level_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"))
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")))
c = Category(
name="third", parent=Category(name="second", parent=Category(name="first"))
)
assert c.to_str() == "first:second:third"
def test_category_str():
c = Category.from_str("first:second:third")
assert str(c) == "first:second:third"
def test_category_repr():
c = Category.from_str("first:second:third")
assert repr(c) == "Category.from_str(\"first:second:third\")"
assert repr(c) == 'Category.from_str("first:second:third")'