yet another refactor
This commit is contained in:
@@ -1,47 +0,0 @@
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.helper.category import upsert_category
|
||||
from src.models import Category
|
||||
from src.models.category 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,
|
||||
)
|
||||
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()
|
||||
assert result
|
||||
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()
|
||||
assert len(result) == 1
|
||||
@@ -1,56 +1,45 @@
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user