125 lines
4.6 KiB
Python
125 lines
4.6 KiB
Python
"""
|
|
Tests for sanguo_orchestrator.runner module
|
|
Tests Orchestrator task coordination
|
|
"""
|
|
import pytest
|
|
from unittest.mock import Mock, patch
|
|
from sanguo_orchestrator.runner import Orchestrator
|
|
from sanguo_orchestrator.task import TaskState
|
|
|
|
|
|
class TestOrchestrator:
|
|
"""Test Orchestrator initialization and task management"""
|
|
|
|
def test_orchestrator_initialization(self):
|
|
"""Test Orchestrator initializes correctly"""
|
|
orchestrator = Orchestrator(db_path="test.db", max_workers=2)
|
|
assert orchestrator.db_path == "test.db"
|
|
assert orchestrator.pool.max_workers == 2
|
|
assert orchestrator._pending == {}
|
|
|
|
@patch('sanguo_orchestrator.runner.TaskPool')
|
|
def test_submit_cta_creates_task(self, mock_pool_class):
|
|
"""Test submit_cta() creates task and stores spec"""
|
|
mock_pool = Mock()
|
|
mock_pool_class.return_value = mock_pool
|
|
mock_pool.submit.return_value = Mock(task_id="test_1")
|
|
|
|
orchestrator = Orchestrator(db_path="test.db", max_workers=2)
|
|
strategy_class = Mock
|
|
symbol = "AAPL"
|
|
params = {"param1": "value1"}
|
|
start = "2024-01-01"
|
|
end = "2024-12-31"
|
|
cfg = Mock()
|
|
|
|
task_id = orchestrator.submit_cta(strategy_class, symbol, params, start, end, cfg)
|
|
|
|
# Verify task was submitted to pool
|
|
mock_pool.submit.assert_called_once()
|
|
call_args = mock_pool.submit.call_args
|
|
assert call_args[0][0] == task_id # task_id
|
|
assert call_args[0][1] == "cta" # task_type
|
|
|
|
# Verify pending spec was stored
|
|
assert orchestrator._pending["strategy_class"] == strategy_class
|
|
assert orchestrator._pending["symbol"] == symbol
|
|
assert orchestrator._pending["params"] == params
|
|
assert orchestrator._pending["start"] == start
|
|
assert orchestrator._pending["end"] == end
|
|
assert orchestrator._pending["cfg"] == cfg
|
|
|
|
assert task_id.startswith("cta_AAPL_")
|
|
|
|
@patch('sanguo_orchestrator.runner.TaskPool')
|
|
def test_get_status(self, mock_pool_class):
|
|
"""Test get_status() delegates to pool"""
|
|
mock_pool = Mock()
|
|
mock_pool_class.return_value = mock_pool
|
|
mock_pool.get_status.return_value = TaskState.PENDING
|
|
|
|
orchestrator = Orchestrator(db_path="test.db", max_workers=2)
|
|
status = orchestrator.get_status("test_1")
|
|
|
|
mock_pool.get_status.assert_called_once_with("test_1")
|
|
assert status == TaskState.PENDING
|
|
|
|
@patch('sanguo_orchestrator.runner.TaskPool')
|
|
def test_get_status_nonexistent(self, mock_pool_class):
|
|
"""Test get_status() returns None for non-existent task"""
|
|
mock_pool = Mock()
|
|
mock_pool_class.return_value = mock_pool
|
|
mock_pool.get_status.return_value = None
|
|
|
|
orchestrator = Orchestrator(db_path="test.db", max_workers=2)
|
|
status = orchestrator.get_status("nonexistent")
|
|
|
|
assert status is None
|
|
|
|
@patch('sanguo_backtest.result_store.load_result')
|
|
@patch('sanguo_orchestrator.runner.TaskPool')
|
|
def test_get_result_done_task(self, mock_pool_class, mock_load_result):
|
|
"""Test get_result() returns result for DONE task"""
|
|
mock_pool = Mock()
|
|
mock_pool_class.return_value = mock_pool
|
|
|
|
mock_task = Mock()
|
|
mock_task.status = TaskState.DONE
|
|
mock_task.result_id = 12345
|
|
mock_pool.get_task.return_value = mock_task
|
|
|
|
mock_result = Mock()
|
|
mock_load_result.return_value = mock_result
|
|
|
|
orchestrator = Orchestrator(db_path="test.db", max_workers=2)
|
|
result = orchestrator.get_result("test_1")
|
|
|
|
mock_load_result.assert_called_once_with(12345, "test.db")
|
|
assert result == mock_result
|
|
|
|
@patch('sanguo_orchestrator.runner.TaskPool')
|
|
def test_get_result_pending_task(self, mock_pool_class):
|
|
"""Test get_result() returns None for PENDING task"""
|
|
mock_pool = Mock()
|
|
mock_pool_class.return_value = mock_pool
|
|
|
|
mock_task = Mock()
|
|
mock_task.status = TaskState.PENDING
|
|
mock_pool.get_task.return_value = mock_task
|
|
|
|
orchestrator = Orchestrator(db_path="test.db", max_workers=2)
|
|
result = orchestrator.get_result("test_1")
|
|
|
|
assert result is None
|
|
|
|
@patch('sanguo_orchestrator.runner.TaskPool')
|
|
def test_get_result_nonexistent_task(self, mock_pool_class):
|
|
"""Test get_result() returns None for non-existent task"""
|
|
mock_pool = Mock()
|
|
mock_pool_class.return_value = mock_pool
|
|
mock_pool.get_task.return_value = None
|
|
|
|
orchestrator = Orchestrator(db_path="test.db", max_workers=2)
|
|
result = orchestrator.get_result("nonexistent")
|
|
|
|
assert result is None |