-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_installation.py
More file actions
274 lines (218 loc) · 8.31 KB
/
test_installation.py
File metadata and controls
274 lines (218 loc) · 8.31 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/env python3
"""
Test script to verify the installation and basic functionality.
Run this after installing requirements to ensure everything works.
"""
import sys
import subprocess
from pathlib import Path
try:
import pytest
except ImportError:
# pytest not available, create a dummy for standalone use
class pytest:
@staticmethod
def fail(msg):
raise AssertionError(msg)
def test_imports():
"""Test that all required packages can be imported."""
print("=" * 60)
print("Testing Package Imports")
print("=" * 60)
packages = {
'mysql.connector': 'mysql-connector-python',
'pandas': 'pandas',
'numpy': 'numpy',
'torch': 'pytorch',
'transformers': 'transformers',
}
failed = []
for module, package_name in packages.items():
try:
__import__(module)
print(f"✓ {module}")
except ImportError as e:
print(f"✗ {module} - MISSING")
print(f" Install with: pip install {package_name}")
failed.append(module)
assert not failed, f"Failed to import: {failed}"
def test_module_imports():
"""Test that project modules can be imported."""
print("\n" + "=" * 60)
print("Testing Project Modules")
print("=" * 60)
sys.path.insert(0, str(Path(__file__).parent / 'src'))
modules = [
('database', 'StockDatabase'),
('processor', 'PriceProcessor'),
('model', 'StockWordDataset'),
('model', 'StockTransformerModel'),
('analysis', 'plot_training_loss'),
]
failed = []
for module_name, class_name in modules:
try:
module = __import__(module_name)
getattr(module, class_name)
print(f"✓ {module_name}.{class_name}")
except (ImportError, AttributeError) as e:
print(f"✗ {module_name}.{class_name}")
failed.append(f"{module_name}.{class_name}")
assert not failed, f"Failed to import: {failed}"
def test_database_connection(db_password):
"""Test database connection (requires running MySQL).
Run with: pytest --db-password=YOUR_PASSWORD test_installation.py::test_database_connection
"""
if db_password is None:
pytest.skip("Database password not provided. Use --db-password to run this test.")
print("\n" + "=" * 60)
print("Testing Database Connection")
print("=" * 60)
sys.path.insert(0, str(Path(__file__).parent / 'src'))
try:
from database import StockDatabase
db = StockDatabase(password=db_password)
db.connect()
print("✓ Database connection successful")
stocks = db.get_all_stocks()
print(f"✓ Found {len(stocks)} stocks in database")
if len(stocks) > 0:
print(f"✓ Sample stocks: {', '.join([s['ticker'] for s in stocks[:3]])}")
db.close()
print("✓ Database disconnected")
except Exception as e:
print(f"✗ Database connection failed: {e}")
print("\nMake sure:")
print(" - MySQL is running")
print(" - Database 'tinker' exists")
print(" - User 'tinker' has correct password")
print(" - Port 3306 is accessible")
pytest.fail(f"Database connection failed: {e}")
def test_pytorch_cuda():
"""Test PyTorch CUDA support."""
print("\n" + "=" * 60)
print("Testing PyTorch Configuration")
print("=" * 60)
import torch
print(f"PyTorch Version: {torch.__version__}")
if torch.cuda.is_available():
print(f"✓ CUDA is available")
print(f" Device: {torch.cuda.get_device_name(0)}")
print(f" Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB")
else:
print("⚠ CUDA is not available (will use CPU)")
assert torch is not None # Always passes if we get here
def test_model_creation():
"""Test model creation."""
print("\n" + "=" * 60)
print("Testing Model Creation")
print("=" * 60)
sys.path.insert(0, str(Path(__file__).parent / 'src'))
try:
from model import StockTransformerModel
# Test small model
model = StockTransformerModel(
vocab_size=100,
hidden_size=64,
num_hidden_layers=2,
num_attention_heads=2
)
print("✓ Model created successfully")
params = sum(p.numel() for p in model.get_model().parameters())
print(f"✓ Model has {params:,} parameters")
assert model is not None
assert params > 0
except Exception as e:
pytest.fail(f"Model creation failed: {e}")
def test_data_processing():
"""Test data processing pipeline."""
print("\n" + "=" * 60)
print("Testing Data Processing")
print("=" * 60)
sys.path.insert(0, str(Path(__file__).parent / 'src'))
try:
from processor import (
PriceProcessor,
DELTA_VALUES,
DELTA_TO_CHAR,
CHAR_TO_DELTA
)
processor = PriceProcessor(interval_minutes=15)
print("✓ PriceProcessor initialized")
# Test delta encoding
test_deltas = [0.032, -0.015, 0.0, 0.08, -0.03]
for delta in test_deltas:
symbol = processor.delta_to_symbol(delta)
back_to_delta = processor.symbol_to_delta(symbol)
print(f" {delta:.3f} → '{symbol}' → {back_to_delta:.3f}")
print("✓ Delta encoding working")
# Test price parsing
test_prices = ['100.50', '99.99', '101.01']
for price_str in test_prices:
price = processor.parse_price(price_str)
print(f" '{price_str}' → {price:.2f}")
print("✓ Price parsing working")
assert processor is not None
except Exception as e:
pytest.fail(f"Data processing test failed: {e}")
def _run_test(name, test_func, *args):
"""Run a test function and return True/False/None."""
try:
test_func(*args)
return True
except AssertionError as e:
return False
except pytest.skip.Exception:
return None
except Exception as e:
return None
def run_all_tests(db_password=None):
"""Run all tests.
Args:
db_password: Optional database password for database connection test
"""
print("\n" + "=" * 60)
print("PYTINK PROJECT - INSTALLATION TEST")
print("=" * 60 + "\n")
results = {
'Imports': _run_test('Imports', test_imports),
'Project Modules': _run_test('Project Modules', test_module_imports),
'PyTorch': _run_test('PyTorch', test_pytorch_cuda),
'Model Creation': _run_test('Model Creation', test_model_creation),
'Data Processing': _run_test('Data Processing', test_data_processing),
}
# Database test is optional - requires password
results['Database Connection'] = _run_test('Database Connection', test_database_connection, db_password)
# Summary
print("\n" + "=" * 60)
print("TEST SUMMARY")
print("=" * 60)
for test_name, result in results.items():
if result is True:
status = "✓ PASS"
elif result is False:
status = "✗ FAIL"
else:
status = "⚠ SKIP"
print(f"{test_name:.<40} {status}")
all_passed = all(v for v in results.values() if v is not None)
print("\n" + "=" * 60)
if all_passed:
print("✓ ALL TESTS PASSED - Ready to use!")
else:
print("✗ SOME TESTS FAILED - See details above")
print("\nNext steps:")
print("1. Check error messages above")
print("2. Ensure MySQL is running for database tests")
print("3. Run: pip install -r requirements.txt")
print("4. See QUICKSTART.md for setup instructions")
print("=" * 60 + "\n")
return all_passed
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Test installation')
parser.add_argument('--db-password', type=str, default=None,
help='Database password for database connection test')
args = parser.parse_args()
success = run_all_tests(db_password=args.db_password)
sys.exit(0 if success else 1)