You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
199 lines
6.3 KiB
199 lines
6.3 KiB
|
2 months ago
|
"""
|
||
|
|
amazingData 服务单元测试
|
||
|
|
"""
|
||
|
|
import pytest
|
||
|
|
from unittest.mock import Mock, patch, MagicMock
|
||
|
|
from datetime import datetime, timedelta
|
||
|
|
|
||
|
|
from app.services.amazing_data_service import AmazingDataService, amazing_data_service
|
||
|
|
from app.services.data_sync_service import DataSyncService
|
||
|
|
|
||
|
|
|
||
|
|
class TestAmazingDataService:
|
||
|
|
"""测试 amazingData 服务"""
|
||
|
|
|
||
|
|
def test_singleton_pattern(self):
|
||
|
|
"""测试单例模式"""
|
||
|
|
service1 = AmazingDataService()
|
||
|
|
service2 = AmazingDataService()
|
||
|
|
assert service1 is service2
|
||
|
|
|
||
|
|
def test_get_instance(self):
|
||
|
|
"""测试获取实例"""
|
||
|
|
service = amazing_data_service
|
||
|
|
assert service is not None
|
||
|
|
assert isinstance(service, AmazingDataService)
|
||
|
|
|
||
|
|
@patch('app.services.amazing_data_service.AmazingDataAdapter')
|
||
|
|
def test_connect_success(self, mock_adapter_class):
|
||
|
|
"""测试连接成功"""
|
||
|
|
mock_adapter = MagicMock()
|
||
|
|
mock_adapter.connect.return_value = True
|
||
|
|
mock_adapter_class.return_value = mock_adapter
|
||
|
|
|
||
|
|
service = AmazingDataService()
|
||
|
|
# 重置初始化标志以便重新测试
|
||
|
|
service._initialized = False
|
||
|
|
service.__init__()
|
||
|
|
|
||
|
|
result = service.connect()
|
||
|
|
|
||
|
|
assert result is True
|
||
|
|
mock_adapter.connect.assert_called_once()
|
||
|
|
|
||
|
|
@patch('app.services.amazing_data_service.AmazingDataAdapter')
|
||
|
|
def test_connect_failure(self, mock_adapter_class):
|
||
|
|
"""测试连接失败"""
|
||
|
|
mock_adapter = MagicMock()
|
||
|
|
mock_adapter.connect.return_value = False
|
||
|
|
mock_adapter_class.return_value = mock_adapter
|
||
|
|
|
||
|
|
service = AmazingDataService()
|
||
|
|
service._initialized = False
|
||
|
|
service.__init__()
|
||
|
|
|
||
|
|
result = service.connect()
|
||
|
|
|
||
|
|
assert result is False
|
||
|
|
|
||
|
|
def test_disconnect(self):
|
||
|
|
"""测试断开连接"""
|
||
|
|
service = amazing_data_service
|
||
|
|
# 注意:实际断开连接需要真实环境
|
||
|
|
# 这里只测试方法存在
|
||
|
|
assert hasattr(service, 'disconnect')
|
||
|
|
|
||
|
|
def test_ensure_connected(self):
|
||
|
|
"""测试确保连接"""
|
||
|
|
service = amazing_data_service
|
||
|
|
assert hasattr(service, 'ensure_connected')
|
||
|
|
|
||
|
|
|
||
|
|
class TestAmazingDataKlineService:
|
||
|
|
"""测试 K 线数据服务"""
|
||
|
|
|
||
|
|
@patch.object(amazing_data_service, 'ensure_connected')
|
||
|
|
@patch.object(amazing_data_service, '_adapter')
|
||
|
|
def test_get_kline_data(self, mock_adapter, mock_ensure):
|
||
|
|
"""测试获取 K 线数据"""
|
||
|
|
import pandas as pd
|
||
|
|
|
||
|
|
# Mock 连接
|
||
|
|
mock_ensure.return_value = True
|
||
|
|
|
||
|
|
# Mock 返回数据
|
||
|
|
mock_df = pd.DataFrame([
|
||
|
|
{
|
||
|
|
'time': '2024-01-01 10:00:00',
|
||
|
|
'open': 3800.0,
|
||
|
|
'high': 3810.0,
|
||
|
|
'low': 3795.0,
|
||
|
|
'close': 3805.0,
|
||
|
|
'volume': 1000,
|
||
|
|
'amount': 3800000.0,
|
||
|
|
'open_interest': 5000
|
||
|
|
}
|
||
|
|
])
|
||
|
|
mock_adapter.get_kline_data.return_value = mock_df
|
||
|
|
|
||
|
|
# 测试(需要实际连接环境,这里仅测试接口)
|
||
|
|
assert hasattr(amazing_data_service, 'get_kline_data')
|
||
|
|
|
||
|
|
@patch.object(amazing_data_service, 'ensure_connected')
|
||
|
|
def test_get_kline_data_not_connected(self, mock_ensure):
|
||
|
|
"""测试未连接时获取 K 线数据"""
|
||
|
|
mock_ensure.return_value = False
|
||
|
|
|
||
|
|
with pytest.raises(Exception) as exc_info:
|
||
|
|
amazing_data_service.get_kline_data(
|
||
|
|
symbol='IF2406',
|
||
|
|
period='1m',
|
||
|
|
start_date='2024-01-01',
|
||
|
|
end_date='2024-01-02'
|
||
|
|
)
|
||
|
|
|
||
|
|
assert '未连接到数据源' in str(exc_info.value)
|
||
|
|
|
||
|
|
|
||
|
|
class TestAmazingDataRealtimeService:
|
||
|
|
"""测试实时行情服务"""
|
||
|
|
|
||
|
|
@patch.object(amazing_data_service, 'ensure_connected')
|
||
|
|
def test_get_realtime_quotes(self, mock_ensure):
|
||
|
|
"""测试获取实时行情"""
|
||
|
|
mock_ensure.return_value = True
|
||
|
|
|
||
|
|
# 测试方法存在
|
||
|
|
assert hasattr(amazing_data_service, 'get_realtime_quotes')
|
||
|
|
|
||
|
|
@patch.object(amazing_data_service, 'ensure_connected')
|
||
|
|
def test_get_realtime_quotes_not_connected(self, mock_ensure):
|
||
|
|
"""测试未连接时获取实时行情"""
|
||
|
|
mock_ensure.return_value = False
|
||
|
|
|
||
|
|
with pytest.raises(Exception) as exc_info:
|
||
|
|
amazing_data_service.get_realtime_quotes(['IF2406', 'IC2406'])
|
||
|
|
|
||
|
|
assert '未连接到数据源' in str(exc_info.value)
|
||
|
|
|
||
|
|
|
||
|
|
class TestAmazingDataSecurityCodes:
|
||
|
|
"""测试证券代码服务"""
|
||
|
|
|
||
|
|
@patch.object(amazing_data_service, 'ensure_connected')
|
||
|
|
def test_get_security_codes(self, mock_ensure):
|
||
|
|
"""测试获取证券代码"""
|
||
|
|
mock_ensure.return_value = True
|
||
|
|
|
||
|
|
assert hasattr(amazing_data_service, 'get_security_codes')
|
||
|
|
|
||
|
|
|
||
|
|
class TestDataSyncService:
|
||
|
|
"""测试数据同步服务"""
|
||
|
|
|
||
|
|
def test_sync_kline_data_method_exists(self):
|
||
|
|
"""测试同步方法存在"""
|
||
|
|
assert hasattr(DataSyncService, 'sync_kline_data')
|
||
|
|
|
||
|
|
def test_sync_all_symbols_method_exists(self):
|
||
|
|
"""测试批量同步方法存在"""
|
||
|
|
assert hasattr(DataSyncService, 'sync_all_symbols')
|
||
|
|
|
||
|
|
def test_default_symbols(self):
|
||
|
|
"""测试默认品种列表"""
|
||
|
|
symbols = DataSyncService.DEFAULT_SYMBOLS
|
||
|
|
assert isinstance(symbols, list)
|
||
|
|
assert len(symbols) > 0
|
||
|
|
|
||
|
|
def test_default_periods(self):
|
||
|
|
"""测试默认周期列表"""
|
||
|
|
periods = DataSyncService.DEFAULT_PERIODS
|
||
|
|
assert isinstance(periods, list)
|
||
|
|
assert '1m' in periods
|
||
|
|
assert '1d' in periods
|
||
|
|
|
||
|
|
|
||
|
|
class TestAmazingDataIntegration:
|
||
|
|
"""集成测试"""
|
||
|
|
|
||
|
|
def test_service_initialization(self):
|
||
|
|
"""测试服务初始化"""
|
||
|
|
service = amazing_data_service
|
||
|
|
assert service is not None
|
||
|
|
assert service._config is not None
|
||
|
|
assert service._config.host == '140.206.44.234'
|
||
|
|
assert service._config.port == 8600
|
||
|
|
|
||
|
|
def test_connection_config(self):
|
||
|
|
"""测试连接配置"""
|
||
|
|
service = amazing_data_service
|
||
|
|
config = service._config
|
||
|
|
|
||
|
|
assert config.username == '11200008169'
|
||
|
|
assert config.password == '11200008169@2026'
|
||
|
|
assert config.use_local_cache is True
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
pytest.main([__file__, '-v'])
|