Spaces:
Sleeping
Sleeping
Create test_connection.py
Browse files- mongodb/test_connection.py +85 -0
mongodb/test_connection.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Test MongoDB Connection Script
|
| 3 |
+
Run this first to verify your MongoDB connection is working before inserting sample data.
|
| 4 |
+
|
| 5 |
+
Usage: python test_connection.py
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import os
|
| 9 |
+
from pymongo import MongoClient
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
|
| 12 |
+
# Load environment variables
|
| 13 |
+
load_dotenv()
|
| 14 |
+
|
| 15 |
+
def test_mongodb_connection():
|
| 16 |
+
"""Test MongoDB connection using environment variables"""
|
| 17 |
+
try:
|
| 18 |
+
# Get MongoDB URL from environment variables (same as your utils.py)
|
| 19 |
+
mongodb_url = os.getenv('MONGODB_URL') or os.getenv('MONGO_URL') or os.getenv('DATABASE_URL')
|
| 20 |
+
|
| 21 |
+
if not mongodb_url:
|
| 22 |
+
print("β Error: No MongoDB URL found in environment variables")
|
| 23 |
+
print("Please add one of these to your .env file:")
|
| 24 |
+
print(" MONGODB_URL=your_mongodb_connection_string")
|
| 25 |
+
print(" MONGO_URL=your_mongodb_connection_string")
|
| 26 |
+
print(" DATABASE_URL=your_mongodb_connection_string")
|
| 27 |
+
return False
|
| 28 |
+
|
| 29 |
+
print(f"π Connecting to MongoDB...")
|
| 30 |
+
print(f"URL: {mongodb_url[:20]}{'...' if len(mongodb_url) > 20 else ''}")
|
| 31 |
+
|
| 32 |
+
# Connect to MongoDB
|
| 33 |
+
client = MongoClient(mongodb_url, serverSelectionTimeoutMS=5000)
|
| 34 |
+
|
| 35 |
+
# Test the connection
|
| 36 |
+
client.admin.command('ping')
|
| 37 |
+
print("β
Successfully connected to MongoDB!")
|
| 38 |
+
|
| 39 |
+
# Get database name
|
| 40 |
+
db_name = os.getenv('MONGODB_DATABASE', 'sparrow_logistics')
|
| 41 |
+
db = client[db_name]
|
| 42 |
+
print(f"β
Database: {db_name}")
|
| 43 |
+
|
| 44 |
+
# List existing collections
|
| 45 |
+
collections = db.list_collection_names()
|
| 46 |
+
if collections:
|
| 47 |
+
print(f"π Existing collections: {', '.join(collections)}")
|
| 48 |
+
else:
|
| 49 |
+
print("π No existing collections found (this is normal for a new database)")
|
| 50 |
+
|
| 51 |
+
# Test write permission
|
| 52 |
+
test_collection = db.connection_test
|
| 53 |
+
test_doc = {"test": "connection", "timestamp": "now"}
|
| 54 |
+
result = test_collection.insert_one(test_doc)
|
| 55 |
+
print(f"β
Write test successful (inserted document: {result.inserted_id})")
|
| 56 |
+
|
| 57 |
+
# Clean up test document
|
| 58 |
+
test_collection.delete_one({"_id": result.inserted_id})
|
| 59 |
+
print("β
Cleanup successful")
|
| 60 |
+
|
| 61 |
+
client.close()
|
| 62 |
+
print("π MongoDB connection test completed successfully!")
|
| 63 |
+
print("You can now run the sample data insertion script.")
|
| 64 |
+
return True
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
print(f"β MongoDB connection failed: {e}")
|
| 68 |
+
print("\nTroubleshooting tips:")
|
| 69 |
+
print("1. Check your .env file has the correct MONGODB_URL")
|
| 70 |
+
print("2. Verify your MongoDB server is running")
|
| 71 |
+
print("3. Check your network connection")
|
| 72 |
+
print("4. Verify your MongoDB credentials are correct")
|
| 73 |
+
return False
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
print("MongoDB Connection Test for Sparrow Logistics")
|
| 77 |
+
print("=" * 50)
|
| 78 |
+
|
| 79 |
+
success = test_mongodb_connection()
|
| 80 |
+
|
| 81 |
+
if success:
|
| 82 |
+
print("\nβ
Ready to proceed with sample data insertion!")
|
| 83 |
+
else:
|
| 84 |
+
print("\nβ Please fix the connection issues before proceeding.")
|
| 85 |
+
exit(1)
|