Additional fixes for gift models
This commit is contained in:
@@ -232,7 +232,7 @@ def read_gift_category(
|
|||||||
elif include_gifts:
|
elif include_gifts:
|
||||||
# If no event_id but include_gifts is true, just get all gifts for this category
|
# 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
|
# 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
|
gifts_list = gifts
|
||||||
|
|
||||||
# Create a new category response with the calculated values
|
# Create a new category response with the calculated values
|
||||||
|
|||||||
@@ -116,8 +116,6 @@ class GiftCategoryBase(BaseModel):
|
|||||||
description: Optional[str] = None
|
description: Optional[str] = None
|
||||||
icon: Optional[str] = None
|
icon: Optional[str] = None
|
||||||
color: Optional[str] = None
|
color: Optional[str] = None
|
||||||
display_order: Optional[int] = 0
|
|
||||||
is_visible: Optional[bool] = True
|
|
||||||
custom_fields: Optional[Dict[str, Any]] = None
|
custom_fields: Optional[Dict[str, Any]] = None
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -201,18 +201,32 @@ def test_create_gift_category(db_session, mock_event, mock_user):
|
|||||||
"name": "Toys",
|
"name": "Toys",
|
||||||
"description": "Fun toys for the birthday",
|
"description": "Fun toys for the birthday",
|
||||||
"icon": "toy-icon",
|
"icon": "toy-icon",
|
||||||
"color": "#FF5733",
|
"color": "#FF5733"
|
||||||
"display_order": 1
|
|
||||||
}
|
}
|
||||||
category_in = GiftCategoryCreate(
|
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)
|
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.name == category_data["name"]
|
||||||
assert category.description == category_data["description"]
|
assert category.description == category_data["description"]
|
||||||
assert category.event_id == mock_event.id
|
|
||||||
assert category.created_by == mock_user.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):
|
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):
|
def test_get_categories_by_event(db_session, mock_event, mock_user):
|
||||||
"""Test retrieving all gift categories for a specific event."""
|
"""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
|
# Create multiple categories for the same event
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
|
# Create the category
|
||||||
category_data = {
|
category_data = {
|
||||||
"name": f"Category {i}",
|
"name": f"Category {i}",
|
||||||
"description": f"Description for category {i}",
|
"description": f"Description for category {i}",
|
||||||
"display_order": i,
|
|
||||||
"event_id": mock_event.id,
|
|
||||||
"created_by": mock_user.id
|
"created_by": mock_user.id
|
||||||
}
|
}
|
||||||
category_in = GiftCategoryCreate(**category_data)
|
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
|
# 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,
|
db=db_session,
|
||||||
event_id=mock_event.id,
|
event_id=mock_event.id,
|
||||||
skip=0,
|
skip=0,
|
||||||
limit=100
|
limit=100
|
||||||
)
|
)
|
||||||
assert len(categories) >= 3
|
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
|
# Check ordering by display_order
|
||||||
for i in range(len(categories) - 1):
|
for i in range(len(associations) - 1):
|
||||||
assert categories[i].display_order <= categories[i + 1].display_order
|
assert associations[i].display_order <= associations[i + 1].display_order
|
||||||
|
|
||||||
|
|
||||||
def test_update_gift_category(db_session, gift_category_fixture):
|
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):
|
def test_delete_gift_category(db_session, gift_category_fixture):
|
||||||
"""Test deleting a gift category."""
|
"""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)
|
category = gift_category_crud.remove(db=db_session, id=gift_category_fixture.id)
|
||||||
assert category.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)
|
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):
|
def test_reorder_categories(db_session, mock_event, mock_user):
|
||||||
"""Test reordering gift categories."""
|
"""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
|
# Create categories with initial display order
|
||||||
categories = []
|
categories = []
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
|
# Create the category
|
||||||
category_data = {
|
category_data = {
|
||||||
"name": f"Category {i}",
|
"name": f"Category {i}",
|
||||||
"display_order": i,
|
|
||||||
"event_id": mock_event.id,
|
|
||||||
"created_by": mock_user.id
|
"created_by": mock_user.id
|
||||||
}
|
}
|
||||||
category_in = GiftCategoryCreate(**category_data)
|
category_in = GiftCategoryCreate(**category_data)
|
||||||
category = 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)
|
||||||
|
|
||||||
categories.append(category)
|
categories.append(category)
|
||||||
|
|
||||||
# Reorder categories (reverse order)
|
# Reorder categories (reverse order)
|
||||||
@@ -302,20 +364,20 @@ def test_reorder_categories(db_session, mock_event, mock_user):
|
|||||||
categories[2].id: 0
|
categories[2].id: 0
|
||||||
}
|
}
|
||||||
|
|
||||||
updated_categories = gift_category_crud.reorder_categories(
|
updated_associations = event_gift_category_crud.reorder_categories(
|
||||||
db=db_session,
|
db=db_session,
|
||||||
event_id=mock_event.id,
|
event_id=mock_event.id,
|
||||||
category_orders=category_orders
|
category_orders=category_orders
|
||||||
)
|
)
|
||||||
|
|
||||||
# Verify new order
|
# Verify new order
|
||||||
for category in updated_categories:
|
for assoc in updated_associations:
|
||||||
if category.id == categories[0].id:
|
if assoc.category_id == categories[0].id:
|
||||||
assert category.display_order == 2
|
assert assoc.display_order == 2
|
||||||
elif category.id == categories[1].id:
|
elif assoc.category_id == categories[1].id:
|
||||||
assert category.display_order == 1
|
assert assoc.display_order == 1
|
||||||
elif category.id == categories[2].id:
|
elif assoc.category_id == categories[2].id:
|
||||||
assert category.display_order == 0
|
assert assoc.display_order == 0
|
||||||
|
|
||||||
|
|
||||||
# Gift Purchase Tests
|
# Gift Purchase Tests
|
||||||
|
|||||||
@@ -135,7 +135,6 @@ def test_create_gift_category(db_session, mock_user):
|
|||||||
# Arrange
|
# Arrange
|
||||||
category = GiftCategory(
|
category = GiftCategory(
|
||||||
id=uuid.uuid4(),
|
id=uuid.uuid4(),
|
||||||
event_id=uuid.uuid4(),
|
|
||||||
created_by=mock_user.id,
|
created_by=mock_user.id,
|
||||||
name="Toys",
|
name="Toys",
|
||||||
description="All kinds of educational 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):
|
def test_gift_category_total_gifts(db_session, mock_user):
|
||||||
# Arrange
|
# Arrange
|
||||||
|
event_id = uuid.uuid4()
|
||||||
category = GiftCategory(
|
category = GiftCategory(
|
||||||
id=uuid.uuid4(),
|
id=uuid.uuid4(),
|
||||||
event_id=uuid.uuid4(),
|
|
||||||
created_by=mock_user.id,
|
created_by=mock_user.id,
|
||||||
name="Baby Essentials",
|
name="Baby Essentials",
|
||||||
)
|
)
|
||||||
db_session.add(category)
|
db_session.add(category)
|
||||||
db_session.commit()
|
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(
|
gift1 = GiftItem(
|
||||||
id=uuid.uuid4(),
|
id=uuid.uuid4(),
|
||||||
event_id=category.event_id,
|
event_id=event_id,
|
||||||
category_id=category.id,
|
category_id=category.id,
|
||||||
name="Bottle Set",
|
name="Bottle Set",
|
||||||
quantity_requested=2,
|
quantity_requested=2,
|
||||||
@@ -173,7 +183,7 @@ def test_gift_category_total_gifts(db_session, mock_user):
|
|||||||
)
|
)
|
||||||
gift2 = GiftItem(
|
gift2 = GiftItem(
|
||||||
id=uuid.uuid4(),
|
id=uuid.uuid4(),
|
||||||
event_id=category.event_id,
|
event_id=event_id,
|
||||||
category_id=category.id,
|
category_id=category.id,
|
||||||
name="Diaper Bag",
|
name="Diaper Bag",
|
||||||
quantity_requested=1,
|
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.
|
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
|
# Update gift_item_fixture to match the gift_category_fixture
|
||||||
gift_item = GiftItem(
|
gift_item = GiftItem(
|
||||||
name="Toy Car",
|
name="Toy Car",
|
||||||
@@ -211,7 +227,7 @@ def test_gift_category_available_gifts(db_session, gift_category_fixture, gift_i
|
|||||||
status=GiftStatus.AVAILABLE,
|
status=GiftStatus.AVAILABLE,
|
||||||
is_visible=True,
|
is_visible=True,
|
||||||
category_id=gift_category_fixture.id,
|
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
|
added_by=gift_category_fixture.created_by # Link to a user
|
||||||
)
|
)
|
||||||
db_session.add(gift_item)
|
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
|
Test that the reorder_gifts method correctly updates gift display_order
|
||||||
based on the provided order of gift IDs.
|
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)
|
# Create GiftItems with proper required fields and UUID objects (not strings)
|
||||||
gift1_id = uuid.uuid4()
|
gift1_id = uuid.uuid4()
|
||||||
gift2_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",
|
name="Gift 1",
|
||||||
display_order=0,
|
display_order=0,
|
||||||
category_id=gift_category_fixture.id,
|
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
|
added_by=gift_category_fixture.created_by, # Required field
|
||||||
status=GiftStatus.AVAILABLE # 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",
|
name="Gift 2",
|
||||||
display_order=1,
|
display_order=1,
|
||||||
category_id=gift_category_fixture.id,
|
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,
|
added_by=gift_category_fixture.created_by,
|
||||||
status=GiftStatus.AVAILABLE
|
status=GiftStatus.AVAILABLE
|
||||||
)
|
)
|
||||||
@@ -253,7 +275,7 @@ def test_gift_category_reorder_gifts(db_session, gift_category_fixture, mock_use
|
|||||||
name="Gift 3",
|
name="Gift 3",
|
||||||
display_order=2,
|
display_order=2,
|
||||||
category_id=gift_category_fixture.id,
|
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,
|
added_by=gift_category_fixture.created_by,
|
||||||
status=GiftStatus.AVAILABLE
|
status=GiftStatus.AVAILABLE
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user