-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_bot.py
More file actions
224 lines (175 loc) Β· 6.57 KB
/
test_bot.py
File metadata and controls
224 lines (175 loc) Β· 6.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python3
"""
Test script for the crypto trading bot.
This script tests the basic functionality including configuration loading,
credential management, and data model validation.
"""
import asyncio
import sys
from pathlib import Path
# Add the project root to Python path
sys.path.insert(0, str(Path(__file__).parent))
from crypto_trading_bot.main import TradingBotApplication
from crypto_trading_bot.utils.config import ConfigManager
from crypto_trading_bot.utils.security import store_api_credentials, load_api_credentials
from crypto_trading_bot.models import (
TradingSignal, MarketData, SignalAction,
create_default_config, validate_trading_signal
)
from datetime import datetime
def test_configuration():
"""Test configuration management."""
print("π§ Testing Configuration Management...")
try:
# Test default config creation
config = create_default_config()
print(f"β
Default config created with {len(config.symbols)} symbols")
print(f" Enabled strategies: {config.get_enabled_strategies()}")
# Test config manager
config_manager = ConfigManager()
loaded_config = config_manager.load_config()
print(f"β
Configuration loaded successfully")
return True
except Exception as e:
print(f"β Configuration test failed: {e}")
return False
def test_credentials():
"""Test credential management."""
print("\nπ Testing Credential Management...")
try:
# Test storing credentials (using dummy values for testing)
api_key = "test_api_key_12345"
api_secret = "test_api_secret_67890"
success = store_api_credentials(api_key, api_secret)
if success:
print("β
Credentials stored successfully")
else:
print("β Failed to store credentials")
return False
# Test loading credentials
loaded_key, loaded_secret = load_api_credentials()
if loaded_key == api_key and loaded_secret == api_secret:
print("β
Credentials loaded and verified successfully")
else:
print("β Credential verification failed")
return False
return True
except Exception as e:
print(f"β Credential test failed: {e}")
return False
def test_data_models():
"""Test data model creation and validation."""
print("\nπ Testing Data Models...")
try:
# Test MarketData creation
market_data = MarketData(
symbol="BTCUSDT",
timestamp=datetime.now(),
price=45000.0,
volume=1000.0,
bid=44999.0,
ask=45001.0
)
print(f"β
MarketData created: {market_data.symbol} @ ${market_data.price}")
# Test TradingSignal creation
signal = TradingSignal(
symbol="BTCUSDT",
action=SignalAction.BUY,
confidence=0.85,
strategy="test_strategy",
target_price=46000.0,
stop_loss=44000.0
)
print(f"β
TradingSignal created: {signal.action.value} {signal.symbol} (confidence: {signal.confidence})")
# Test validation
is_valid = validate_trading_signal(signal)
if is_valid:
print("β
Signal validation passed")
else:
print("β Signal validation failed")
return False
return True
except Exception as e:
print(f"β Data model test failed: {e}")
return False
def test_logging():
"""Test logging system."""
print("\nπ Testing Logging System...")
try:
from crypto_trading_bot.utils.logging_config import setup_logging, get_logger
# Setup logging
setup_logging()
logger = get_logger("test_logger")
# Test different log levels
logger.info("Test info message")
logger.warning("Test warning message")
logger.error("Test error message")
print("β
Logging system working correctly")
return True
except Exception as e:
print(f"β Logging test failed: {e}")
return False
async def test_bot_initialization():
"""Test bot initialization."""
print("\nπ€ Testing Bot Initialization...")
try:
# Create bot application
app = TradingBotApplication()
print("β
Bot application created successfully")
# Test configuration validation
config_manager = ConfigManager()
# Add test credentials to config for validation
test_config = config_manager.load_config()
test_config['api_key'] = "test_key"
test_config['api_secret'] = "test_secret"
# Save config with credentials
success = config_manager.save_config(test_config)
if success:
print("β
Configuration saved with credentials")
else:
print("β Failed to save configuration")
return False
print("β
Bot initialization test completed")
return True
except Exception as e:
print(f"β Bot initialization test failed: {e}")
return False
async def main():
"""Run all tests."""
print("π Starting Crypto Trading Bot Tests\n")
tests = [
("Configuration", test_configuration),
("Credentials", test_credentials),
("Data Models", test_data_models),
("Logging", test_logging),
("Bot Initialization", test_bot_initialization)
]
passed = 0
total = len(tests)
for test_name, test_func in tests:
try:
if asyncio.iscoroutinefunction(test_func):
result = await test_func()
else:
result = test_func()
if result:
passed += 1
except Exception as e:
print(f"β {test_name} test crashed: {e}")
print(f"\nπ Test Results: {passed}/{total} tests passed")
if passed == total:
print("π All tests passed! The bot is ready for development.")
return True
else:
print("β οΈ Some tests failed. Please check the issues above.")
return False
if __name__ == "__main__":
try:
success = asyncio.run(main())
sys.exit(0 if success else 1)
except KeyboardInterrupt:
print("\nβΉοΈ Tests interrupted by user")
sys.exit(1)
except Exception as e:
print(f"\nπ₯ Test runner crashed: {e}")
sys.exit(1)