import pytest from pydantic import ValidationError from models.account import ( AccountType, BankAccount, CashAccount, EWalletAccount, ) from models.ref import AccountRef def test_bank_account(): """测试银行账户模型""" # 正常创建(最简) bank = BankAccount(name="招行工资卡", issuer_name="招商银行", account_suffix="8888") assert bank.account_type == AccountType.ASSET assert bank.name == "招行工资卡" assert bank.issuer_name == "招商银行" assert bank.account_suffix == "8888" assert bank.account_number is None assert bank.sub_accounts == [] assert bank.is_active is True assert bank.deleted_at is None # 带可选字段 ref = AccountRef(ref_id=bank.id) bank_full = BankAccount( name="招行工资卡", issuer_name="招商银行", account_suffix="8888", account_number="6225880000008888", sub_accounts=[ref], primary_currency="CNY", supported_currencies=["CNY", "HKD", "USD"], remark="主账户", ) assert bank_full.account_number == "6225880000008888" assert len(bank_full.sub_accounts) == 1 assert bank_full.primary_currency == "CNY" assert len(bank_full.supported_currencies) == 3 # 负债类型(信用卡) credit = BankAccount( name="招行信用卡", issuer_name="招商银行", account_suffix="1234", account_type=AccountType.LIABILITY, ) assert credit.account_type == AccountType.LIABILITY # 必填字段缺失 with pytest.raises(ValidationError): BankAccount(name="", issuer_name="招商银行", account_suffix="8888") with pytest.raises(ValidationError): BankAccount(name="招行工资卡", issuer_name="", account_suffix="8888") def test_ewallet_account(): """测试电子钱包账户模型""" # 正常创建(最简) wallet = EWalletAccount(name="支付宝日常", platform="Alipay") assert wallet.account_type == AccountType.ASSET assert wallet.platform == "Alipay" assert wallet.account_id is None # 带可选字段 wallet_full = EWalletAccount( name="支付宝日常", platform="Alipay", account_id="138****0000", primary_currency="CNY", remark="日常消费", ) assert wallet_full.account_id == "138****0000" # platform 必填 with pytest.raises(ValidationError): EWalletAccount(name="支付宝", platform="") with pytest.raises(ValidationError): EWalletAccount(platform="Alipay") def test_cash_account(): """测试现金账户模型""" # 正常创建(最简) cash = CashAccount(name="家里现金") assert cash.account_type == AccountType.ASSET assert cash.location is None # 带可选字段 cash_full = CashAccount(name="家里现金", location="保险箱") assert cash_full.location == "保险箱" # name 必填 with pytest.raises(ValidationError): CashAccount(name="") def test_account_ref(): """测试账户引用""" from models.account import BankAccount from ulid import ULID bank = BankAccount(name="测试账户", issuer_name="测试银行", account_suffix="0000") ref = AccountRef(ref_id=bank.id) assert ref.ref_type == "ACCOUNT" assert ref.ref_id == bank.id