Repository navigation
Expand file tree
/
Copy pathtest_user_storage.py
More file actions
141 lines (115 loc) · 4.87 KB
/
Copy pathtest_user_storage.py
File metadata and controls
141 lines (115 loc) · 4.87 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
#!/usr/bin/env python3
"""
Test script to verify user_name storage in chat logs
"""
import sys
from unittest.mock import patch, MagicMock
from datetime import datetime
import uuid
# Mock streamlit secrets
mock_secrets = {
"OPENAI_API_KEY": "test-key",
"ADMIN_PASSWORD": "test-admin"
}
with patch('streamlit.secrets', mock_secrets):
from app.chatlog.chatlog_handler import insert_chat_log, initialize_chatlog_table
from app.db.database_connection import connect_to_db
def test_user_name_storage():
"""Test that user names are properly stored in chat logs"""
print("🧪 Testing user_name storage in chat logs...")
# Test data
test_prompt = "What is market failure?"
test_response = "Market failure occurs when..."
test_conversation_id = str(uuid.uuid4())
test_user_name = "John Doe"
try:
# Initialize the table (this will add user_name column if it doesn't exist)
print("📝 Initializing chatlog table...")
initialize_chatlog_table()
# Insert a test chat log with user_name
print("💾 Inserting test chat log...")
insert_chat_log(test_prompt, test_response, test_conversation_id, test_user_name)
# Query the database to verify the insertion
print("🔍 Verifying insertion...")
conn = connect_to_db()
if conn:
with conn.cursor() as cur:
cur.execute("""
SELECT prompt, response, conversation_id, user_name
FROM chat_logs
WHERE conversation_id = %s
ORDER BY timestamp DESC
LIMIT 1
""", (test_conversation_id,))
result = cur.fetchone()
if result:
stored_prompt, stored_response, stored_conv_id, stored_user_name = result
print(f"✅ Chat log found:")
print(f" Prompt: {stored_prompt}")
print(f" Response: {stored_response[:50]}...")
print(f" Conversation ID: {stored_conv_id}")
print(f" User Name: {stored_user_name}")
if stored_user_name == test_user_name:
print("🎉 SUCCESS: User name stored correctly!")
return True
else:
print(f"❌ FAIL: Expected user name '{test_user_name}', got '{stored_user_name}'")
return False
else:
print("❌ FAIL: No chat log found with the test conversation ID")
return False
conn.close()
else:
print("❌ FAIL: Could not connect to database")
return False
except Exception as e:
print(f"❌ ERROR: {e}")
return False
def test_null_user_name():
"""Test insertion without user_name (should work with NULL)"""
print("\n🧪 Testing chat log insertion without user_name...")
test_prompt = "Test prompt without user"
test_response = "Test response"
test_conversation_id = str(uuid.uuid4())
try:
# Insert without user_name
insert_chat_log(test_prompt, test_response, test_conversation_id)
# Verify insertion
conn = connect_to_db()
if conn:
with conn.cursor() as cur:
cur.execute("""
SELECT user_name FROM chat_logs
WHERE conversation_id = %s
ORDER BY timestamp DESC
LIMIT 1
""", (test_conversation_id,))
result = cur.fetchone()
if result:
stored_user_name = result[0]
print(f"✅ Insertion successful, user_name: {stored_user_name}")
return True
else:
print("❌ FAIL: Chat log not found")
return False
conn.close()
else:
print("❌ FAIL: Could not connect to database")
return False
except Exception as e:
print(f"❌ ERROR: {e}")
return False
def main():
"""Run all tests"""
print("🚀 Testing User Name Storage System\n")
success1 = test_user_name_storage()
success2 = test_null_user_name()
print(f"\n📋 Test Results:")
print(f" User name storage: {'✅ PASS' if success1 else '❌ FAIL'}")
print(f" NULL user name handling: {'✅ PASS' if success2 else '❌ FAIL'}")
if success1 and success2:
print("\n🎉 All tests passed! User name storage is working correctly.")
else:
print("\n⚠️ Some tests failed. Check your database schema and connection.")
if __name__ == "__main__":
main()