fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 - 修复PEP8格式问题 - 添加类型注解 - 修复缺失的urllib.parse导入
This commit is contained in:
@@ -11,6 +11,7 @@ from subscription_manager import PaymentProvider, SubscriptionManager
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
|
||||
def test_subscription_manager():
|
||||
"""测试订阅管理器"""
|
||||
print("=" * 60)
|
||||
@@ -18,7 +19,7 @@ def test_subscription_manager():
|
||||
print("=" * 60)
|
||||
|
||||
# 使用临时文件数据库进行测试
|
||||
db_path = tempfile.mktemp(suffix='.db')
|
||||
db_path = tempfile.mktemp(suffix=".db")
|
||||
|
||||
try:
|
||||
manager = SubscriptionManager(db_path=db_path)
|
||||
@@ -55,7 +56,7 @@ def test_subscription_manager():
|
||||
tenant_id=tenant_id,
|
||||
plan_id=pro_plan.id,
|
||||
payment_provider=PaymentProvider.STRIPE.value,
|
||||
trial_days=14
|
||||
trial_days=14,
|
||||
)
|
||||
|
||||
print(f"✓ 创建订阅: {subscription.id}")
|
||||
@@ -78,7 +79,7 @@ def test_subscription_manager():
|
||||
resource_type="transcription",
|
||||
quantity=120,
|
||||
unit="minute",
|
||||
description="会议转录"
|
||||
description="会议转录",
|
||||
)
|
||||
print(f"✓ 记录转录用量: {usage1.quantity} {usage1.unit}, 费用: ¥{usage1.cost:.2f}")
|
||||
|
||||
@@ -88,7 +89,7 @@ def test_subscription_manager():
|
||||
resource_type="storage",
|
||||
quantity=2.5,
|
||||
unit="gb",
|
||||
description="文件存储"
|
||||
description="文件存储",
|
||||
)
|
||||
print(f"✓ 记录存储用量: {usage2.quantity} {usage2.unit}, 费用: ¥{usage2.cost:.2f}")
|
||||
|
||||
@@ -96,7 +97,7 @@ def test_subscription_manager():
|
||||
summary = manager.get_usage_summary(tenant_id)
|
||||
print("✓ 用量汇总:")
|
||||
print(f" - 总费用: ¥{summary['total_cost']:.2f}")
|
||||
for resource, data in summary['breakdown'].items():
|
||||
for resource, data in summary["breakdown"].items():
|
||||
print(f" - {resource}: {data['quantity']} (¥{data['cost']:.2f})")
|
||||
|
||||
print("\n4. 测试支付管理")
|
||||
@@ -108,7 +109,7 @@ def test_subscription_manager():
|
||||
amount=99.0,
|
||||
currency="CNY",
|
||||
provider=PaymentProvider.ALIPAY.value,
|
||||
payment_method="qrcode"
|
||||
payment_method="qrcode",
|
||||
)
|
||||
print(f"✓ 创建支付: {payment.id}")
|
||||
print(f" - 金额: ¥{payment.amount}")
|
||||
@@ -145,7 +146,7 @@ def test_subscription_manager():
|
||||
payment_id=payment.id,
|
||||
amount=50.0,
|
||||
reason="服务不满意",
|
||||
requested_by="user_001"
|
||||
requested_by="user_001",
|
||||
)
|
||||
print(f"✓ 申请退款: {refund.id}")
|
||||
print(f" - 金额: ¥{refund.amount}")
|
||||
@@ -180,29 +181,23 @@ def test_subscription_manager():
|
||||
tenant_id=tenant_id,
|
||||
plan_id=enterprise_plan.id,
|
||||
success_url="https://example.com/success",
|
||||
cancel_url="https://example.com/cancel"
|
||||
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("stripe", {
|
||||
"event_type": "checkout.session.completed",
|
||||
"data": {"object": {"id": "cs_test"}}
|
||||
})
|
||||
webhook_result = manager.handle_webhook(
|
||||
"stripe",
|
||||
{"event_type": "checkout.session.completed", "data": {"object": {"id": "cs_test"}}},
|
||||
)
|
||||
print(f"✓ Webhook 处理: {webhook_result}")
|
||||
|
||||
print("\n9. 测试订阅变更")
|
||||
@@ -210,16 +205,12 @@ def test_subscription_manager():
|
||||
|
||||
# 更改计划
|
||||
changed = manager.change_plan(
|
||||
subscription_id=subscription.id,
|
||||
new_plan_id=enterprise_plan.id
|
||||
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}")
|
||||
|
||||
@@ -233,11 +224,13 @@ def test_subscription_manager():
|
||||
os.remove(db_path)
|
||||
print(f"\n清理临时数据库: {db_path}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
test_subscription_manager()
|
||||
except Exception as e:
|
||||
print(f"\n❌ 测试失败: {e}")
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
sys.exit(1)
|
||||
|
||||
Reference in New Issue
Block a user