test(loyalty): add integration and unit tests for analytics, pages, and stats
Some checks failed
Some checks failed
- Add merchant stats API tests (GET /merchants/loyalty/stats) with 7 test cases - Add merchant page route tests (program, program-edit, analytics) with 6 test cases - Add store page route tests (terminal, cards, card-detail, program, program-edit, analytics, enroll) with 16 test cases - Add unit tests for get_merchant_stats() enhanced fields (new_this_month, estimated_liability_cents, location breakdown) with 6 test cases - Add unit tests for get_platform_stats() enhanced fields (total_points_issued/redeemed, total_points_balance, new_this_month, estimated_liability_cents) with 4 test cases - Total: 38 new tests (174 -> 212 passing) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -447,3 +447,323 @@ class TestGetProgramStats:
|
||||
assert stats["active_cards"] == 3
|
||||
assert stats["total_points_balance"] == 600 # 100+200+300
|
||||
assert stats["avg_points_per_member"] == 200.0 # 600/3
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Merchant Stats
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.loyalty
|
||||
class TestGetMerchantStats:
|
||||
"""Tests for get_merchant_stats."""
|
||||
|
||||
def setup_method(self):
|
||||
self.service = ProgramService()
|
||||
|
||||
def test_merchant_stats_returns_all_fields(self, db, ps_program, ps_merchant):
|
||||
"""Merchant stats include all required fields."""
|
||||
stats = self.service.get_merchant_stats(db, ps_merchant.id)
|
||||
|
||||
expected_fields = [
|
||||
"merchant_id",
|
||||
"program_id",
|
||||
"total_cards",
|
||||
"active_cards",
|
||||
"new_this_month",
|
||||
"total_points_issued",
|
||||
"total_points_redeemed",
|
||||
"points_issued_30d",
|
||||
"points_redeemed_30d",
|
||||
"transactions_30d",
|
||||
"estimated_liability_cents",
|
||||
"program",
|
||||
"locations",
|
||||
]
|
||||
for field in expected_fields:
|
||||
assert field in stats, f"Missing field: {field}"
|
||||
|
||||
def test_merchant_stats_empty_program(self, db, ps_program, ps_merchant):
|
||||
"""Merchant stats for program with no cards."""
|
||||
stats = self.service.get_merchant_stats(db, ps_merchant.id)
|
||||
|
||||
assert stats["merchant_id"] == ps_merchant.id
|
||||
assert stats["program_id"] == ps_program.id
|
||||
assert stats["total_cards"] == 0
|
||||
assert stats["active_cards"] == 0
|
||||
assert stats["new_this_month"] == 0
|
||||
assert stats["estimated_liability_cents"] == 0
|
||||
|
||||
def test_merchant_stats_no_program(self, db):
|
||||
"""Returns empty stats when merchant has no program."""
|
||||
from middleware.auth import AuthManager
|
||||
|
||||
auth = AuthManager()
|
||||
uid = uuid.uuid4().hex[:8]
|
||||
|
||||
owner = User(
|
||||
email=f"noprg_{uid}@test.com",
|
||||
username=f"noprg_{uid}",
|
||||
hashed_password=auth.hash_password("testpass123"),
|
||||
role="merchant_owner",
|
||||
is_active=True,
|
||||
is_email_verified=True,
|
||||
)
|
||||
db.add(owner)
|
||||
db.commit()
|
||||
db.refresh(owner)
|
||||
|
||||
merchant = Merchant(
|
||||
name=f"No Program Merchant {uid}",
|
||||
owner_user_id=owner.id,
|
||||
contact_email=owner.email,
|
||||
is_active=True,
|
||||
is_verified=True,
|
||||
)
|
||||
db.add(merchant)
|
||||
db.commit()
|
||||
db.refresh(merchant)
|
||||
|
||||
stats = self.service.get_merchant_stats(db, merchant.id)
|
||||
assert stats["program_id"] is None
|
||||
assert stats["total_cards"] == 0
|
||||
assert stats["program"] is None
|
||||
assert stats["locations"] == []
|
||||
|
||||
def test_merchant_stats_includes_program_info(self, db, ps_program, ps_merchant):
|
||||
"""Merchant stats include program configuration."""
|
||||
stats = self.service.get_merchant_stats(db, ps_merchant.id)
|
||||
|
||||
assert stats["program"] is not None
|
||||
assert stats["program"]["id"] == ps_program.id
|
||||
assert stats["program"]["loyalty_type"] == "points"
|
||||
assert stats["program"]["points_per_euro"] == 10
|
||||
|
||||
def test_merchant_stats_with_cards(self, db, ps_program, ps_merchant):
|
||||
"""Merchant stats reflect card data including new fields."""
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from app.modules.customers.models.customer import Customer
|
||||
from app.modules.loyalty.models import LoyaltyCard
|
||||
from app.modules.tenancy.models import Store
|
||||
|
||||
uid_store = uuid.uuid4().hex[:8]
|
||||
store = Store(
|
||||
merchant_id=ps_merchant.id,
|
||||
store_code=f"MSTAT_{uid_store.upper()}",
|
||||
subdomain=f"mstat{uid_store}",
|
||||
name=f"Merchant Stats Store {uid_store}",
|
||||
is_active=True,
|
||||
is_verified=True,
|
||||
)
|
||||
db.add(store)
|
||||
db.flush()
|
||||
|
||||
for i in range(2):
|
||||
uid = uuid.uuid4().hex[:8]
|
||||
customer = Customer(
|
||||
email=f"mstat_{uid}@test.com",
|
||||
first_name="MS",
|
||||
last_name=f"Customer{i}",
|
||||
hashed_password="!unused!", # noqa: SEC001
|
||||
customer_number=f"MS-{uid.upper()}",
|
||||
store_id=store.id,
|
||||
is_active=True,
|
||||
)
|
||||
db.add(customer)
|
||||
db.flush()
|
||||
|
||||
card = LoyaltyCard(
|
||||
merchant_id=ps_merchant.id,
|
||||
program_id=ps_program.id,
|
||||
customer_id=customer.id,
|
||||
enrolled_at_store_id=store.id,
|
||||
card_number=f"MSTAT-{i}-{uuid.uuid4().hex[:6]}",
|
||||
points_balance=200,
|
||||
total_points_earned=200,
|
||||
is_active=True,
|
||||
last_activity_at=datetime.now(UTC),
|
||||
)
|
||||
db.add(card)
|
||||
db.commit()
|
||||
|
||||
stats = self.service.get_merchant_stats(db, ps_merchant.id)
|
||||
|
||||
assert stats["total_cards"] == 2
|
||||
assert stats["active_cards"] == 2
|
||||
# new_this_month should include cards created today
|
||||
assert stats["new_this_month"] == 2
|
||||
# estimated_liability_cents: 2 cards * 200 points each = 400 points
|
||||
# 400 // 100 * 100 = 400 cents
|
||||
assert stats["estimated_liability_cents"] == 400
|
||||
|
||||
def test_merchant_stats_location_breakdown(self, db, ps_program, ps_merchant):
|
||||
"""Location breakdown includes per-store data."""
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from app.modules.customers.models.customer import Customer
|
||||
from app.modules.loyalty.models import LoyaltyCard
|
||||
from app.modules.tenancy.models import Store
|
||||
|
||||
uid = uuid.uuid4().hex[:8]
|
||||
store = Store(
|
||||
merchant_id=ps_merchant.id,
|
||||
store_code=f"MLOC_{uid.upper()}",
|
||||
subdomain=f"mloc{uid}",
|
||||
name=f"Location Store {uid}",
|
||||
is_active=True,
|
||||
is_verified=True,
|
||||
)
|
||||
db.add(store)
|
||||
db.flush()
|
||||
|
||||
customer = Customer(
|
||||
email=f"mloc_{uid}@test.com",
|
||||
first_name="Loc",
|
||||
last_name="Customer",
|
||||
hashed_password="!unused!", # noqa: SEC001
|
||||
customer_number=f"LC-{uid.upper()}",
|
||||
store_id=store.id,
|
||||
is_active=True,
|
||||
)
|
||||
db.add(customer)
|
||||
db.flush()
|
||||
|
||||
card = LoyaltyCard(
|
||||
merchant_id=ps_merchant.id,
|
||||
program_id=ps_program.id,
|
||||
customer_id=customer.id,
|
||||
enrolled_at_store_id=store.id,
|
||||
card_number=f"MLOC-{uuid.uuid4().hex[:6]}",
|
||||
points_balance=50,
|
||||
total_points_earned=50,
|
||||
is_active=True,
|
||||
last_activity_at=datetime.now(UTC),
|
||||
)
|
||||
db.add(card)
|
||||
db.commit()
|
||||
|
||||
stats = self.service.get_merchant_stats(db, ps_merchant.id)
|
||||
|
||||
assert len(stats["locations"]) >= 1
|
||||
# Find our store in locations
|
||||
our_loc = next(
|
||||
(loc for loc in stats["locations"] if loc["store_id"] == store.id),
|
||||
None,
|
||||
)
|
||||
assert our_loc is not None
|
||||
assert our_loc["store_name"] == store.name
|
||||
assert our_loc["enrolled_count"] == 1
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Platform Stats
|
||||
# ============================================================================
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.loyalty
|
||||
class TestGetPlatformStats:
|
||||
"""Tests for get_platform_stats."""
|
||||
|
||||
def setup_method(self):
|
||||
self.service = ProgramService()
|
||||
|
||||
def test_platform_stats_returns_all_fields(self, db):
|
||||
"""Platform stats include all required fields."""
|
||||
stats = self.service.get_platform_stats(db)
|
||||
|
||||
expected_fields = [
|
||||
"total_programs",
|
||||
"active_programs",
|
||||
"merchants_with_programs",
|
||||
"total_cards",
|
||||
"active_cards",
|
||||
"transactions_30d",
|
||||
"points_issued_30d",
|
||||
"points_redeemed_30d",
|
||||
"total_points_issued",
|
||||
"total_points_redeemed",
|
||||
"total_points_balance",
|
||||
"new_this_month",
|
||||
"estimated_liability_cents",
|
||||
]
|
||||
for field in expected_fields:
|
||||
assert field in stats, f"Missing field: {field}"
|
||||
|
||||
def test_platform_stats_counts_programs(self, db, ps_program):
|
||||
"""Platform stats count programs correctly."""
|
||||
stats = self.service.get_platform_stats(db)
|
||||
|
||||
assert stats["total_programs"] >= 1
|
||||
assert stats["active_programs"] >= 1
|
||||
assert stats["merchants_with_programs"] >= 1
|
||||
|
||||
def test_platform_stats_with_cards(self, db, ps_program, ps_merchant):
|
||||
"""Platform stats reflect card data across all programs."""
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from app.modules.customers.models.customer import Customer
|
||||
from app.modules.loyalty.models import LoyaltyCard
|
||||
from app.modules.tenancy.models import Store
|
||||
|
||||
uid = uuid.uuid4().hex[:8]
|
||||
store = Store(
|
||||
merchant_id=ps_merchant.id,
|
||||
store_code=f"PLAT_{uid.upper()}",
|
||||
subdomain=f"plat{uid}",
|
||||
name=f"Platform Stats Store {uid}",
|
||||
is_active=True,
|
||||
is_verified=True,
|
||||
)
|
||||
db.add(store)
|
||||
db.flush()
|
||||
|
||||
customer = Customer(
|
||||
email=f"plat_{uid}@test.com",
|
||||
first_name="Plat",
|
||||
last_name="Customer",
|
||||
hashed_password="!unused!", # noqa: SEC001
|
||||
customer_number=f"PC-{uid.upper()}",
|
||||
store_id=store.id,
|
||||
is_active=True,
|
||||
)
|
||||
db.add(customer)
|
||||
db.flush()
|
||||
|
||||
card = LoyaltyCard(
|
||||
merchant_id=ps_merchant.id,
|
||||
program_id=ps_program.id,
|
||||
customer_id=customer.id,
|
||||
card_number=f"PLAT-{uuid.uuid4().hex[:6]}",
|
||||
points_balance=300,
|
||||
total_points_earned=300,
|
||||
is_active=True,
|
||||
last_activity_at=datetime.now(UTC),
|
||||
)
|
||||
db.add(card)
|
||||
db.commit()
|
||||
|
||||
stats = self.service.get_platform_stats(db)
|
||||
|
||||
assert stats["total_cards"] >= 1
|
||||
assert stats["active_cards"] >= 1
|
||||
assert stats["total_points_balance"] >= 300
|
||||
assert stats["new_this_month"] >= 1
|
||||
assert stats["estimated_liability_cents"] >= 0
|
||||
|
||||
def test_platform_stats_zero_when_empty(self, db):
|
||||
"""All numeric fields are zero or positive."""
|
||||
stats = self.service.get_platform_stats(db)
|
||||
|
||||
assert stats["total_cards"] >= 0
|
||||
assert stats["active_cards"] >= 0
|
||||
assert stats["transactions_30d"] >= 0
|
||||
assert stats["points_issued_30d"] >= 0
|
||||
assert stats["points_redeemed_30d"] >= 0
|
||||
assert stats["total_points_issued"] >= 0
|
||||
assert stats["total_points_redeemed"] >= 0
|
||||
assert stats["total_points_balance"] >= 0
|
||||
assert stats["new_this_month"] >= 0
|
||||
assert stats["estimated_liability_cents"] >= 0
|
||||
|
||||
Reference in New Issue
Block a user