Additional fixes for gift models

This commit is contained in:
2025-03-16 15:33:17 +01:00
parent b5f1c7ddcb
commit 19f3534f22
4 changed files with 116 additions and 34 deletions

View File

@@ -201,18 +201,32 @@ def test_create_gift_category(db_session, mock_event, mock_user):
"name": "Toys",
"description": "Fun toys for the birthday",
"icon": "toy-icon",
"color": "#FF5733",
"display_order": 1
"color": "#FF5733"
}
category_in = GiftCategoryCreate(
**category_data, event_id=mock_event.id, created_by=mock_user.id
**category_data, created_by=mock_user.id
)
category = gift_category_crud.create(db=db_session, obj_in=category_in)
# Create the association between the category and the event
from app.models.gift import EventGiftCategory
from app.schemas.gifts import EventGiftCategoryCreate
from app.crud.gift import event_gift_category_crud
association_data = EventGiftCategoryCreate(
event_id=mock_event.id,
category_id=category.id,
display_order=1,
is_visible=True
)
association = event_gift_category_crud.create(db=db_session, obj_in=association_data)
assert category.name == category_data["name"]
assert category.description == category_data["description"]
assert category.event_id == mock_event.id
assert category.created_by == mock_user.id
assert association.event_id == mock_event.id
assert association.category_id == category.id
assert association.display_order == 1
def test_get_gift_category(db_session, gift_category_fixture):
@@ -225,31 +239,48 @@ def test_get_gift_category(db_session, gift_category_fixture):
def test_get_categories_by_event(db_session, mock_event, mock_user):
"""Test retrieving all gift categories for a specific event."""
# Import necessary modules
from app.schemas.gifts import EventGiftCategoryCreate
from app.crud.gift import event_gift_category_crud
# Create multiple categories for the same event
for i in range(3):
# Create the category
category_data = {
"name": f"Category {i}",
"description": f"Description for category {i}",
"display_order": i,
"event_id": mock_event.id,
"created_by": mock_user.id
}
category_in = GiftCategoryCreate(**category_data)
gift_category_crud.create(db=db_session, obj_in=category_in)
category = gift_category_crud.create(db=db_session, obj_in=category_in)
# Create the association between the category and the event
association_data = EventGiftCategoryCreate(
event_id=mock_event.id,
category_id=category.id,
display_order=i,
is_visible=True
)
event_gift_category_crud.create(db=db_session, obj_in=association_data)
# Retrieve categories for the event
categories = gift_category_crud.get_multi_by_event(
categories = event_gift_category_crud.get_categories_by_event(
db=db_session,
event_id=mock_event.id,
skip=0,
limit=100
)
assert len(categories) >= 3
assert all(category.event_id == mock_event.id for category in categories)
# Get the associations to check display_order
from app.models.gift import EventGiftCategory
associations = db_session.query(EventGiftCategory).filter(
EventGiftCategory.event_id == mock_event.id
).order_by(EventGiftCategory.display_order).all()
# Check ordering by display_order
for i in range(len(categories) - 1):
assert categories[i].display_order <= categories[i + 1].display_order
for i in range(len(associations) - 1):
assert associations[i].display_order <= associations[i + 1].display_order
def test_update_gift_category(db_session, gift_category_fixture):
@@ -274,6 +305,24 @@ def test_update_gift_category(db_session, gift_category_fixture):
def test_delete_gift_category(db_session, gift_category_fixture):
"""Test deleting a gift category."""
# First, we need to delete the EventGiftCategory association
from app.models.gift import EventGiftCategory
from app.crud.gift import event_gift_category_crud
# Get the association
association = db_session.query(EventGiftCategory).filter(
EventGiftCategory.category_id == gift_category_fixture.id
).first()
# Delete the association
if association:
event_gift_category_crud.remove(
db=db_session,
event_id=association.event_id,
category_id=gift_category_fixture.id
)
# Now we can delete the category
category = gift_category_crud.remove(db=db_session, id=gift_category_fixture.id)
assert category.id == gift_category_fixture.id
deleted_category = gift_category_crud.get(db=db_session, id=gift_category_fixture.id)
@@ -282,17 +331,30 @@ def test_delete_gift_category(db_session, gift_category_fixture):
def test_reorder_categories(db_session, mock_event, mock_user):
"""Test reordering gift categories."""
# Import necessary modules
from app.schemas.gifts import EventGiftCategoryCreate
from app.crud.gift import event_gift_category_crud
# Create categories with initial display order
categories = []
for i in range(3):
# Create the category
category_data = {
"name": f"Category {i}",
"display_order": i,
"event_id": mock_event.id,
"created_by": mock_user.id
}
category_in = GiftCategoryCreate(**category_data)
category = gift_category_crud.create(db=db_session, obj_in=category_in)
# Create the association between the category and the event
association_data = EventGiftCategoryCreate(
event_id=mock_event.id,
category_id=category.id,
display_order=i,
is_visible=True
)
event_gift_category_crud.create(db=db_session, obj_in=association_data)
categories.append(category)
# Reorder categories (reverse order)
@@ -302,20 +364,20 @@ def test_reorder_categories(db_session, mock_event, mock_user):
categories[2].id: 0
}
updated_categories = gift_category_crud.reorder_categories(
updated_associations = event_gift_category_crud.reorder_categories(
db=db_session,
event_id=mock_event.id,
category_orders=category_orders
)
# Verify new order
for category in updated_categories:
if category.id == categories[0].id:
assert category.display_order == 2
elif category.id == categories[1].id:
assert category.display_order == 1
elif category.id == categories[2].id:
assert category.display_order == 0
for assoc in updated_associations:
if assoc.category_id == categories[0].id:
assert assoc.display_order == 2
elif assoc.category_id == categories[1].id:
assert assoc.display_order == 1
elif assoc.category_id == categories[2].id:
assert assoc.display_order == 0
# Gift Purchase Tests
@@ -446,4 +508,4 @@ def test_create_gift_item_with_invalid_quantity(db_session, mock_event, mock_use
with pytest.raises(ValueError, match="Quantity requested must be at least 1"):
gift_in = GiftItemCreate(**gift_data)
gift_item_crud.create(db=db_session, obj_in=gift_in)
gift_item_crud.create(db=db_session, obj_in=gift_in)