import pytest from pydantic import ValidationError from models.channel import ( CashChannel, ChannelType, EWalletChannel, PaymentCardChannel, ) from models.ref import AccountRef @pytest.fixture def account_ref(): """创建账户引用 fixture""" from ulid import ULID return AccountRef(ref_id=ULID()) def test_payment_card_channel(account_ref): """测试支付卡渠道模型""" # 正常创建 card = PaymentCardChannel( ref_accounts=[account_ref], region="HK", issuer_name="HSBC HK", card_suffix="8335", card_brand="UnionPay", ) assert card.channel_type == ChannelType.PAYMENT_CARD assert card.region == "HK" assert card.issuer_name == "HSBC HK" assert card.card_suffix == "8335" assert card.card_brand == "UnionPay" assert len(card.ref_accounts) == 1 # card_brand 可为空 card_without_brand = PaymentCardChannel( ref_accounts=[account_ref], region="HK", issuer_name="AMEX HK", card_suffix="12345", ) assert card_without_brand.card_brand is None # card_suffix 至少一位 with pytest.raises(ValidationError): PaymentCardChannel( ref_accounts=[account_ref], region="CN", issuer_name="渣打中国", card_suffix="" ) # issuer_name 至少一位 with pytest.raises(ValidationError): PaymentCardChannel( ref_accounts=[account_ref], region="SG", issuer_name="", card_suffix="7431" ) # 无效国家代码 with pytest.raises(ValidationError): PaymentCardChannel( ref_accounts=[account_ref], region="XX", issuer_name="Fake Bank", card_suffix="0000" ) # ref_accounts 至少一个 with pytest.raises(ValidationError): PaymentCardChannel(ref_accounts=[], region="CN", issuer_name="招商银行", card_suffix="8888") def test_ewallet_channel(account_ref): """测试电子钱包渠道模型""" # 正常创建 wallet = EWalletChannel( ref_accounts=[account_ref], region="CN", platform="支付宝", platform_account_id="138****0000", sub_channel="余额", ) assert wallet.channel_type == ChannelType.E_WALLET assert wallet.region == "CN" assert wallet.platform == "支付宝" assert wallet.platform_account_id == "138****0000" assert wallet.sub_channel == "余额" # 可选字段可以为空 wallet_minimal = EWalletChannel(ref_accounts=[account_ref], region="CN", platform="WeChat") assert wallet_minimal.platform_account_id is None assert wallet_minimal.sub_channel is None # platform 至少一位 with pytest.raises(ValidationError): EWalletChannel(ref_accounts=[account_ref], region="CN", platform="") def test_cash_channel(account_ref): """测试现金渠道模型""" # 正常创建 cash = CashChannel(ref_accounts=[account_ref]) assert cash.channel_type == ChannelType.CASH assert len(cash.ref_accounts) == 1 # 多个关联账户 cash_multi = CashChannel(ref_accounts=[account_ref, account_ref]) assert len(cash_multi.ref_accounts) == 2 # ref_accounts 至少一个 with pytest.raises(ValidationError): CashChannel(ref_accounts=[]) if __name__ == "__main__": pytest.main([__file__, "-v"])