"""账户资金口径:防止回笼重复计入,账户展示与策略估值分开。""" import unit_test_guard # noqa: F401 import os import sys import unittest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from app.core import rule_gate from app.services import portfolio from test_wiring import install_fakes from test_batch3_units import ctx FUNDS = dict(total_asset=1936261.62, available_cash=1806071.62, frozen_cash=0.0, sell_return_today=1644823.13, market_value=130190.0) class AccountFundsTest(unittest.TestCase): def setUp(self): self.fake = install_fakes(params={'PMS_TOTAL_SCALE': '2000000'}) def funds(self, **values): data = dict(FUNDS, **values) self.fake.qmt.put_snapshot(kind='funds', data=data) return data def gate(self, qty): view = portfolio.positions_view() caps = portfolio.caps_ctx(view, ts_code='600000.SH') return rule_gate.check(side='buy', action='OPEN', qty=qty, price=10, ctx=ctx(caps=caps)) def test_real_snapshot_sale_stat_does_not_create_extra_cash(self): self.funds() cash = portfolio.cash_view() self.assertEqual(cash['cash_avail'], 1806071.62) self.assertAlmostEqual(cash['cash_avail'] + cash['account_market_value'], cash['total_asset']) def test_gate_rejects_order_only_affordable_with_double_count(self): self.funds(available_cash=5000, sell_return_today=10000, total_asset=5000, market_value=0) result = self.gate(1000) self.assertFalse(result['passed']) self.assertTrue(any(x.startswith('INSUFFICIENT_CASH:') for x in result['failed'])) self.assertEqual(result['hard_numbers']['cash_avail'], 5000) def test_returned_sale_cash_remains_usable_in_reported_balance(self): self.funds(available_cash=12000, sell_return_today=8000, total_asset=12000, market_value=0) result = self.gate(1000) self.assertTrue(result['passed'], result) def test_zero_available_is_not_replaced_by_sale_stat(self): self.funds(available_cash=0, sell_return_today=10000, total_asset=10000, frozen_cash=10000, market_value=0) result = self.gate(100) self.assertFalse(result['passed']) self.assertEqual(result['hard_numbers']['cash_avail'], 0) def test_frozen_cash_is_not_subtracted_twice(self): self.funds(available_cash=8000, frozen_cash=2000, total_asset=10000, market_value=0) result = self.gate(800) self.assertTrue(result['passed'], result) self.assertEqual(portfolio.overview()['frozen_cash'], 2000) def test_missing_available_does_not_infer_cash_from_other_fields(self): self.funds(available_cash=None) cash = portfolio.cash_view() self.assertEqual(cash['source'], 'estimate') self.assertIsNone(cash['cash_avail']) self.assertIsNone(cash['account_market_value']) def test_stale_snapshot_does_not_masquerade_as_current_account(self): self.fake.qmt.put_snapshot(kind='funds', data=FUNDS, age_sec=901) result = portfolio.overview() self.assertEqual(result['cash_source'], 'estimate') for key in ('cash_avail', 'total_asset', 'account_market_value', 'frozen_cash'): self.assertIsNone(result[key]) def test_account_display_does_not_change_strategy_valuation(self): self.fake = install_fakes(prices={'002050.SZ': 34.41, '002518.SZ': 27.49, '688778.SH': 37.13}, positions=[{'ts_code': '002050.SZ', 'total_qty': 1800, 'avg_cost': 36.697}, {'ts_code': '002518.SZ', 'total_qty': 400, 'avg_cost': 27.549}, {'ts_code': '688778.SH', 'total_qty': 1400, 'avg_cost': 41.849}], params={'PMS_TOTAL_SCALE': '2000000'}) self.funds() result = portfolio.overview() self.assertEqual(result['account_market_value'], 130190) self.assertEqual(result['portfolio_mv'], 124916) self.assertEqual(result['portfolio_pct'], 0.0625) self.assertEqual(result['float_pnl'], -10746.8) caps = portfolio.caps_ctx(portfolio.positions_view()) self.assertEqual(caps['portfolio_mv'], 124916) self.assertEqual(caps['cash_avail'], 1806071.62) def test_missing_account_value_is_not_replaced_by_market_estimate(self): self.fake = install_fakes(prices={'600000.SH': 10}, positions=[ {'ts_code': '600000.SH', 'total_qty': 100, 'avg_cost': 8}]) self.funds(market_value=None, frozen_cash=None) result = portfolio.overview() self.assertIsNone(result['account_market_value']) self.assertIsNone(result['frozen_cash']) self.assertEqual(result['portfolio_mv'], 1000) self.assertEqual(result['cash_avail'], 1806071.62) if __name__ == '__main__': unittest.main()