This repository was archived by the owner on Jun 15, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_mongodb_integration.py
More file actions
217 lines (179 loc) · 8 KB
/
Copy pathtest_mongodb_integration.py
File metadata and controls
217 lines (179 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python3
"""
MongoDB Integration Test Script for IQToolkit Analyzer
This script tests the complete MongoDB integration functionality:
- MongoDB connection and profiler setup
- Query pattern analysis and normalization
- Report generation in multiple formats
- Configuration loading and validation
Usage:
python test_mongodb_integration.py [--connection-string CONN_STR]
Requirements:
- MongoDB server running (default: mongodb://localhost:27017)
- PyMongo installed (poetry add pymongo)
"""
import argparse
import sys
import tempfile
from pathlib import Path
from iqtoolkit_analyzer import __version__
def test_mongodb_integration(
connection_string: str = "mongodb://localhost:27017",
) -> bool:
"""Test complete MongoDB integration functionality."""
print("🍃 MongoDB IQToolkit Analyzer Integration Test")
print("=" * 50)
try:
# Test 1: Import MongoDB modules
print("📦 Testing module imports...")
from iqtoolkit_analyzer.mongodb_analyzer import (
MongoDBProfilerIntegration,
MongoDBQueryPatternRecognizer,
)
from iqtoolkit_analyzer.mongodb_config import (
MongoDBConfig,
MongoDBThresholdConfig,
)
from iqtoolkit_analyzer.mongodb_report_generator import MongoDBReportGenerator
print("✅ All MongoDB modules imported successfully")
# Test 2: Configuration system
print("\n⚙️ Testing configuration system...")
config = MongoDBConfig()
if config.connection is not None:
config.connection.connection_string = connection_string
thresholds = config.thresholds or MongoDBThresholdConfig()
print(f"✅ Configuration created - threshold: {thresholds.slow_threshold_ms}ms")
# Test 3: MongoDB connection
print(f"\n🔌 Testing MongoDB connection to {connection_string}...")
profiler = MongoDBProfilerIntegration(connection_string, thresholds)
if profiler.connect():
print("✅ MongoDB connection successful")
# Test 4: Query pattern recognition
print("\n🔍 Testing query pattern recognition...")
recognizer = MongoDBQueryPatternRecognizer()
# Test query normalization
test_query = {"find": "users", "filter": {"name": "John", "age": 25}}
normalized = recognizer.normalize_query(test_query)
print(f"✅ Query normalization working - pattern: {normalized}")
# Test operation categorization
op_type = recognizer.categorize_operation(test_query)
print(f"✅ Operation categorization working - type: {op_type}")
# Test 5: Report generation system
print("\n📊 Testing report generation...")
# Create sample slow query data
from datetime import datetime
from iqtoolkit_analyzer.mongodb_analyzer import MongoDBSlowQuery
sample_query = MongoDBSlowQuery(
command=test_query,
collection="test_collection",
database="test_db",
operation_type="find",
duration_ms=1500.0,
timestamp=datetime.now(),
query_shape=normalized,
examined_docs=10000,
returned_docs=100,
)
# Test report generator initialization
try:
report_generator = MongoDBReportGenerator(config)
# Test that the generator has expected attributes
assert hasattr(report_generator, "config"), (
"Report generator missing config attribute"
)
print("✅ Report generator initialization working")
except Exception as e:
print(f"❌ Report generator initialization failed: {e}")
return False
# Create a temporary directory for test reports
with tempfile.TemporaryDirectory() as temp_dir:
# Test JSON report generation
json_file = Path(temp_dir) / "test_report.json"
report_data = {
"metadata": {
"generated_at": datetime.now().isoformat(),
"database_name": "test_db",
"tool_version": __version__,
},
"slow_queries": [sample_query.__dict__],
"executive_summary": {
"total_slow_query_patterns": 1,
"average_query_duration_ms": 1500.0,
},
}
# Test JSON generation
try:
import json
with open(json_file, "w") as f:
json.dump(report_data, f, indent=2, default=str)
print("✅ JSON report generation working")
except Exception as e:
print(f"❌ JSON report generation failed: {e}")
return False
# Test Markdown report generation (basic)
md_file = Path(temp_dir) / "test_report.md"
try:
with open(md_file, "w") as f:
f.write("# MongoDB Slow Query Analysis Report\n\n")
f.write("- **Total Slow Queries**: 1\n")
f.write("- **Average Duration**: 1500.0ms\n\n")
f.write("## Sample Query\n\n")
f.write(f"- **Collection**: {sample_query.collection}\n")
f.write(f"- **Duration**: {sample_query.duration_ms}ms\n")
print("✅ Markdown report generation working")
except Exception as e:
print(f"❌ Markdown report generation failed: {e}")
return False
print("\n🎉 All integration tests passed!")
print("\n📋 Integration Test Summary:")
print("- ✅ Module imports successful")
print("- ✅ Configuration system working")
print("- ✅ MongoDB connection established")
print("- ✅ Query pattern recognition functional")
print("- ✅ Report generation system operational")
return True
else:
print("❌ MongoDB connection failed")
print("💡 Suggestions:")
print(" - Ensure MongoDB is running")
print(" - Check connection string format")
print(" - Verify network connectivity")
print(" - Check authentication credentials")
return False
except ImportError as e:
print(f"❌ Import error: {e}")
print("💡 Suggestion: Install PyMongo with: poetry add pymongo")
return False
except Exception as e:
print(f"❌ Integration test failed: {e}")
return False
def main():
"""Main entry point for integration test."""
parser = argparse.ArgumentParser(
description="Test MongoDB integration for IQToolkit Analyzer"
)
parser.add_argument(
"--connection-string",
default="mongodb://localhost:27017",
help="MongoDB connection string (default: mongodb://localhost:27017)",
)
args = parser.parse_args()
print("MongoDB IQToolkit Analyzer Integration Test")
print(f"Testing connection to: {args.connection_string}")
print()
success = test_mongodb_integration(args.connection_string)
if success:
print("\n🎉 MongoDB integration is ready for production use!")
print("\n📖 Next steps:")
print("1. Copy .mongodb-config.yml.example to .mongodb-config.yml")
print("2. Customize configuration for your environment")
print(
"3. Run: python -m iqtoolkit_analyzer mongodb --config .mongodb-config.yml"
)
sys.exit(0)
else:
print("\n❌ Integration test failed!")
print("Please check the error messages above and resolve any issues.")
sys.exit(1)
if __name__ == "__main__":
main()