-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_setup.py
More file actions
executable file
·238 lines (193 loc) · 6.94 KB
/
test_setup.py
File metadata and controls
executable file
·238 lines (193 loc) · 6.94 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
#!/usr/bin/env python3
"""
Test script to verify Telegram client setup
"""
import sys
import os
import json
from typing import Dict, List
def test_imports() -> bool:
"""Test if all required modules can be imported"""
print("Testing imports...")
try:
import requests
print("✓ requests module imported successfully")
except ImportError as e:
print(f"✗ Failed to import requests: {e}")
return False
try:
import sqlite3
print("✓ sqlite3 module imported successfully")
except ImportError as e:
print(f"✗ Failed to import sqlite3: {e}")
return False
try:
import redis
print("✓ redis module imported successfully")
except ImportError as e:
print(f"✗ Failed to import redis: {e}")
return False
try:
from dotenv import load_dotenv
print("✓ python-dotenv module imported successfully")
except ImportError as e:
print(f"✗ Failed to import python-dotenv: {e}")
return False
try:
import telegram
print("✓ python-telegram-bot module imported successfully")
except ImportError as e:
print(f"✗ Failed to import python-telegram-bot: {e}")
return False
return True
def test_config() -> bool:
"""Test configuration loading"""
print("\nTesting configuration...")
try:
from config import Config, validate_config
print("✓ Config module imported successfully")
# Test if .env file exists
if os.path.exists('.env'):
print("✓ .env file found")
else:
print("✗ .env file not found")
return False
# Test configuration validation
try:
validate_config()
print("✓ Configuration validation passed")
except ValueError as e:
print(f"✗ Configuration validation failed: {e}")
return False
return True
except Exception as e:
print(f"✗ Failed to test configuration: {e}")
return False
def test_database() -> bool:
"""Test database connection"""
print("\nTesting database...")
try:
from database import DatabaseManager
from config import setup_logging
setup_logging()
db = DatabaseManager()
# Test basic operations
test_message = {
'message_id': 'test_123',
'text': 'Test message',
'timestamp': '2024-01-01T12:00:00'
}
# Test storing pending message
if db.store_pending_message(test_message):
print("✓ Store pending message successful")
else:
print("✗ Store pending message failed")
return False
# Test getting pending messages
pending = db.get_pending_messages(limit=1)
if pending and len(pending) > 0:
print("✓ Get pending messages successful")
else:
print("✗ Get pending messages failed")
return False
# Test removing pending message
if db.remove_pending_message(pending[0]['id']):
print("✓ Remove pending message successful")
else:
print("✗ Remove pending message failed")
return False
# Test storing sent message
if db.store_sent_message('test_123', test_message, 'test response'):
print("✓ Store sent message successful")
else:
print("✗ Store sent message failed")
return False
# Test getting stats
stats = db.get_stats()
if stats:
print(f"✓ Database stats: {stats}")
else:
print("✗ Get database stats failed")
return False
db.close()
return True
except Exception as e:
print(f"✗ Database test failed: {e}")
return False
def test_webhook_client() -> bool:
"""Test webhook client"""
print("\nTesting webhook client...")
try:
from webhook_client import WebhookClient
client = WebhookClient()
print("✓ WebhookClient created successfully")
# Test with a mock webhook URL (this will fail but we can test the client creation)
test_message = {
'message_id': 'test_123',
'text': 'Test message',
'timestamp': '2024-01-01T12:00:00'
}
print("✓ WebhookClient test completed (connection test skipped)")
return True
except Exception as e:
print(f"✗ WebhookClient test failed: {e}")
return False
def test_telegram_client() -> bool:
"""Test Telegram client initialization"""
print("\nTesting Telegram client...")
try:
from telegram_client import TelegramClient
# This will fail if config is invalid, but we can test the import
print("✓ TelegramClient module imported successfully")
print("✓ Telegram client test completed (full initialization skipped)")
return True
except Exception as e:
print(f"✗ TelegramClient test failed: {e}")
return False
def main():
"""Run all tests"""
print("=" * 50)
print("Telegram Client Setup Test")
print("=" * 50)
tests = [
("Imports", test_imports),
("Configuration", test_config),
("Database", test_database),
("Webhook Client", test_webhook_client),
("Telegram Client", test_telegram_client)
]
results = []
for test_name, test_func in tests:
try:
result = test_func()
results.append((test_name, result))
except Exception as e:
print(f"✗ {test_name} test crashed: {e}")
results.append((test_name, False))
print("\n" + "=" * 50)
print("Test Results Summary")
print("=" * 50)
all_passed = True
for test_name, result in results:
status = "PASS" if result else "FAIL"
print(f"{test_name}: {status}")
if not result:
all_passed = False
print("\n" + "=" * 50)
if all_passed:
print("✓ All tests passed! Your setup is ready.")
print("\nNext steps:")
print("1. Start the service: sudo systemctl start telegram-client.service")
print("2. Check status: ./status.sh")
print("3. Send a test message to your Telegram bot")
else:
print("✗ Some tests failed. Please check the errors above.")
print("\nTroubleshooting:")
print("1. Make sure you've run the installation script: ./install.sh")
print("2. Check that your .env file is properly configured")
print("3. Verify all dependencies are installed")
print("4. Check the logs for more details")
print("=" * 50)
return 0 if all_passed else 1
if __name__ == "__main__":
sys.exit(main())