-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_bot_integration.py
More file actions
187 lines (148 loc) Β· 5.93 KB
/
test_bot_integration.py
File metadata and controls
187 lines (148 loc) Β· 5.93 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
#!/usr/bin/env python3
"""
Comprehensive test of bot integration for USDC purchases
"""
import os
import sqlite3
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def setup_test_database():
"""Set up a test database with a test user and wallet"""
print("π§ Setting up test database...")
try:
# Connect to the bot's database
db_file = 'tradeseer_bot.db'
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
# Create test user
test_chat_id = 12345
test_user_id = 1
cursor.execute('''
INSERT OR REPLACE INTO users
(user_id, chat_id, username, first_name, last_name, is_active)
VALUES (?, ?, ?, ?, ?, ?)
''', (test_user_id, test_chat_id, 'testuser', 'Test', 'User', 1))
# Create test wallet
test_wallet = "0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6"
cursor.execute('''
INSERT OR REPLACE INTO connected_wallets
(user_id, chat_id, wallet_address, wallet_name, is_active)
VALUES (?, ?, ?, ?, ?)
''', (test_user_id, test_chat_id, test_wallet, 'Test Wallet', 1))
conn.commit()
conn.close()
print(f"β
Test database setup complete")
print(f" - User ID: {test_user_id}")
print(f" - Chat ID: {test_chat_id}")
print(f" - Wallet: {test_wallet}")
return test_chat_id, test_user_id, test_wallet
except Exception as e:
print(f"β Database setup failed: {e}")
return None, None, None
def test_bot_functions_with_data():
"""Test bot functions with actual database data"""
print("\nπ Testing Bot Functions with Database Data:")
try:
from bot import get_user_by_chat_id, get_user_wallets, handle_buy_usdc
test_chat_id, test_user_id, test_wallet = setup_test_database()
if not test_chat_id:
print("β Cannot proceed without test data")
return
# Test user retrieval
print(f"\nTesting user retrieval for chat_id: {test_chat_id}")
user = get_user_by_chat_id(test_chat_id)
if user:
print(f"β
User found: ID={user['user_id']}, Chat={user['chat_id']}")
else:
print("β User not found")
return
# Test wallet retrieval
print(f"\nTesting wallet retrieval for chat_id: {test_chat_id}")
wallets = get_user_wallets(test_chat_id)
if wallets:
print(f"β
Wallets found: {len(wallets)} wallets")
for i, wallet in enumerate(wallets):
print(f" {i+1}. {wallet[0]} ({wallet[1]}) - Active: {wallet[2]}")
else:
print("β No wallets found")
return
# Test USDC purchase with amount
print(f"\nTesting USDC purchase with amount: $100")
try:
result = handle_buy_usdc(test_chat_id, "100")
print(f"β
USDC purchase function executed: {result}")
except Exception as e:
print(f"β USDC purchase failed: {e}")
import traceback
traceback.print_exc()
# Test USDC purchase without amount (should show menu)
print(f"\nTesting USDC purchase without amount (menu)")
try:
result = handle_buy_usdc(test_chat_id)
print(f"β
USDC purchase menu function executed: {result}")
except Exception as e:
print(f"β USDC purchase menu failed: {e}")
import traceback
traceback.print_exc()
except Exception as e:
print(f"β Bot functions test failed: {e}")
import traceback
traceback.print_exc()
def test_callback_handling():
"""Test callback handling for USDC purchase amounts"""
print("\nπ Testing Callback Handling:")
try:
from bot import handle_callback_query
test_chat_id = 12345
# Test different callback types
callbacks_to_test = [
"buy_usdc_menu",
"buy_usdc_100",
"buy_usdc_custom"
]
for callback_data in callbacks_to_test:
print(f"\nTesting callback: {callback_data}")
# Create a proper mock callback query object as a dictionary
mock_callback = {
'data': callback_data,
'message': {
'chat': {
'id': test_chat_id
}
}
}
try:
result = handle_callback_query(mock_callback)
print(f"β
Callback '{callback_data}' handled successfully")
except Exception as e:
print(f"β Callback '{callback_data}' failed: {e}")
except Exception as e:
print(f"β Callback handling test failed: {e}")
import traceback
traceback.print_exc()
def cleanup_test_data():
"""Clean up test data from database"""
print("\nπ§Ή Cleaning up test data...")
try:
db_file = 'tradeseer_bot.db'
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
# Remove test user and associated data
test_chat_id = 12345
cursor.execute('DELETE FROM connected_wallets WHERE chat_id = ?', (test_chat_id,))
cursor.execute('DELETE FROM users WHERE chat_id = ?', (test_chat_id,))
conn.commit()
conn.close()
print("β
Test data cleaned up")
except Exception as e:
print(f"β Cleanup failed: {e}")
if __name__ == "__main__":
print("π TradeSeer Bot Integration Test")
print("=" * 50)
try:
test_bot_functions_with_data()
test_callback_handling()
finally:
cleanup_test_data()
print("\nπ Integration test completed!")