test_settlement_tech_attribution.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # tests/test_settlement_tech_attribution.py
  2. import pytest
  3. from datetime import datetime
  4. from sqlalchemy import select, update
  5. from app.core.database import AsyncSessionLocal
  6. from app.services.settlement_service import SettlementService
  7. from app.schemas.settlement import SettlementPreviewRequest
  8. from app.models.product import VasProduct
  9. from app.models.user import VasUser
  10. from app.core.biz_exception import BizLogicError
  11. @pytest.mark.anyio
  12. async def test_settlement_tech_attribution_and_strict_unbound_guard():
  13. async with AsyncSessionLocal() as db:
  14. # 1. 正常分润试算(明确指定合伙人真实用户ID,所有商品均已绑定插件)
  15. res = await SettlementService.preview_settlement(
  16. db,
  17. SettlementPreviewRequest(
  18. start_date=datetime(2026, 9, 1),
  19. end_date=datetime(2026, 10, 31),
  20. partner_a_id="usr-c1f46c54",
  21. partner_b_id="usr-df718001",
  22. )
  23. )
  24. assert res.total_orders > 0
  25. # 验证所有分润归属人必须为系统真实存在的自然人(VasUser),绝不能是 vfs_dev 等虚拟标识
  26. all_users = (await db.execute(select(VasUser))).scalars().all()
  27. valid_user_ids = {u.id for u in all_users}
  28. for payout in res.payout_summary:
  29. assert payout.user_id in valid_user_ids, f"Invalid user_id in payout: {payout.user_id}"
  30. assert not payout.user_id.endswith("_dev"), f"Virtual dev string found: {payout.user_id}"
  31. assert payout.user_name is not None
  32. assert len(payout.user_name) > 0
  33. # 验证订单明细中的开发者与对接人均有真实姓名
  34. for d in res.details:
  35. if d.developer_id:
  36. assert d.developer_id in valid_user_ids
  37. assert not d.developer_id.endswith("_dev")
  38. assert d.developer_name is not None
  39. # 2. 严格模式测试:商品未绑定插件直接报错,不进行任何模糊推导
  40. await db.execute(
  41. update(VasProduct).where(VasProduct.id == 36).values(plugin_id=None, plugin_name=None, developer_id=None)
  42. )
  43. await db.commit()
  44. try:
  45. with pytest.raises(BizLogicError) as exc_info:
  46. await SettlementService.preview_settlement(
  47. db,
  48. SettlementPreviewRequest(
  49. start_date=datetime(2026, 9, 1),
  50. end_date=datetime(2026, 10, 31),
  51. partner_a_id="usr-c1f46c54",
  52. partner_b_id="usr-df718001",
  53. )
  54. )
  55. assert "尚未绑定履约处理插件" in str(exc_info.value)
  56. finally:
  57. # 恢复商品 36 的插件绑定
  58. await db.execute(
  59. update(VasProduct).where(VasProduct.id == 36).values(plugin_id=3, plugin_name="vfs_plugin", developer_id="usr-c1f46c54")
  60. )
  61. await db.commit()
  62. # 3. 严格模式测试:合伙人未指定直接报错,无任何默认预设
  63. with pytest.raises(BizLogicError) as p_exc:
  64. await SettlementService.preview_settlement(
  65. db,
  66. SettlementPreviewRequest(
  67. start_date=datetime(2026, 9, 1),
  68. end_date=datetime(2026, 10, 31),
  69. partner_a_id="",
  70. partner_b_id="usr-df718001",
  71. )
  72. )
  73. assert "请提供合伙人甲方的系统用户ID" in str(p_exc.value)