| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- # tests/test_settlement_tech_attribution.py
- import pytest
- from datetime import datetime
- from sqlalchemy import select, update
- from app.core.database import AsyncSessionLocal
- from app.services.settlement_service import SettlementService
- from app.schemas.settlement import SettlementPreviewRequest
- from app.models.product import VasProduct
- from app.models.user import VasUser
- from app.core.biz_exception import BizLogicError
- @pytest.mark.anyio
- async def test_settlement_tech_attribution_and_strict_unbound_guard():
- async with AsyncSessionLocal() as db:
- # 1. 正常分润试算(明确指定合伙人真实用户ID,所有商品均已绑定插件)
- res = await SettlementService.preview_settlement(
- db,
- SettlementPreviewRequest(
- start_date=datetime(2026, 9, 1),
- end_date=datetime(2026, 10, 31),
- partner_a_id="usr-c1f46c54",
- partner_b_id="usr-df718001",
- )
- )
- assert res.total_orders > 0
- # 验证所有分润归属人必须为系统真实存在的自然人(VasUser),绝不能是 vfs_dev 等虚拟标识
- all_users = (await db.execute(select(VasUser))).scalars().all()
- valid_user_ids = {u.id for u in all_users}
- for payout in res.payout_summary:
- assert payout.user_id in valid_user_ids, f"Invalid user_id in payout: {payout.user_id}"
- assert not payout.user_id.endswith("_dev"), f"Virtual dev string found: {payout.user_id}"
- assert payout.user_name is not None
- assert len(payout.user_name) > 0
- # 验证订单明细中的开发者与对接人均有真实姓名
- for d in res.details:
- if d.developer_id:
- assert d.developer_id in valid_user_ids
- assert not d.developer_id.endswith("_dev")
- assert d.developer_name is not None
- # 2. 严格模式测试:商品未绑定插件直接报错,不进行任何模糊推导
- await db.execute(
- update(VasProduct).where(VasProduct.id == 36).values(plugin_id=None, plugin_name=None, developer_id=None)
- )
- await db.commit()
- try:
- with pytest.raises(BizLogicError) as exc_info:
- await SettlementService.preview_settlement(
- db,
- SettlementPreviewRequest(
- start_date=datetime(2026, 9, 1),
- end_date=datetime(2026, 10, 31),
- partner_a_id="usr-c1f46c54",
- partner_b_id="usr-df718001",
- )
- )
- assert "尚未绑定履约处理插件" in str(exc_info.value)
- finally:
- # 恢复商品 36 的插件绑定
- await db.execute(
- update(VasProduct).where(VasProduct.id == 36).values(plugin_id=3, plugin_name="vfs_plugin", developer_id="usr-c1f46c54")
- )
- await db.commit()
- # 3. 严格模式测试:合伙人未指定直接报错,无任何默认预设
- with pytest.raises(BizLogicError) as p_exc:
- await SettlementService.preview_settlement(
- db,
- SettlementPreviewRequest(
- start_date=datetime(2026, 9, 1),
- end_date=datetime(2026, 10, 31),
- partner_a_id="",
- partner_b_id="usr-df718001",
- )
- )
- assert "请提供合伙人甲方的系统用户ID" in str(p_exc.value)
|