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

@@ -232,7 +232,7 @@ def read_gift_category(
elif include_gifts:
# If no event_id but include_gifts is true, just get all gifts for this category
# This is less useful without event context but included for completeness
gifts = db.query(GiftItem).filter(GiftItem.category_id == category_id).all()
gifts = db.query(GiftItemModel).filter(GiftItemModel.category_id == category_id).all()
gifts_list = gifts
# Create a new category response with the calculated values

View File

@@ -116,8 +116,6 @@ class GiftCategoryBase(BaseModel):
description: Optional[str] = None
icon: Optional[str] = None
color: Optional[str] = None
display_order: Optional[int] = 0
is_visible: Optional[bool] = True
custom_fields: Optional[Dict[str, Any]] = None

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)

View File

@@ -135,7 +135,6 @@ def test_create_gift_category(db_session, mock_user):
# Arrange
category = GiftCategory(
id=uuid.uuid4(),
event_id=uuid.uuid4(),
created_by=mock_user.id,
name="Toys",
description="All kinds of educational toys."
@@ -153,18 +152,29 @@ def test_create_gift_category(db_session, mock_user):
def test_gift_category_total_gifts(db_session, mock_user):
# Arrange
event_id = uuid.uuid4()
category = GiftCategory(
id=uuid.uuid4(),
event_id=uuid.uuid4(),
created_by=mock_user.id,
name="Baby Essentials",
)
db_session.add(category)
db_session.commit()
# Create the association between the category and the event
from app.models.gift import EventGiftCategory
event_gift_category = EventGiftCategory(
event_id=event_id,
category_id=category.id,
display_order=0,
is_visible=True
)
db_session.add(event_gift_category)
db_session.commit()
gift1 = GiftItem(
id=uuid.uuid4(),
event_id=category.event_id,
event_id=event_id,
category_id=category.id,
name="Bottle Set",
quantity_requested=2,
@@ -173,7 +183,7 @@ def test_gift_category_total_gifts(db_session, mock_user):
)
gift2 = GiftItem(
id=uuid.uuid4(),
event_id=category.event_id,
event_id=event_id,
category_id=category.id,
name="Diaper Bag",
quantity_requested=1,
@@ -200,6 +210,12 @@ def test_gift_category_available_gifts(db_session, gift_category_fixture, gift_i
"""
Test that the available_gifts method correctly counts the available gifts.
"""
# Get the event_id from the EventGiftCategory association
from app.models.gift import EventGiftCategory
event_gift_category = db_session.query(EventGiftCategory).filter_by(
category_id=gift_category_fixture.id
).first()
# Update gift_item_fixture to match the gift_category_fixture
gift_item = GiftItem(
name="Toy Car",
@@ -211,7 +227,7 @@ def test_gift_category_available_gifts(db_session, gift_category_fixture, gift_i
status=GiftStatus.AVAILABLE,
is_visible=True,
category_id=gift_category_fixture.id,
event_id=gift_category_fixture.event_id, # Ensure event linkage
event_id=event_gift_category.event_id, # Get event_id from association
added_by=gift_category_fixture.created_by # Link to a user
)
db_session.add(gift_item)
@@ -225,6 +241,12 @@ def test_gift_category_reorder_gifts(db_session, gift_category_fixture, mock_use
Test that the reorder_gifts method correctly updates gift display_order
based on the provided order of gift IDs.
"""
# Get the event_id from the EventGiftCategory association
from app.models.gift import EventGiftCategory
event_gift_category = db_session.query(EventGiftCategory).filter_by(
category_id=gift_category_fixture.id
).first()
# Create GiftItems with proper required fields and UUID objects (not strings)
gift1_id = uuid.uuid4()
gift2_id = uuid.uuid4()
@@ -235,7 +257,7 @@ def test_gift_category_reorder_gifts(db_session, gift_category_fixture, mock_use
name="Gift 1",
display_order=0,
category_id=gift_category_fixture.id,
event_id=gift_category_fixture.event_id, # Required field
event_id=event_gift_category.event_id, # Get event_id from association
added_by=gift_category_fixture.created_by, # Required field
status=GiftStatus.AVAILABLE # Required field
)
@@ -244,7 +266,7 @@ def test_gift_category_reorder_gifts(db_session, gift_category_fixture, mock_use
name="Gift 2",
display_order=1,
category_id=gift_category_fixture.id,
event_id=gift_category_fixture.event_id,
event_id=event_gift_category.event_id, # Get event_id from association
added_by=gift_category_fixture.created_by,
status=GiftStatus.AVAILABLE
)
@@ -253,7 +275,7 @@ def test_gift_category_reorder_gifts(db_session, gift_category_fixture, mock_use
name="Gift 3",
display_order=2,
category_id=gift_category_fixture.id,
event_id=gift_category_fixture.event_id,
event_id=event_gift_category.event_id, # Get event_id from association
added_by=gift_category_fixture.created_by,
status=GiftStatus.AVAILABLE
)
@@ -345,4 +367,4 @@ def test_gift_purchase_repr(db_session, gift_purchase_fixture):
repr_output = repr(gift_purchase_fixture)
# Assert
assert repr_output == f"<GiftPurchase gift_id={gift_purchase_fixture.gift_id} guest_id={gift_purchase_fixture.guest_id}>"
assert repr_output == f"<GiftPurchase gift_id={gift_purchase_fixture.gift_id} guest_id={gift_purchase_fixture.guest_id}>"