Add unit tests for GiftItem, GiftCategory, and GiftPurchase models
Introduced comprehensive test coverage for GiftItem, GiftCategory, and GiftPurchase models to validate core functionality such as creation, updates, deletions, and derived properties. Also updated the `total_gifts` method in `GiftCategory` to calculate the total requested quantity rather than the count of gifts.
This commit is contained in:
@@ -158,8 +158,9 @@ class GiftCategory(Base, UUIDMixin, TimestampMixin):
|
||||
|
||||
@property
|
||||
def total_gifts(self) -> int:
|
||||
"""Get total number of gifts in category"""
|
||||
return len(self.gifts)
|
||||
"""Get the total quantity of gifts requested in the category."""
|
||||
return sum(gift.quantity_requested for gift in self.gifts)
|
||||
|
||||
|
||||
@property
|
||||
def available_gifts(self) -> int:
|
||||
|
||||
198
backend/tests/models/test_gift.py
Normal file
198
backend/tests/models/test_gift.py
Normal file
@@ -0,0 +1,198 @@
|
||||
# tests/models/test_gift.py
|
||||
import uuid
|
||||
from app.models.gift import GiftItem, GiftStatus, GiftPriority, GiftCategory, GiftPurchase
|
||||
|
||||
|
||||
# ================================
|
||||
# GiftItem Test Cases
|
||||
# ================================
|
||||
|
||||
def test_create_gift_item(db_session, mock_user):
|
||||
# Arrange
|
||||
gift = GiftItem(
|
||||
id=uuid.uuid4(),
|
||||
event_id=uuid.uuid4(),
|
||||
added_by=mock_user.id,
|
||||
name="Baby Stroller",
|
||||
description="High-quality baby stroller.",
|
||||
price=150.00,
|
||||
currency="USD",
|
||||
quantity_requested=2,
|
||||
quantity_received=0,
|
||||
status=GiftStatus.AVAILABLE,
|
||||
priority=GiftPriority.MUST_HAVE,
|
||||
is_visible=True,
|
||||
)
|
||||
db_session.add(gift)
|
||||
|
||||
# Act
|
||||
db_session.commit()
|
||||
created_gift = db_session.query(GiftItem).filter_by(name="Baby Stroller").first()
|
||||
|
||||
# Assert
|
||||
assert created_gift is not None
|
||||
assert created_gift.name == "Baby Stroller"
|
||||
assert created_gift.status == GiftStatus.AVAILABLE
|
||||
assert created_gift.priority == GiftPriority.MUST_HAVE
|
||||
assert created_gift.price == 150.00
|
||||
assert created_gift.currency == "USD"
|
||||
|
||||
|
||||
def test_update_gift_item(gift_item_fixture, db_session):
|
||||
# Act
|
||||
gift_item_fixture.quantity_requested = 3
|
||||
gift_item_fixture.status = GiftStatus.RESERVED
|
||||
gift_item_fixture.description = "Updated description for the gift."
|
||||
db_session.commit()
|
||||
|
||||
# Assert
|
||||
assert gift_item_fixture.quantity_requested == 3
|
||||
assert gift_item_fixture.status == GiftStatus.RESERVED
|
||||
assert gift_item_fixture.description == "Updated description for the gift."
|
||||
|
||||
|
||||
def test_delete_gift_item(gift_item_fixture, db_session):
|
||||
# Act
|
||||
db_session.delete(gift_item_fixture)
|
||||
db_session.commit()
|
||||
|
||||
# Assert
|
||||
deleted_gift = db_session.query(GiftItem).filter_by(id=gift_item_fixture.id).first()
|
||||
assert deleted_gift is None
|
||||
|
||||
|
||||
def test_gift_item_remaining_quantity(gift_item_fixture):
|
||||
# Arrange
|
||||
gift_item_fixture.quantity_requested = 5
|
||||
gift_item_fixture.quantity_received = 2
|
||||
|
||||
# Act
|
||||
remaining_quantity = gift_item_fixture.remaining_quantity
|
||||
|
||||
# Assert
|
||||
assert remaining_quantity == 3 # 5 requested - 2 received
|
||||
|
||||
|
||||
def test_gift_item_is_fully_received(gift_item_fixture):
|
||||
# Arrange
|
||||
gift_item_fixture.quantity_requested = 4
|
||||
gift_item_fixture.quantity_received = 4
|
||||
|
||||
# Act
|
||||
is_fully_received = gift_item_fixture.is_fully_received
|
||||
|
||||
# Assert
|
||||
assert is_fully_received is True
|
||||
|
||||
|
||||
# ================================
|
||||
# GiftCategory Test Cases
|
||||
# ================================
|
||||
|
||||
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."
|
||||
)
|
||||
db_session.add(category)
|
||||
|
||||
# Act
|
||||
db_session.commit()
|
||||
created_category = db_session.query(GiftCategory).filter_by(name="Toys").first()
|
||||
|
||||
# Assert
|
||||
assert created_category is not None
|
||||
assert created_category.description == "All kinds of educational toys."
|
||||
|
||||
|
||||
def test_gift_category_total_gifts(db_session, mock_user):
|
||||
# Arrange
|
||||
category = GiftCategory(
|
||||
id=uuid.uuid4(),
|
||||
event_id=uuid.uuid4(),
|
||||
created_by=mock_user.id,
|
||||
name="Baby Essentials",
|
||||
)
|
||||
db_session.add(category)
|
||||
db_session.commit()
|
||||
|
||||
gift1 = GiftItem(
|
||||
id=uuid.uuid4(),
|
||||
event_id=category.event_id,
|
||||
category_id=category.id,
|
||||
name="Bottle Set",
|
||||
quantity_requested=2,
|
||||
added_by=mock_user.id,
|
||||
status=GiftStatus.AVAILABLE,
|
||||
)
|
||||
gift2 = GiftItem(
|
||||
id=uuid.uuid4(),
|
||||
event_id=category.event_id,
|
||||
category_id=category.id,
|
||||
name="Diaper Bag",
|
||||
quantity_requested=1,
|
||||
added_by=mock_user.id,
|
||||
status=GiftStatus.AVAILABLE,
|
||||
)
|
||||
db_session.add_all([gift1, gift2])
|
||||
db_session.commit()
|
||||
|
||||
# Act
|
||||
total_gifts = category.total_gifts
|
||||
|
||||
# Assert
|
||||
assert total_gifts == 3 # 2 from gift1 + 1 from gift2
|
||||
|
||||
|
||||
# ================================
|
||||
# GiftPurchase Test Cases
|
||||
# ================================
|
||||
|
||||
def test_create_gift_purchase(db_session, mock_user, gift_item_fixture):
|
||||
# Arrange
|
||||
purchase = GiftPurchase(
|
||||
id=uuid.uuid4(),
|
||||
gift_id=gift_item_fixture.id,
|
||||
guest_id=mock_user.id,
|
||||
quantity=2,
|
||||
purchase_price=50.00,
|
||||
purchase_currency="USD",
|
||||
notes="Gift purchased for the event."
|
||||
)
|
||||
db_session.add(purchase)
|
||||
|
||||
# Act
|
||||
db_session.commit()
|
||||
created_purchase = db_session.query(GiftPurchase).filter_by(gift_id=gift_item_fixture.id).first()
|
||||
|
||||
# Assert
|
||||
assert created_purchase is not None
|
||||
assert created_purchase.quantity == 2
|
||||
assert created_purchase.purchase_currency == "USD"
|
||||
assert created_purchase.notes == "Gift purchased for the event."
|
||||
|
||||
|
||||
def test_link_purchase_to_gift(db_session, gift_item_fixture):
|
||||
# Arrange
|
||||
purchase = GiftPurchase(
|
||||
id=uuid.uuid4(),
|
||||
gift_id=gift_item_fixture.id,
|
||||
guest_id=uuid.uuid4(),
|
||||
quantity=1,
|
||||
purchase_price=25.00,
|
||||
purchase_currency="USD"
|
||||
)
|
||||
db_session.add(purchase)
|
||||
db_session.commit()
|
||||
|
||||
# Act
|
||||
gift_purchases = gift_item_fixture.purchase_history
|
||||
|
||||
# Assert
|
||||
assert len(gift_purchases) == 1
|
||||
assert gift_purchases[0].quantity == 1
|
||||
assert gift_purchases[0].purchase_price == 25.00
|
||||
Reference in New Issue
Block a user