120 lines
3.7 KiB
Python
Executable File
120 lines
3.7 KiB
Python
Executable File
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
import sys
|
|
import sqlite3
|
|
|
|
# Add the parent directory to path so we can import the app
|
|
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
sys.path.insert(0, parent_dir)
|
|
|
|
from app import app, db_path
|
|
from models import db, User, Thought, MindMapNode, Category
|
|
|
|
def test_database_connection():
|
|
"""Test if the database exists and can be connected to."""
|
|
try:
|
|
if not os.path.exists(db_path):
|
|
print(f"Database file does not exist: {db_path}")
|
|
return False
|
|
|
|
conn = sqlite3.connect(db_path)
|
|
cursor = conn.cursor()
|
|
cursor.execute("PRAGMA integrity_check")
|
|
result = cursor.fetchone()
|
|
conn.close()
|
|
|
|
if result and result[0] == "ok":
|
|
print(f"Database integrity check passed: {db_path}")
|
|
return True
|
|
else:
|
|
print(f"Database integrity check failed: {result}")
|
|
return False
|
|
except Exception as e:
|
|
print(f"Error testing database connection: {e}")
|
|
return False
|
|
|
|
def test_models():
|
|
"""Test if all models are properly defined and can be queried."""
|
|
with app.app_context():
|
|
try:
|
|
print("\nTesting User model...")
|
|
user_count = User.query.count()
|
|
print(f" Found {user_count} users")
|
|
|
|
print("\nTesting Category model...")
|
|
category_count = Category.query.count()
|
|
print(f" Found {category_count} categories")
|
|
|
|
print("\nTesting MindMapNode model...")
|
|
node_count = MindMapNode.query.count()
|
|
print(f" Found {node_count} mindmap nodes")
|
|
|
|
print("\nTesting Thought model...")
|
|
thought_count = Thought.query.count()
|
|
print(f" Found {thought_count} thoughts")
|
|
|
|
if user_count == 0:
|
|
print("\nWARNING: No users found in the database. You might need to create an admin user.")
|
|
|
|
return True
|
|
except Exception as e:
|
|
print(f"Error testing models: {e}")
|
|
return False
|
|
|
|
def print_database_stats():
|
|
"""Print database statistics."""
|
|
with app.app_context():
|
|
try:
|
|
stats = []
|
|
stats.append(("Users", User.query.count()))
|
|
stats.append(("Categories", Category.query.count()))
|
|
stats.append(("Mindmap Nodes", MindMapNode.query.count()))
|
|
stats.append(("Thoughts", Thought.query.count()))
|
|
|
|
print("\nDatabase Statistics:")
|
|
print("-" * 40)
|
|
for name, count in stats:
|
|
print(f"{name:<20} : {count}")
|
|
print("-" * 40)
|
|
|
|
return True
|
|
except Exception as e:
|
|
print(f"Error generating database statistics: {e}")
|
|
return False
|
|
|
|
def run_all_tests():
|
|
"""Run all database tests."""
|
|
success = True
|
|
|
|
print("=" * 60)
|
|
print("STARTING DATABASE TESTS")
|
|
print("=" * 60)
|
|
|
|
# Test database connection
|
|
print("\n1. Testing database connection...")
|
|
if not test_database_connection():
|
|
success = False
|
|
|
|
# Test models
|
|
print("\n2. Testing database models...")
|
|
if not test_models():
|
|
success = False
|
|
|
|
# Print statistics
|
|
print("\n3. Database statistics:")
|
|
if not print_database_stats():
|
|
success = False
|
|
|
|
print("\n" + "=" * 60)
|
|
if success:
|
|
print("All database tests completed successfully!")
|
|
else:
|
|
print("Some database tests failed. Check the output above for details.")
|
|
print("=" * 60)
|
|
|
|
return success
|
|
|
|
if __name__ == "__main__":
|
|
run_all_tests() |