#!/usr/bin/env python3 """ Test MongoDB connection for user authentication tests This script verifies that the MongoDB connection is working properly for the user authentication test suite. """ import sys import os sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # Load environment variables try: from dotenv import load_dotenv load_dotenv() print("โœ… Environment variables loaded") except ImportError: print("โš ๏ธ dotenv not available - continuing without .env file loading") import asyncio from analytics.database import connect_to_database, get_database async def test_mongodb_connection(): """Test MongoDB connection""" print("\n๐Ÿ” Testing MongoDB Connection") print("=" * 40) # Check environment variables mongodb_url = os.getenv("MONGODB_URL") mongodb_db = os.getenv("MONGODB_DATABASE") if mongodb_url: print(f"โœ… MONGODB_URL found: {mongodb_url[:30]}...") else: print("โŒ MONGODB_URL not found") return False if mongodb_db: print(f"โœ… MONGODB_DATABASE found: {mongodb_db}") else: print("โŒ MONGODB_DATABASE not found") return False # Test database connection try: print("\n๐Ÿ”— Attempting to connect to MongoDB...") database = await connect_to_database() if database is not None: print("โœ… MongoDB connection successful!") # Test a simple operation try: # Try to list collections collections = await database.list_collection_names() print(f"โœ… Database accessible - found {len(collections)} collections") if collections: print(" Collections:", ", ".join(collections[:5])) if len(collections) > 5: print(f" ... and {len(collections) - 5} more") return True except Exception as e: print(f"โš ๏ธ Database accessible but operation failed: {e}") return True # Connection works, operation might need permissions else: print("โŒ MongoDB connection failed - falling back to JSON storage") return False except Exception as e: print(f"โŒ MongoDB connection error: {e}") return False async def test_analytics_collections(): """Test analytics collections access""" print("\n๐Ÿ“Š Testing Analytics Collections") print("=" * 40) try: from analytics.database import ( get_sessions_collection, get_messages_collection, get_search_analytics_collection ) # Test sessions collection sessions_collection = await get_sessions_collection() if sessions_collection is not None: count = await sessions_collection.count_documents({}) print(f"โœ… Sessions collection accessible - {count} documents") else: print("โš ๏ธ Sessions collection not available") # Test messages collection messages_collection = await get_messages_collection() if messages_collection is not None: count = await messages_collection.count_documents({}) print(f"โœ… Messages collection accessible - {count} documents") else: print("โš ๏ธ Messages collection not available") # Test search analytics collection search_collection = await get_search_analytics_collection() if search_collection is not None: count = await search_collection.count_documents({}) print(f"โœ… Search analytics collection accessible - {count} documents") else: print("โš ๏ธ Search analytics collection not available") return True except Exception as e: print(f"โŒ Analytics collections test failed: {e}") return False async def main(): """Main test function""" print("๐Ÿงช MongoDB Connection Test for User Authentication") print("=" * 60) # Test basic connection connection_ok = await test_mongodb_connection() # Test analytics collections collections_ok = await test_analytics_collections() print("\n" + "=" * 60) if connection_ok and collections_ok: print("๐ŸŽ‰ MongoDB is ready for user authentication tests!") print("โœ… All database operations should work correctly") elif connection_ok: print("โš ๏ธ MongoDB connection works but some collections may need setup") print("โœ… Basic tests should work, some advanced tests may be limited") else: print("โŒ MongoDB connection failed") print("โš ๏ธ Tests will use JSON file fallback storage") print("โœ… Tests will still run but without persistent database storage") return connection_ok if __name__ == "__main__": success = asyncio.run(main()) sys.exit(0 if success else 1)