fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 - 修复PEP8格式问题 - 修复语法错误(运算符空格问题) - 修复类型注解格式
This commit is contained in:
@@ -12,31 +12,31 @@ from subscription_manager import PaymentProvider, SubscriptionManager
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
|
||||
def test_subscription_manager():
|
||||
def test_subscription_manager() -> None:
|
||||
"""测试订阅管理器"""
|
||||
print("=" * 60)
|
||||
print(" = " * 60)
|
||||
print("InsightFlow Phase 8 Task 2 - 订阅与计费系统测试")
|
||||
print("=" * 60)
|
||||
print(" = " * 60)
|
||||
|
||||
# 使用临时文件数据库进行测试
|
||||
db_path = tempfile.mktemp(suffix=".db")
|
||||
db_path = tempfile.mktemp(suffix = ".db")
|
||||
|
||||
try:
|
||||
manager = SubscriptionManager(db_path=db_path)
|
||||
manager = SubscriptionManager(db_path = db_path)
|
||||
|
||||
print("\n1. 测试订阅计划管理")
|
||||
print("-" * 40)
|
||||
|
||||
# 获取默认计划
|
||||
plans = manager.list_plans()
|
||||
plans = manager.list_plans()
|
||||
print(f"✓ 默认计划数量: {len(plans)}")
|
||||
for plan in plans:
|
||||
print(f" - {plan.name} ({plan.tier}): ¥{plan.price_monthly}/月")
|
||||
|
||||
# 通过 tier 获取计划
|
||||
free_plan = manager.get_plan_by_tier("free")
|
||||
pro_plan = manager.get_plan_by_tier("pro")
|
||||
enterprise_plan = manager.get_plan_by_tier("enterprise")
|
||||
free_plan = manager.get_plan_by_tier("free")
|
||||
pro_plan = manager.get_plan_by_tier("pro")
|
||||
enterprise_plan = manager.get_plan_by_tier("enterprise")
|
||||
|
||||
assert free_plan is not None, "Free 计划应该存在"
|
||||
assert pro_plan is not None, "Pro 计划应该存在"
|
||||
@@ -49,14 +49,14 @@ def test_subscription_manager():
|
||||
print("\n2. 测试订阅管理")
|
||||
print("-" * 40)
|
||||
|
||||
tenant_id = "test-tenant-001"
|
||||
tenant_id = "test-tenant-001"
|
||||
|
||||
# 创建订阅
|
||||
subscription = manager.create_subscription(
|
||||
tenant_id=tenant_id,
|
||||
plan_id=pro_plan.id,
|
||||
payment_provider=PaymentProvider.STRIPE.value,
|
||||
trial_days=14,
|
||||
subscription = manager.create_subscription(
|
||||
tenant_id = tenant_id,
|
||||
plan_id = pro_plan.id,
|
||||
payment_provider = PaymentProvider.STRIPE.value,
|
||||
trial_days = 14,
|
||||
)
|
||||
|
||||
print(f"✓ 创建订阅: {subscription.id}")
|
||||
@@ -66,7 +66,7 @@ def test_subscription_manager():
|
||||
print(f" - 试用结束: {subscription.trial_end}")
|
||||
|
||||
# 获取租户订阅
|
||||
tenant_sub = manager.get_tenant_subscription(tenant_id)
|
||||
tenant_sub = manager.get_tenant_subscription(tenant_id)
|
||||
assert tenant_sub is not None, "应该能获取到租户订阅"
|
||||
print(f"✓ 获取租户订阅: {tenant_sub.id}")
|
||||
|
||||
@@ -74,27 +74,27 @@ def test_subscription_manager():
|
||||
print("-" * 40)
|
||||
|
||||
# 记录转录用量
|
||||
usage1 = manager.record_usage(
|
||||
tenant_id=tenant_id,
|
||||
resource_type="transcription",
|
||||
quantity=120,
|
||||
unit="minute",
|
||||
description="会议转录",
|
||||
usage1 = manager.record_usage(
|
||||
tenant_id = tenant_id,
|
||||
resource_type = "transcription",
|
||||
quantity = 120,
|
||||
unit = "minute",
|
||||
description = "会议转录",
|
||||
)
|
||||
print(f"✓ 记录转录用量: {usage1.quantity} {usage1.unit}, 费用: ¥{usage1.cost:.2f}")
|
||||
|
||||
# 记录存储用量
|
||||
usage2 = manager.record_usage(
|
||||
tenant_id=tenant_id,
|
||||
resource_type="storage",
|
||||
quantity=2.5,
|
||||
unit="gb",
|
||||
description="文件存储",
|
||||
usage2 = manager.record_usage(
|
||||
tenant_id = tenant_id,
|
||||
resource_type = "storage",
|
||||
quantity = 2.5,
|
||||
unit = "gb",
|
||||
description = "文件存储",
|
||||
)
|
||||
print(f"✓ 记录存储用量: {usage2.quantity} {usage2.unit}, 费用: ¥{usage2.cost:.2f}")
|
||||
|
||||
# 获取用量汇总
|
||||
summary = manager.get_usage_summary(tenant_id)
|
||||
summary = manager.get_usage_summary(tenant_id)
|
||||
print("✓ 用量汇总:")
|
||||
print(f" - 总费用: ¥{summary['total_cost']:.2f}")
|
||||
for resource, data in summary["breakdown"].items():
|
||||
@@ -104,12 +104,12 @@ def test_subscription_manager():
|
||||
print("-" * 40)
|
||||
|
||||
# 创建支付
|
||||
payment = manager.create_payment(
|
||||
tenant_id=tenant_id,
|
||||
amount=99.0,
|
||||
currency="CNY",
|
||||
provider=PaymentProvider.ALIPAY.value,
|
||||
payment_method="qrcode",
|
||||
payment = manager.create_payment(
|
||||
tenant_id = tenant_id,
|
||||
amount = 99.0,
|
||||
currency = "CNY",
|
||||
provider = PaymentProvider.ALIPAY.value,
|
||||
payment_method = "qrcode",
|
||||
)
|
||||
print(f"✓ 创建支付: {payment.id}")
|
||||
print(f" - 金额: ¥{payment.amount}")
|
||||
@@ -117,22 +117,22 @@ def test_subscription_manager():
|
||||
print(f" - 状态: {payment.status}")
|
||||
|
||||
# 确认支付
|
||||
confirmed = manager.confirm_payment(payment.id, "alipay_123456")
|
||||
confirmed = manager.confirm_payment(payment.id, "alipay_123456")
|
||||
print(f"✓ 确认支付完成: {confirmed.status}")
|
||||
|
||||
# 列出支付记录
|
||||
payments = manager.list_payments(tenant_id)
|
||||
payments = manager.list_payments(tenant_id)
|
||||
print(f"✓ 支付记录数量: {len(payments)}")
|
||||
|
||||
print("\n5. 测试发票管理")
|
||||
print("-" * 40)
|
||||
|
||||
# 列出发票
|
||||
invoices = manager.list_invoices(tenant_id)
|
||||
invoices = manager.list_invoices(tenant_id)
|
||||
print(f"✓ 发票数量: {len(invoices)}")
|
||||
|
||||
if invoices:
|
||||
invoice = invoices[0]
|
||||
invoice = invoices[0]
|
||||
print(f" - 发票号: {invoice.invoice_number}")
|
||||
print(f" - 金额: ¥{invoice.amount_due}")
|
||||
print(f" - 状态: {invoice.status}")
|
||||
@@ -141,12 +141,12 @@ def test_subscription_manager():
|
||||
print("-" * 40)
|
||||
|
||||
# 申请退款
|
||||
refund = manager.request_refund(
|
||||
tenant_id=tenant_id,
|
||||
payment_id=payment.id,
|
||||
amount=50.0,
|
||||
reason="服务不满意",
|
||||
requested_by="user_001",
|
||||
refund = manager.request_refund(
|
||||
tenant_id = tenant_id,
|
||||
payment_id = payment.id,
|
||||
amount = 50.0,
|
||||
reason = "服务不满意",
|
||||
requested_by = "user_001",
|
||||
)
|
||||
print(f"✓ 申请退款: {refund.id}")
|
||||
print(f" - 金额: ¥{refund.amount}")
|
||||
@@ -154,21 +154,21 @@ def test_subscription_manager():
|
||||
print(f" - 状态: {refund.status}")
|
||||
|
||||
# 批准退款
|
||||
approved = manager.approve_refund(refund.id, "admin_001")
|
||||
approved = manager.approve_refund(refund.id, "admin_001")
|
||||
print(f"✓ 批准退款: {approved.status}")
|
||||
|
||||
# 完成退款
|
||||
completed = manager.complete_refund(refund.id, "refund_123456")
|
||||
completed = manager.complete_refund(refund.id, "refund_123456")
|
||||
print(f"✓ 完成退款: {completed.status}")
|
||||
|
||||
# 列出退款记录
|
||||
refunds = manager.list_refunds(tenant_id)
|
||||
refunds = manager.list_refunds(tenant_id)
|
||||
print(f"✓ 退款记录数量: {len(refunds)}")
|
||||
|
||||
print("\n7. 测试账单历史")
|
||||
print("-" * 40)
|
||||
|
||||
history = manager.get_billing_history(tenant_id)
|
||||
history = manager.get_billing_history(tenant_id)
|
||||
print(f"✓ 账单历史记录数量: {len(history)}")
|
||||
for h in history:
|
||||
print(f" - [{h.type}] {h.description}: ¥{h.amount}")
|
||||
@@ -177,24 +177,24 @@ def test_subscription_manager():
|
||||
print("-" * 40)
|
||||
|
||||
# Stripe Checkout
|
||||
stripe_session = manager.create_stripe_checkout_session(
|
||||
tenant_id=tenant_id,
|
||||
plan_id=enterprise_plan.id,
|
||||
success_url="https://example.com/success",
|
||||
cancel_url="https://example.com/cancel",
|
||||
stripe_session = manager.create_stripe_checkout_session(
|
||||
tenant_id = tenant_id,
|
||||
plan_id = enterprise_plan.id,
|
||||
success_url = "https://example.com/success",
|
||||
cancel_url = "https://example.com/cancel",
|
||||
)
|
||||
print(f"✓ Stripe Checkout 会话: {stripe_session['session_id']}")
|
||||
|
||||
# 支付宝订单
|
||||
alipay_order = manager.create_alipay_order(tenant_id=tenant_id, plan_id=pro_plan.id)
|
||||
alipay_order = manager.create_alipay_order(tenant_id = tenant_id, plan_id = pro_plan.id)
|
||||
print(f"✓ 支付宝订单: {alipay_order['order_id']}")
|
||||
|
||||
# 微信支付订单
|
||||
wechat_order = manager.create_wechat_order(tenant_id=tenant_id, plan_id=pro_plan.id)
|
||||
wechat_order = manager.create_wechat_order(tenant_id = tenant_id, plan_id = pro_plan.id)
|
||||
print(f"✓ 微信支付订单: {wechat_order['order_id']}")
|
||||
|
||||
# Webhook 处理
|
||||
webhook_result = manager.handle_webhook(
|
||||
webhook_result = manager.handle_webhook(
|
||||
"stripe",
|
||||
{"event_type": "checkout.session.completed", "data": {"object": {"id": "cs_test"}}},
|
||||
)
|
||||
@@ -204,19 +204,19 @@ def test_subscription_manager():
|
||||
print("-" * 40)
|
||||
|
||||
# 更改计划
|
||||
changed = manager.change_plan(
|
||||
subscription_id=subscription.id, new_plan_id=enterprise_plan.id
|
||||
changed = manager.change_plan(
|
||||
subscription_id = subscription.id, new_plan_id = enterprise_plan.id
|
||||
)
|
||||
print(f"✓ 更改计划: {changed.plan_id} (Enterprise)")
|
||||
|
||||
# 取消订阅
|
||||
cancelled = manager.cancel_subscription(subscription_id=subscription.id, at_period_end=True)
|
||||
cancelled = manager.cancel_subscription(subscription_id = subscription.id, at_period_end = True)
|
||||
print(f"✓ 取消订阅: {cancelled.status}")
|
||||
print(f" - 周期结束时取消: {cancelled.cancel_at_period_end}")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("\n" + " = " * 60)
|
||||
print("所有测试通过! ✓")
|
||||
print("=" * 60)
|
||||
print(" = " * 60)
|
||||
|
||||
finally:
|
||||
# 清理临时数据库
|
||||
|
||||
Reference in New Issue
Block a user