89 lines
4.3 KiB
Python
89 lines
4.3 KiB
Python
|
|
"""持仓展示与批次查询的策略隔离。"""
|
||
|
|
import unit_test_guard # noqa: F401
|
||
|
|
import copy
|
||
|
|
import unittest
|
||
|
|
from unittest.mock import patch
|
||
|
|
from test_wiring import install_fakes
|
||
|
|
from app.core import strategy_book as books
|
||
|
|
from app.services import portfolio
|
||
|
|
from config.settings import settings
|
||
|
|
|
||
|
|
|
||
|
|
class PositionGroupsTests(unittest.TestCase):
|
||
|
|
def setUp(self):
|
||
|
|
install_fakes(prices={'600000.SH': 12}, positions=[
|
||
|
|
{'ts_code': '600000.SH', 'total_qty': 100, 'avail_qty': 100, 'avg_cost': 10}])
|
||
|
|
self.original = portfolio.positions_view()
|
||
|
|
|
||
|
|
def test_unmigrated_micro_is_empty_without_query(self):
|
||
|
|
before = copy.deepcopy(self.original)
|
||
|
|
with patch.object(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', False), \
|
||
|
|
patch.object(portfolio, 'positions_view') as read:
|
||
|
|
groups = portfolio.position_groups(self.original)
|
||
|
|
read.assert_not_called()
|
||
|
|
self.assertEqual(groups[0]['totals']['market_value'], 1200)
|
||
|
|
self.assertEqual(groups[1]['held'], [])
|
||
|
|
self.assertTrue(groups[1]['ready'])
|
||
|
|
self.assertEqual(self.original, before)
|
||
|
|
|
||
|
|
def test_same_stock_keeps_separate_quantity_cost_and_profit(self):
|
||
|
|
def read(**kwargs):
|
||
|
|
self.assertEqual(books.CURRENT.get(), 'micro30')
|
||
|
|
return [{'ts_code': '600000.SH', 'total_qty': 300, 'avg_cost': 11}]
|
||
|
|
with patch.object(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', True), \
|
||
|
|
patch.object(portfolio.pms_repo, 'list_positions', side_effect=read):
|
||
|
|
groups = portfolio.position_groups(self.original)
|
||
|
|
self.assertEqual([g['held'][0]['total_qty'] for g in groups], [100, 300])
|
||
|
|
self.assertEqual([g['held'][0]['avg_cost'] for g in groups], [10, 11])
|
||
|
|
self.assertEqual([g['totals']['pnl'] for g in groups], [200, 300])
|
||
|
|
self.assertEqual([g['held'][0]['book_id'] for g in groups], ['original', 'micro30'])
|
||
|
|
self.assertEqual(books.CURRENT.get(), 'original')
|
||
|
|
|
||
|
|
def test_missing_quote_does_not_claim_zero_profit(self):
|
||
|
|
self.original['price_missing'] = ['600000.SH']
|
||
|
|
with patch.object(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', False):
|
||
|
|
groups = portfolio.position_groups(self.original)
|
||
|
|
self.assertIsNone(groups[0]['totals']['pnl'])
|
||
|
|
self.assertEqual(groups[0]['price_missing'], ['600000.SH'])
|
||
|
|
|
||
|
|
def test_query_failure_is_not_empty_position(self):
|
||
|
|
with patch.object(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', True), \
|
||
|
|
patch.object(portfolio, 'positions_view', side_effect=RuntimeError('offline')):
|
||
|
|
groups = portfolio.position_groups(self.original)
|
||
|
|
self.assertTrue(groups[0]['ready'])
|
||
|
|
self.assertFalse(groups[1]['ready'])
|
||
|
|
self.assertIsNone(groups[1]['totals']['market_value'])
|
||
|
|
self.assertIsNone(groups[1]['totals']['pnl'])
|
||
|
|
self.assertEqual(books.CURRENT.get(), 'original')
|
||
|
|
|
||
|
|
def test_lots_same_code_follow_selected_book(self):
|
||
|
|
def read(code, status):
|
||
|
|
return [{'ts_code': code, 'book_id': books.CURRENT.get(), 'status': status}]
|
||
|
|
with patch.object(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', True), \
|
||
|
|
patch.object(portfolio.pms_repo, 'list_lots', side_effect=read):
|
||
|
|
for book in ('original', 'micro30'):
|
||
|
|
rows = portfolio.position_lots('600000.SH', book, None)
|
||
|
|
self.assertEqual(rows[0]['book_id'], book)
|
||
|
|
self.assertIsNone(rows[0]['status'])
|
||
|
|
self.assertEqual(books.CURRENT.get(), 'original')
|
||
|
|
|
||
|
|
def test_unmigrated_micro_lots_never_read_original(self):
|
||
|
|
with patch.object(settings, 'INFRA_STRATEGY_BOOK_SCHEMA', False), \
|
||
|
|
patch.object(portfolio.pms_repo, 'list_lots') as read:
|
||
|
|
self.assertEqual(portfolio.position_lots('600000.SH', 'micro30', None), [])
|
||
|
|
read.assert_not_called()
|
||
|
|
|
||
|
|
def test_unknown_book_is_rejected(self):
|
||
|
|
with self.assertRaises(ValueError):
|
||
|
|
portfolio.position_lots('600000.SH', 'all', None)
|
||
|
|
|
||
|
|
def test_api_passes_selected_book_and_all_statuses(self):
|
||
|
|
from app.web.main import api_lots
|
||
|
|
with patch.object(portfolio, 'position_lots', return_value=[]) as read:
|
||
|
|
api_lots('600000.SH', status='', book_id='micro30')
|
||
|
|
read.assert_called_once_with('600000.SH', 'micro30', status=None)
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
unittest.main()
|