-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtest_database.py
More file actions
118 lines (100 loc) · 3.95 KB
/
test_database.py
File metadata and controls
118 lines (100 loc) · 3.95 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
"""
Test script to verify database initialization and basic operations
This script tests the persistent database implementation
"""
import os
import sys
# Add server directory to path
sys.path.append(os.path.join(os.path.dirname(__file__), 'server'))
from flask import Flask
from server.middleware.database import init_db
from server.models import User, Session, ChatHistory, ProjectMetadata
from server.utils.db_utils import (
ensure_database_directory,
create_session_for_user,
add_chat_message,
create_project_metadata,
get_database_stats
)
def test_database_setup():
"""Test database initialization"""
print("🧪 Testing Database Setup...")
# Create test Flask app
app = Flask(__name__)
app.config['SECRET_KEY'] = 'test-secret-key'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test_calliope.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
with app.app_context():
try:
# Initialize database
db = init_db(app)
print("✅ Database initialized successfully")
# Ensure database directory exists
ensure_database_directory()
print("✅ Database directory created")
# Check if tables were created
from sqlalchemy import inspect
inspector = inspect(db.engine)
tables = inspector.get_table_names()
expected_tables = ['users', 'refresh_tokens', 'sessions', 'chat_history', 'project_metadata']
for table in expected_tables:
if table in tables:
print(f"✅ Table '{table}' created")
else:
print(f"❌ Table '{table}' missing")
# Test basic operations
print("\n🧪 Testing Basic Operations...")
# Create test user
test_user = User(
email='test@example.com',
username='testuser',
password='testpassword123'
)
db.session.add(test_user)
db.session.commit()
print("✅ Test user created")
# Create session
session = create_session_for_user(
user_id=test_user.id,
session_token='test_session_123',
instance_dir='test_instance',
port=8080
)
print("✅ Test session created")
# Add chat message
chat_msg = add_chat_message(
session_id=session.id,
role='user',
content='Test message',
message_type='text'
)
print("✅ Chat message added")
# Create project
project = create_project_metadata(
user_id=test_user.id,
project_name='Test Project',
description='A test project',
project_type='web_app',
language='python'
)
print("✅ Project metadata created")
# Get stats
stats = get_database_stats()
print(f"✅ Database stats retrieved: {stats}")
# Cleanup test database
os.remove('test_calliope.db')
print("✅ Test database cleaned up")
print("\n🎉 All tests passed! Database implementation is working correctly.")
return True
except Exception as e:
print(f"❌ Error during testing: {str(e)}")
# Cleanup on error
if os.path.exists('test_calliope.db'):
os.remove('test_calliope.db')
return False
if __name__ == "__main__":
success = test_database_setup()
if success:
print("\n✅ Database implementation is ready for production!")
else:
print("\n❌ Database implementation has issues that need to be fixed.")