Optimize
This commit is contained in:
BIN
py-client/tests/__pycache__/test_market.cpython-311.pyc
Normal file
BIN
py-client/tests/__pycache__/test_market.cpython-311.pyc
Normal file
Binary file not shown.
Binary file not shown.
22
py-client/tests/test_market.py
Normal file
22
py-client/tests/test_market.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from libs.market import market_allow_open, refresh_market
|
||||
|
||||
|
||||
class MarketCacheTests(unittest.TestCase):
|
||||
def test_refresh_updates_open_cache(self):
|
||||
with patch("libs.market.get_json", return_value={"data": {"action": "UP"}}):
|
||||
self.assertEqual(refresh_market("http://example"), "UP")
|
||||
self.assertTrue(market_allow_open())
|
||||
|
||||
def test_refresh_failure_blocks_open(self):
|
||||
with patch("libs.market.get_json", side_effect=OSError("offline")):
|
||||
self.assertEqual(refresh_market("http://example"), "UNKNOWN")
|
||||
self.assertFalse(market_allow_open())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from datetime import datetime, timedelta
|
||||
from tempfile import TemporaryDirectory
|
||||
from types import SimpleNamespace
|
||||
@@ -151,28 +152,29 @@ class TrendTests(unittest.TestCase):
|
||||
deals=lambda: [],
|
||||
full_tick=lambda _codes: {"A": Tick(last_price=11)},
|
||||
)
|
||||
runtime = SimpleNamespace(
|
||||
client=client,
|
||||
account_cfg=SimpleNamespace(min_cash_ratio=0.1),
|
||||
global_cfg=SimpleNamespace(api_host="http://example"),
|
||||
orders=SimpleNamespace(refresh=lambda _client: None),
|
||||
state=SimpleNamespace(
|
||||
codes=["A"],
|
||||
unresolved_codes=[],
|
||||
reconcile=lambda *_args: None,
|
||||
),
|
||||
)
|
||||
with (
|
||||
patch("strategy.trend.boot.trading_time", return_value=True),
|
||||
patch("strategy.trend.boot.market_allow_open", return_value=True),
|
||||
patch("strategy.trend.boot.open_signal") as open_mock,
|
||||
patch("strategy.trend.boot.manage_positions") as manage_mock,
|
||||
):
|
||||
RunOnce(runtime, [])
|
||||
with ThreadPoolExecutor(max_workers=2) as executor:
|
||||
runtime = SimpleNamespace(
|
||||
client=client,
|
||||
account_cfg=SimpleNamespace(min_cash_ratio=0.1),
|
||||
global_cfg=SimpleNamespace(api_host="http://example"),
|
||||
orders=SimpleNamespace(refresh=lambda _client: None, data=[]),
|
||||
state=SimpleNamespace(
|
||||
codes=["A"],
|
||||
reconcile=lambda *_args: None,
|
||||
),
|
||||
executor=executor,
|
||||
)
|
||||
with (
|
||||
patch("strategy.trend.boot.trading_time", return_value=True),
|
||||
patch("strategy.trend.boot.market_allow_open", return_value=True),
|
||||
patch("strategy.trend.boot.open_signal") as open_mock,
|
||||
patch("strategy.trend.boot.manage_positions") as manage_mock,
|
||||
):
|
||||
RunOnce(runtime, [])
|
||||
open_mock.assert_not_called()
|
||||
manage_mock.assert_called_once()
|
||||
|
||||
def test_unknown_order_without_position_blocks_reopen(self):
|
||||
def test_state_without_broker_order_allows_reopen(self):
|
||||
with TemporaryDirectory() as directory:
|
||||
state = State.for_strategy(directory, "trend", "A")
|
||||
state.set(StateItem(
|
||||
@@ -189,26 +191,28 @@ class TrendTests(unittest.TestCase):
|
||||
deals=lambda: [],
|
||||
full_tick=lambda _codes: {"A": Tick(last_price=10)},
|
||||
)
|
||||
runtime = SimpleNamespace(
|
||||
client=client,
|
||||
account_cfg=SimpleNamespace(min_cash_ratio=0.1),
|
||||
global_cfg=SimpleNamespace(api_host="http://example"),
|
||||
orders=OrderBook(),
|
||||
state=state,
|
||||
open_watch=SimpleNamespace(forget=lambda _code: None),
|
||||
add_watch=SimpleNamespace(forget=lambda _code: None),
|
||||
)
|
||||
signal = SimpleNamespace(code="A", signal_key="morning")
|
||||
with (
|
||||
patch("strategy.trend.boot.trading_time", return_value=True),
|
||||
patch("strategy.trend.boot.market_allow_open", return_value=True),
|
||||
patch("strategy.trend.boot.open_signal") as open_mock,
|
||||
patch("strategy.trend.boot.manage_positions"),
|
||||
):
|
||||
RunOnce(runtime, [signal])
|
||||
with ThreadPoolExecutor(max_workers=2) as executor:
|
||||
runtime = SimpleNamespace(
|
||||
client=client,
|
||||
account_cfg=SimpleNamespace(min_cash_ratio=0.1),
|
||||
global_cfg=SimpleNamespace(api_host="http://example"),
|
||||
orders=OrderBook(),
|
||||
state=state,
|
||||
open_watch=SimpleNamespace(forget=lambda _code: None),
|
||||
add_watch=SimpleNamespace(forget=lambda _code: None),
|
||||
executor=executor,
|
||||
)
|
||||
with (
|
||||
patch("strategy.trend.boot.trading_time", return_value=True),
|
||||
patch("strategy.trend.boot.market_allow_open", return_value=True),
|
||||
patch("strategy.trend.boot.open_signal") as open_mock,
|
||||
patch("strategy.trend.boot.manage_positions"),
|
||||
):
|
||||
RunOnce(runtime, [signal])
|
||||
|
||||
open_mock.assert_not_called()
|
||||
self.assertTrue(state.has_unresolved_order("A"))
|
||||
open_mock.assert_called_once()
|
||||
self.assertEqual(state.codes, [])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user