-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_usdc_fixed.py
More file actions
199 lines (158 loc) Β· 6.69 KB
/
test_usdc_fixed.py
File metadata and controls
199 lines (158 loc) Β· 6.69 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
#!/usr/bin/env python3
"""
Fixed test of USDC purchase functionality with proper database setup and valid wallet address
"""
import os
import sqlite3
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def setup_proper_test_data():
"""Set up test data with proper relationships"""
print("π§ Setting up proper test data...")
try:
db_file = 'tradeseer_bot.db'
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
# Clean up any existing test 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,))
# Create test user first
cursor.execute('''
INSERT INTO users
(chat_id, username, first_name, last_name, registration_date, last_activity, is_active, user_type)
VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?, ?)
''', (test_chat_id, 'testuser', 'Test', 'User', 1, 'regular'))
# Get the user_id that was created
user_id = cursor.lastrowid
print(f"β
Created user with ID: {user_id}")
# Create test wallet with correct user_id (using a valid checksum address)
test_wallet = "0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6"
cursor.execute('''
INSERT INTO connected_wallets
(user_id, chat_id, wallet_address, wallet_name, is_active, date_connected)
VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP)
''', (user_id, test_chat_id, test_wallet, 'Test Wallet', 1))
conn.commit()
conn.close()
print(f"β
Test data setup complete")
print(f" - User ID: {user_id}")
print(f" - Chat ID: {test_chat_id}")
print(f" - Wallet: {test_wallet}")
return test_chat_id, user_id, test_wallet
except Exception as e:
print(f"β Database setup failed: {e}")
import traceback
traceback.print_exc()
return None, None, None
def test_usdc_purchase_flow():
"""Test the complete USDC purchase flow"""
print("\nπ Testing Complete USDC Purchase Flow:")
try:
from bot import get_user_by_chat_id, get_user_wallets, handle_buy_usdc
test_chat_id, test_user_id, test_wallet = setup_proper_test_data()
if not test_chat_id:
print("β Cannot proceed without test data")
return
# Test 1: User retrieval
print(f"\nπ Test 1: 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']}")
print(f" Username: {user['username']}, Name: {user['first_name']} {user['last_name']}")
else:
print("β User not found")
return
# Test 2: Wallet retrieval
print(f"\nπ Test 2: 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 3: USDC purchase with specific amount
print(f"\nπ Test 3: USDC purchase with amount: $100")
try:
result = handle_buy_usdc(test_chat_id, "100")
print(f"β
USDC purchase function executed successfully")
print(f" Result: {result}")
except Exception as e:
print(f"β USDC purchase failed: {e}")
import traceback
traceback.print_exc()
# Test 4: USDC purchase without amount (should show menu)
print(f"\nπ Test 4: USDC purchase without amount (menu)")
try:
result = handle_buy_usdc(test_chat_id)
print(f"β
USDC purchase menu function executed successfully")
print(f" Result: {result}")
except Exception as e:
print(f"β USDC purchase menu failed: {e}")
import traceback
traceback.print_exc()
except Exception as e:
print(f"β USDC purchase flow 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}")
print(f" Error type: {type(e).__name__}")
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 USDC Purchase Fixed Test")
print("=" * 50)
try:
test_usdc_purchase_flow()
test_callback_handling()
finally:
cleanup_test_data()
print("\nπ Fixed test completed!")