insightfy-bloom-ms-ems / test_gift_card_implementation.py
MukeshKapoor25's picture
feat: Implement Gift Card Template API with CRUD operations
20ffa57
#!/usr/bin/env python3
"""
Test script for Gift Card Template API endpoints.
This script demonstrates the usage of the gift card template endpoints.
"""
import asyncio
import sys
import os
# Add the app directory to Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from app.schemas.gift_card_schema import (
GiftCardTemplateCreate,
GiftCardTemplateUpdate,
DesignTemplate
)
def test_gift_card_schemas():
"""Test gift card schema validation."""
print("Testing Gift Card Template Schemas...")
# Test creating a digital gift card template
try:
digital_template = GiftCardTemplateCreate(
name="Digital Birthday Card",
description="Special digital card for birthdays",
delivery_type="digital",
currency="INR",
validity_days=365,
allow_custom_amount=True,
predefined_amounts=[500, 1000, 2000],
reloadable=False,
allow_partial_redemption=True,
requires_pin=True,
requires_otp=False,
status="active",
design_template=DesignTemplate(
theme="blue",
image_url="https://example.com/birthday.png"
),
created_by="admin_123"
)
print("βœ“ Digital template schema validation passed")
print(f" Template name: {digital_template.name}")
print(f" Delivery type: {digital_template.delivery_type}")
print(f" Currency: {digital_template.currency}")
except Exception as e:
print(f"βœ— Digital template schema validation failed: {e}")
return False
# Test creating a physical gift card template
try:
physical_template = GiftCardTemplateCreate(
name="Physical Premium β‚Ή1000",
description="Premium physical plastic card with fixed denomination",
delivery_type="physical",
currency="INR",
validity_days=730,
allow_custom_amount=False,
predefined_amounts=[1000],
reloadable=False,
allow_partial_redemption=True,
requires_pin=True,
requires_otp=False,
status="active",
max_issues=500,
design_template=DesignTemplate(
theme="gold",
image_url="https://example.com/physical.png"
),
branch_ids=["branch_1", "branch_2"],
campaign_id="campaign_holiday_2025",
metadata={"batch": "2025_holiday"},
created_by="marketing_mgr"
)
print("βœ“ Physical template schema validation passed")
print(f" Template name: {physical_template.name}")
print(f" Max issues: {physical_template.max_issues}")
print(f" Branch IDs: {physical_template.branch_ids}")
except Exception as e:
print(f"βœ— Physical template schema validation failed: {e}")
return False
# Test update schema
try:
update_template = GiftCardTemplateUpdate(
status="inactive",
predefined_amounts=[1000, 2000]
)
print("βœ“ Update template schema validation passed")
print(f" Status update: {update_template.status}")
print(f" Amounts update: {update_template.predefined_amounts}")
except Exception as e:
print(f"βœ— Update template schema validation failed: {e}")
return False
# Test validation errors
try:
# This should fail - physical card without max_issues
GiftCardTemplateCreate(
name="Invalid Physical Card",
delivery_type="physical",
currency="INR",
allow_custom_amount=True,
reloadable=False,
allow_partial_redemption=True,
requires_pin=True,
requires_otp=False,
status="active",
created_by="admin_123"
)
print("βœ— Validation error test failed - should have caught missing max_issues")
return False
except ValueError as e:
print("βœ“ Validation error correctly caught:", str(e))
try:
# This should fail - no amounts configuration
GiftCardTemplateCreate(
name="Invalid Amounts Card",
delivery_type="digital",
currency="INR",
allow_custom_amount=False,
reloadable=False,
allow_partial_redemption=True,
requires_pin=True,
requires_otp=False,
status="active",
created_by="admin_123"
)
print("βœ— Validation error test failed - should have caught missing amounts config")
return False
except ValueError as e:
print("βœ“ Validation error correctly caught:", str(e))
return True
def test_models_import():
"""Test that all models can be imported successfully."""
try:
from app.models.gift_card_models import GiftCardTemplateModel
from app.repositories.gift_card_repository import GiftCardRepository
from app.services.gift_card_service import GiftCardService
from app.routers.gift_card_router import router
print("βœ“ All modules imported successfully")
print(f" Model: {GiftCardTemplateModel.__name__}")
print(f" Repository: {GiftCardRepository.__name__}")
print(f" Service: {GiftCardService.__name__}")
print(f" Router: {router.__class__.__name__}")
return True
except ImportError as e:
print(f"βœ— Import error: {e}")
return False
def main():
"""Run all tests."""
print("🎯 Gift Card Template Implementation Test\n")
# Test imports
print("1. Testing module imports...")
import_success = test_models_import()
print()
# Test schemas
print("2. Testing schema validation...")
schema_success = test_gift_card_schemas()
print()
# Summary
print("πŸ“Š Test Summary:")
print(f" Module imports: {'βœ“ PASS' if import_success else 'βœ— FAIL'}")
print(f" Schema validation: {'βœ“ PASS' if schema_success else 'βœ— FAIL'}")
if import_success and schema_success:
print("\nπŸŽ‰ All tests passed! Gift Card Template implementation is working correctly.")
print("\nπŸ“š Available endpoints:")
print(" POST /api/v1/giftcard/ - Create gift card template")
print(" GET /api/v1/giftcard/ - List gift card templates")
print(" GET /api/v1/giftcard/{id} - Get specific template")
print(" PUT /api/v1/giftcard/{id} - Update template")
print(" DELETE /api/v1/giftcard/{id} - Delete template")
print(" GET /api/v1/giftcard/{id}/stock - Check stock availability")
print(" GET /api/v1/giftcard/active/list - Get active templates")
return True
else:
print("\n❌ Some tests failed. Please check the implementation.")
return False
if __name__ == "__main__":
main()