Skip to content

Commit 357c293

Browse files
committed
fix: Add test connectivity script
1 parent babf694 commit 357c293

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test MongoDB connection and display database information.
4+
5+
Usage:
6+
python test_connection.py
7+
python test_connection.py --database assertionservice
8+
"""
9+
10+
import argparse
11+
import sys
12+
13+
# Import shared modules
14+
from logger_config import setup_logger
15+
from db_connection import MongoDBConnection
16+
from config import Config
17+
18+
# Set up logging
19+
logger = setup_logger(__name__, log_file='test-connection.log')
20+
21+
22+
def test_database_connection(uri: str, database_name: str) -> bool:
23+
"""
24+
Test connection to a specific database.
25+
26+
Returns:
27+
True if connection successful, False otherwise
28+
"""
29+
print("\n" + "="*80)
30+
print(f"Testing Connection: {database_name}")
31+
print("="*80)
32+
33+
connection = MongoDBConnection(uri, database_name)
34+
35+
try:
36+
if not connection.connect():
37+
print(f"❌ Failed to connect to {database_name}")
38+
return False
39+
40+
print(f"✅ Successfully connected to database: {database_name}")
41+
42+
# List collections
43+
collections = connection.db.list_collection_names()
44+
print(f"\nCollections found: {len(collections)}")
45+
46+
if collections:
47+
for coll_name in sorted(collections):
48+
count = connection.db[coll_name].count_documents({})
49+
print(f" • {coll_name}: {count:,} documents")
50+
else:
51+
print(" (No collections found)")
52+
53+
return True
54+
55+
except Exception as e:
56+
logger.error(f"❌ Error: {e}")
57+
return False
58+
finally:
59+
connection.disconnect()
60+
61+
62+
def parse_arguments():
63+
parser = argparse.ArgumentParser(description='Test MongoDB connection')
64+
parser.add_argument('--mongo-uri', help='MongoDB URI (overrides env)')
65+
parser.add_argument('--database', default='memberservice', help='Database to test (default: memberservice)')
66+
return parser.parse_args()
67+
68+
69+
def main():
70+
args = parse_arguments()
71+
72+
config = Config()
73+
mongo_uri = args.mongo_uri or config.mongo_uri
74+
database = args.database
75+
76+
print("="*80)
77+
print("MongoDB Connection Test")
78+
print("="*80)
79+
print(f"URI: {mongo_uri[:30]}..." if len(mongo_uri) > 30 else f"URI: {mongo_uri}")
80+
print(f"Database: {database}")
81+
82+
success = test_database_connection(mongo_uri, database)
83+
84+
print("\n" + "="*80)
85+
if success:
86+
print("✅ Connection test passed")
87+
return 0
88+
else:
89+
print("❌ Connection test failed")
90+
return 1
91+
92+
93+
if __name__ == '__main__':
94+
sys.exit(main())

0 commit comments

Comments
 (0)