| |
| """ |
| Test script to verify database connection and test account creation. |
| """ |
|
|
| import os |
| import sys |
| import logging |
| from datetime import datetime |
|
|
| |
| sys.path.insert(0, os.path.join(os.path.dirname(__file__))) |
|
|
| from flask import Flask |
| from backend.config import Config |
| from backend.utils.database import init_supabase |
|
|
| |
| logging.basicConfig( |
| level=logging.INFO, |
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' |
| ) |
| logger = logging.getLogger(__name__) |
|
|
| def test_database_connection(): |
| """Test database connection and basic operations.""" |
| logger.info("π Testing database connection...") |
| |
| app = Flask(__name__) |
| app.config.from_object(Config) |
| |
| try: |
| |
| supabase = init_supabase(app.config['SUPABASE_URL'], app.config['SUPABASE_KEY']) |
| logger.info("β
Supabase client initialized successfully") |
| |
| |
| logger.info("π Testing basic database connection...") |
| response = supabase.table("Social_network").select("count", count="exact").execute() |
| logger.info(f"β
Database connection successful. Response: {response}") |
| |
| |
| logger.info("π Testing database insertion...") |
| test_data = { |
| "social_network": "test_network", |
| "account_name": "Test Account", |
| "id_utilisateur": "test_user_id", |
| "token": "test_token", |
| "sub": "test_sub", |
| "given_name": "Test", |
| "family_name": "User", |
| "picture": "https://test.com/avatar.jpg" |
| } |
| |
| insert_response = supabase.table("Social_network").insert(test_data).execute() |
| logger.info(f"β
Insert test successful. Response: {insert_response}") |
| |
| if insert_response.data: |
| logger.info(f"β
Record inserted successfully. ID: {insert_response.data[0].get('id')}") |
| |
| |
| logger.info("π Testing record retrieval...") |
| retrieve_response = supabase.table("Social_network").select("*").eq("id", insert_response.data[0].get('id')).execute() |
| logger.info(f"β
Retrieve test successful. Found {len(retrieve_response.data)} records") |
| |
| |
| logger.info("π Testing record deletion...") |
| delete_response = supabase.table("Social_network").delete().eq("id", insert_response.data[0].get('id')).execute() |
| logger.info(f"β
Delete test successful. Response: {delete_response}") |
| |
| logger.info("π All database tests passed!") |
| return True |
| else: |
| logger.error("β Insert test failed - no data returned") |
| return False |
| |
| except Exception as e: |
| logger.error(f"β Database test failed: {str(e)}") |
| import traceback |
| logger.error(f"β Traceback: {traceback.format_exc()}") |
| return False |
|
|
| def test_supabase_auth(): |
| """Test Supabase authentication.""" |
| logger.info("π Testing Supabase authentication...") |
| |
| app = Flask(__name__) |
| app.config.from_object(Config) |
| |
| try: |
| supabase = init_supabase(app.config['SUPABASE_URL'], app.config['SUPABASE_KEY']) |
| |
| |
| logger.info("π Testing auth status...") |
| try: |
| user_response = supabase.auth.get_user() |
| logger.info(f"β
Auth test successful. User: {user_response}") |
| except Exception as auth_error: |
| logger.info(f"βΉοΈ Auth test expected (not authenticated): {str(auth_error)}") |
| |
| logger.info("π Auth test completed!") |
| return True |
| |
| except Exception as e: |
| logger.error(f"β Auth test failed: {str(e)}") |
| return False |
|
|
| def main(): |
| """Main test function.""" |
| logger.info("π Starting database connection tests...") |
| logger.info(f"Test started at: {datetime.now().isoformat()}") |
| |
| |
| db_success = test_database_connection() |
| |
| |
| auth_success = test_supabase_auth() |
| |
| |
| logger.info("π Test Summary:") |
| logger.info(f" Database Connection: {'β
PASS' if db_success else 'β FAIL'}") |
| logger.info(f" Authentication: {'β
PASS' if auth_success else 'β FAIL'}") |
| |
| if db_success and auth_success: |
| logger.info("π All tests passed! Database is working correctly.") |
| return 0 |
| else: |
| logger.error("β Some tests failed. Please check the configuration.") |
| return 1 |
|
|
| if __name__ == "__main__": |
| sys.exit(main()) |