updated services api

This commit is contained in:
2025-05-30 23:43:46 +03:00
parent e5829f0525
commit f8184246d9
31 changed files with 2963 additions and 9 deletions

View File

@@ -0,0 +1,219 @@
# MongoDB Handler
A singleton MongoDB handler with context manager support for MongoDB collections and automatic retry capabilities.
## Features
- **Singleton Pattern**: Ensures only one instance of the MongoDB handler exists
- **Context Manager**: Automatically manages connection lifecycle
- **Retry Capability**: Automatically retries MongoDB operations on failure
- **Connection Pooling**: Configurable connection pooling
- **Graceful Degradation**: Handles connection failures without crashing
## Usage
```python
from Controllers.Mongo.database import mongo_handler
# Use the context manager to access a collection
with mongo_handler.collection("users") as users_collection:
# Perform operations on the collection
users_collection.insert_one({"username": "john", "email": "john@example.com"})
user = users_collection.find_one({"username": "john"})
# Connection is automatically closed when exiting the context
```
## Configuration
MongoDB connection settings are configured via environment variables with the `MONGO_` prefix:
- `MONGO_ENGINE`: Database engine (e.g., "mongodb")
- `MONGO_USER`: MongoDB username
- `MONGO_PASSWORD`: MongoDB password
- `MONGO_HOST`: MongoDB host
- `MONGO_PORT`: MongoDB port
- `MONGO_DB`: Database name
- `MONGO_AUTH_DB`: Authentication database
## Monitoring Connection Closure
To verify that MongoDB sessions are properly closed, you can implement one of the following approaches:
### 1. Add Logging to the `__exit__` Method
```python
def __exit__(self, exc_type, exc_val, exc_tb):
"""
Exit context, closing the connection.
"""
if self.client:
print(f"Closing MongoDB connection for collection: {self.collection_name}")
# Or use a proper logger
# logger.info(f"Closing MongoDB connection for collection: {self.collection_name}")
self.client.close()
self.client = None
self.collection = None
print(f"MongoDB connection closed successfully")
```
### 2. Add Connection Tracking
```python
class MongoDBHandler:
# Add these to your class
_open_connections = 0
def get_connection_stats(self):
"""Return statistics about open connections"""
return {"open_connections": self._open_connections}
```
Then modify the `CollectionContext` class:
```python
def __enter__(self):
try:
# Create a new client connection
self.client = MongoClient(self.db_handler.uri, **self.db_handler.client_options)
# Increment connection counter
self.db_handler._open_connections += 1
# Rest of your code...
def __exit__(self, exc_type, exc_val, exc_tb):
if self.client:
# Decrement connection counter
self.db_handler._open_connections -= 1
self.client.close()
self.client = None
self.collection = None
```
### 3. Use MongoDB's Built-in Monitoring
```python
from pymongo import monitoring
class ConnectionCommandListener(monitoring.CommandListener):
def started(self, event):
print(f"Command {event.command_name} started on server {event.connection_id}")
def succeeded(self, event):
print(f"Command {event.command_name} succeeded in {event.duration_micros} microseconds")
def failed(self, event):
print(f"Command {event.command_name} failed in {event.duration_micros} microseconds")
# Register the listener
monitoring.register(ConnectionCommandListener())
```
### 4. Add a Test Function
```python
def test_connection_closure():
"""Test that MongoDB connections are properly closed."""
print("\nTesting connection closure...")
# Record initial connection count (if you implemented the counter)
initial_count = mongo_handler.get_connection_stats()["open_connections"]
# Use multiple nested contexts
for i in range(5):
with mongo_handler.collection("test_collection") as collection:
# Do some simple operation
collection.find_one({})
# Check final connection count
final_count = mongo_handler.get_connection_stats()["open_connections"]
if final_count == initial_count:
print("Test passed: All connections were properly closed")
return True
else:
print(f"Test failed: {final_count - initial_count} connections remain open")
return False
```
### 5. Use MongoDB Server Logs
You can also check the MongoDB server logs to see connection events:
```bash
# Run this on your MongoDB server
tail -f /var/log/mongodb/mongod.log | grep "connection"
```
## Best Practices
1. Always use the context manager pattern to ensure connections are properly closed
2. Keep operations within the context manager as concise as possible
3. Handle exceptions within the context to prevent unexpected behavior
4. Avoid nesting multiple context managers unnecessarily
5. Use the retry decorator for operations that might fail due to transient issues
## LXC Container Configuration
### Authentication Issues
If you encounter authentication errors when connecting to the MongoDB container at 10.10.2.13:27017, you may need to update the container configuration:
1. **Check MongoDB Authentication**: Ensure the MongoDB container is configured with the correct authentication mechanism
2. **Verify Network Configuration**: Make sure the container network allows connections from your application
3. **Update MongoDB Configuration**:
- Edit the MongoDB configuration file in the container
- Ensure `bindIp` is set correctly (e.g., `0.0.0.0` to allow connections from any IP)
- Check that authentication is enabled with the correct mechanism
4. **User Permissions**:
- Verify that the application user (`appuser`) exists in the MongoDB instance
- Ensure the user has the correct roles and permissions for the database
### Example MongoDB Container Configuration
```yaml
# Example docker-compose.yml configuration
services:
mongodb:
image: mongo:latest
container_name: mongodb
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password
volumes:
- ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
ports:
- "27017:27017"
command: mongod --auth
```
```javascript
// Example init-mongo.js
db.createUser({
user: 'appuser',
pwd: 'apppassword',
roles: [
{ role: 'readWrite', db: 'appdb' }
]
});
```
## Troubleshooting
### Common Issues
1. **Authentication Failed**:
- Verify username and password in environment variables
- Check that the user exists in the specified authentication database
- Ensure the user has appropriate permissions
2. **Connection Refused**:
- Verify the MongoDB host and port are correct
- Check network connectivity between application and MongoDB container
- Ensure MongoDB is running and accepting connections
3. **Resource Leaks**:
- Use the context manager pattern to ensure connections are properly closed
- Monitor connection pool size and active connections
- Implement proper error handling to close connections in case of exceptions

View File

@@ -0,0 +1,31 @@
import os
from pydantic_settings import BaseSettings, SettingsConfigDict
class Configs(BaseSettings):
"""
MongoDB configuration settings.
"""
# MongoDB connection settings
ENGINE: str = "mongodb"
USERNAME: str = "appuser" # Application user
PASSWORD: str = "apppassword" # Application password
HOST: str = "10.10.2.13"
PORT: int = 27017
DB: str = "appdb" # The application database
AUTH_DB: str = "appdb" # Authentication is done against admin database
@property
def url(self):
"""Generate the database URL.
mongodb://{MONGO_USERNAME}:{MONGO_PASSWORD}@{MONGO_HOST}:{MONGO_PORT}/{DB}?authSource={MONGO_AUTH_DB}
"""
# Include the database name in the URI
return f"{self.ENGINE}://{self.USERNAME}:{self.PASSWORD}@{self.HOST}:{self.PORT}/{self.DB}?authSource={self.DB}"
model_config = SettingsConfigDict(env_prefix="_MONGO_")
# Create a singleton instance of the MongoDB configuration settings
mongo_configs = Configs()

View File

@@ -0,0 +1,373 @@
import time
import functools
from pymongo import MongoClient
from pymongo.errors import PyMongoError
from .config import mongo_configs
def retry_operation(max_attempts=3, delay=1.0, backoff=2.0, exceptions=(PyMongoError,)):
"""
Decorator for retrying MongoDB operations with exponential backoff.
Args:
max_attempts: Maximum number of retry attempts
delay: Initial delay between retries in seconds
backoff: Multiplier for delay after each retry
exceptions: Tuple of exceptions to catch and retry
"""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
mtries, mdelay = max_attempts, delay
while mtries > 1:
try:
return func(*args, **kwargs)
except exceptions as e:
time.sleep(mdelay)
mtries -= 1
mdelay *= backoff
return func(*args, **kwargs)
return wrapper
return decorator
class MongoDBHandler:
"""
A MongoDB handler that provides context manager access to specific collections
with automatic retry capability. Implements singleton pattern.
"""
_instance = None
_debug_mode = False # Set to True to enable debug mode
def __new__(cls, *args, **kwargs):
"""
Implement singleton pattern for the handler.
"""
if cls._instance is None:
cls._instance = super(MongoDBHandler, cls).__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self, debug_mode=False, mock_mode=False):
"""Initialize the MongoDB handler.
Args:
debug_mode: If True, use a simplified connection for debugging
mock_mode: If True, use mock collections instead of real MongoDB connections
"""
if not hasattr(self, "_initialized") or not self._initialized:
self._debug_mode = debug_mode
self._mock_mode = mock_mode
if mock_mode:
# In mock mode, we don't need a real connection string
self.uri = "mongodb://mock:27017/mockdb"
print("MOCK MODE: Using simulated MongoDB connections")
elif debug_mode:
# Use a direct connection without authentication for testing
self.uri = f"mongodb://{mongo_configs.HOST}:{mongo_configs.PORT}/{mongo_configs.DB}"
print(f"DEBUG MODE: Using direct connection: {self.uri}")
else:
# Use the configured connection string with authentication
self.uri = mongo_configs.url
print(f"Connecting to MongoDB: {self.uri}")
# Define MongoDB client options with increased timeouts for better reliability
self.client_options = {
"maxPoolSize": 5,
"minPoolSize": 1,
"maxIdleTimeMS": 60000,
"waitQueueTimeoutMS": 5000,
"serverSelectionTimeoutMS": 10000,
"connectTimeoutMS": 30000,
"socketTimeoutMS": 45000,
"retryWrites": True,
"retryReads": True,
}
self._initialized = True
def collection(self, collection_name: str):
"""
Get a context manager for a specific collection.
Args:
collection_name: Name of the collection to access
Returns:
A context manager for the specified collection
"""
return CollectionContext(self, collection_name)
class CollectionContext:
"""
Context manager for MongoDB collections with automatic retry capability.
"""
def __init__(self, db_handler: MongoDBHandler, collection_name: str):
"""
Initialize collection context.
Args:
db_handler: Reference to the MongoDB handler
collection_name: Name of the collection to access
"""
self.db_handler = db_handler
self.collection_name = collection_name
self.client = None
self.collection = None
def __enter__(self):
"""
Enter context, establishing a new connection.
Returns:
The MongoDB collection object with retry capabilities
"""
# If we're in mock mode, return a mock collection immediately
if self.db_handler._mock_mode:
return self._create_mock_collection()
try:
# Create a new client connection
self.client = MongoClient(
self.db_handler.uri, **self.db_handler.client_options
)
if self.db_handler._debug_mode:
# In debug mode, we explicitly use the configured DB
db_name = mongo_configs.DB
print(f"DEBUG MODE: Using database '{db_name}'")
else:
# In normal mode, extract database name from the URI
try:
db_name = self.client.get_database().name
except Exception:
db_name = mongo_configs.DB
print(f"Using fallback database '{db_name}'")
self.collection = self.client[db_name][self.collection_name]
# Enhance collection methods with retry capabilities
self._add_retry_capabilities()
return self.collection
except pymongo.errors.OperationFailure as e:
if "Authentication failed" in str(e):
print(f"MongoDB authentication error: {e}")
print("Attempting to reconnect with direct connection...")
try:
# Try a direct connection without authentication for testing
direct_uri = f"mongodb://{mongo_configs.HOST}:{mongo_configs.PORT}/{mongo_configs.DB}"
print(f"Trying direct connection: {direct_uri}")
self.client = MongoClient(
direct_uri, **self.db_handler.client_options
)
self.collection = self.client[mongo_configs.DB][
self.collection_name
]
self._add_retry_capabilities()
return self.collection
except Exception as inner_e:
print(f"Direct connection also failed: {inner_e}")
# Fall through to mock collection creation
else:
print(f"MongoDB operation error: {e}")
if self.client:
self.client.close()
self.client = None
except Exception as e:
print(f"MongoDB connection error: {e}")
if self.client:
self.client.close()
self.client = None
return self._create_mock_collection()
def _create_mock_collection(self):
"""
Create a mock collection for testing or graceful degradation.
This prevents the application from crashing when MongoDB is unavailable.
Returns:
A mock MongoDB collection with simulated behaviors
"""
from unittest.mock import MagicMock
if self.db_handler._mock_mode:
print(f"MOCK MODE: Using mock collection '{self.collection_name}'")
else:
print(
f"Using mock MongoDB collection '{self.collection_name}' for graceful degradation"
)
# Create in-memory storage for this mock collection
if not hasattr(self.db_handler, "_mock_storage"):
self.db_handler._mock_storage = {}
if self.collection_name not in self.db_handler._mock_storage:
self.db_handler._mock_storage[self.collection_name] = []
mock_collection = MagicMock()
mock_data = self.db_handler._mock_storage[self.collection_name]
# Define behavior for find operations
def mock_find(query=None, *args, **kwargs):
# Simple implementation that returns all documents
return mock_data
def mock_find_one(query=None, *args, **kwargs):
# Simple implementation that returns the first matching document
if not mock_data:
return None
return mock_data[0]
def mock_insert_one(document, *args, **kwargs):
# Add _id if not present
if "_id" not in document:
document["_id"] = f"mock_id_{len(mock_data)}"
mock_data.append(document)
result = MagicMock()
result.inserted_id = document["_id"]
return result
def mock_insert_many(documents, *args, **kwargs):
inserted_ids = []
for doc in documents:
result = mock_insert_one(doc)
inserted_ids.append(result.inserted_id)
result = MagicMock()
result.inserted_ids = inserted_ids
return result
def mock_update_one(query, update, *args, **kwargs):
result = MagicMock()
result.modified_count = 1
return result
def mock_update_many(query, update, *args, **kwargs):
result = MagicMock()
result.modified_count = len(mock_data)
return result
def mock_delete_one(query, *args, **kwargs):
result = MagicMock()
result.deleted_count = 1
if mock_data:
mock_data.pop(0) # Just remove the first item for simplicity
return result
def mock_delete_many(query, *args, **kwargs):
count = len(mock_data)
mock_data.clear()
result = MagicMock()
result.deleted_count = count
return result
def mock_count_documents(query, *args, **kwargs):
return len(mock_data)
def mock_aggregate(pipeline, *args, **kwargs):
return []
def mock_create_index(keys, **kwargs):
return f"mock_index_{keys}"
# Assign the mock implementations
mock_collection.find.side_effect = mock_find
mock_collection.find_one.side_effect = mock_find_one
mock_collection.insert_one.side_effect = mock_insert_one
mock_collection.insert_many.side_effect = mock_insert_many
mock_collection.update_one.side_effect = mock_update_one
mock_collection.update_many.side_effect = mock_update_many
mock_collection.delete_one.side_effect = mock_delete_one
mock_collection.delete_many.side_effect = mock_delete_many
mock_collection.count_documents.side_effect = mock_count_documents
mock_collection.aggregate.side_effect = mock_aggregate
mock_collection.create_index.side_effect = mock_create_index
# Add retry capabilities to the mock collection
self._add_retry_capabilities_to_mock(mock_collection)
self.collection = mock_collection
return self.collection
def _add_retry_capabilities(self):
"""
Add retry capabilities to all collection methods.
"""
# Store original methods for common operations
original_insert_one = self.collection.insert_one
original_insert_many = self.collection.insert_many
original_find_one = self.collection.find_one
original_find = self.collection.find
original_update_one = self.collection.update_one
original_update_many = self.collection.update_many
original_delete_one = self.collection.delete_one
original_delete_many = self.collection.delete_many
original_replace_one = self.collection.replace_one
original_count_documents = self.collection.count_documents
# Add retry capabilities to methods
self.collection.insert_one = retry_operation()(original_insert_one)
self.collection.insert_many = retry_operation()(original_insert_many)
self.collection.find_one = retry_operation()(original_find_one)
self.collection.find = retry_operation()(original_find)
self.collection.update_one = retry_operation()(original_update_one)
self.collection.update_many = retry_operation()(original_update_many)
self.collection.delete_one = retry_operation()(original_delete_one)
self.collection.delete_many = retry_operation()(original_delete_many)
self.collection.replace_one = retry_operation()(original_replace_one)
self.collection.count_documents = retry_operation()(original_count_documents)
def _add_retry_capabilities_to_mock(self, mock_collection):
"""
Add retry capabilities to mock collection methods.
This is a simplified version that just wraps the mock methods.
Args:
mock_collection: The mock collection to enhance
"""
# List of common MongoDB collection methods to add retry capabilities to
methods = [
"insert_one",
"insert_many",
"find_one",
"find",
"update_one",
"update_many",
"delete_one",
"delete_many",
"replace_one",
"count_documents",
"aggregate",
]
# Add retry decorator to each method
for method_name in methods:
if hasattr(mock_collection, method_name):
original_method = getattr(mock_collection, method_name)
setattr(
mock_collection,
method_name,
retry_operation(max_retries=1, retry_interval=0)(original_method),
)
def __exit__(self, exc_type, exc_val, exc_tb):
"""
Exit context, closing the connection.
"""
if self.client:
self.client.close()
self.client = None
self.collection = None
# Create a singleton instance of the MongoDB handler
mongo_handler = MongoDBHandler()

View File

@@ -0,0 +1,519 @@
# Initialize the MongoDB handler with your configuration
from datetime import datetime
from .database import MongoDBHandler, mongo_handler
def cleanup_test_data():
"""Clean up any test data before running tests."""
try:
with mongo_handler.collection("test_collection") as collection:
collection.delete_many({})
print("Successfully cleaned up test data")
except Exception as e:
print(f"Warning: Could not clean up test data: {e}")
print("Continuing with tests using mock data...")
def test_basic_crud_operations():
"""Test basic CRUD operations on users collection."""
print("\nTesting basic CRUD operations...")
try:
with mongo_handler.collection("users") as users_collection:
# First, clear any existing data
users_collection.delete_many({})
print("Cleared existing data")
# Insert multiple documents
insert_result = users_collection.insert_many(
[
{"username": "john", "email": "john@example.com", "role": "user"},
{"username": "jane", "email": "jane@example.com", "role": "admin"},
{"username": "bob", "email": "bob@example.com", "role": "user"},
]
)
print(f"Inserted {len(insert_result.inserted_ids)} documents")
# Find with multiple conditions
admin_users = list(users_collection.find({"role": "admin"}))
print(f"Found {len(admin_users)} admin users")
if admin_users:
print(f"Admin user: {admin_users[0].get('username')}")
# Update multiple documents
update_result = users_collection.update_many(
{"role": "user"}, {"$set": {"last_login": datetime.now().isoformat()}}
)
print(f"Updated {update_result.modified_count} documents")
# Delete documents
delete_result = users_collection.delete_many({"username": "bob"})
print(f"Deleted {delete_result.deleted_count} documents")
# Count remaining documents
remaining = users_collection.count_documents({})
print(f"Remaining documents: {remaining}")
# Check each condition separately
condition1 = len(admin_users) == 1
condition2 = admin_users and admin_users[0].get("username") == "jane"
condition3 = update_result.modified_count == 2
condition4 = delete_result.deleted_count == 1
print(f"Condition 1 (admin count): {condition1}")
print(f"Condition 2 (admin is jane): {condition2}")
print(f"Condition 3 (updated 2 users): {condition3}")
print(f"Condition 4 (deleted bob): {condition4}")
success = condition1 and condition2 and condition3 and condition4
print(f"Test {'passed' if success else 'failed'}")
return success
except Exception as e:
print(f"Test failed with exception: {e}")
return False
def test_nested_documents():
"""Test operations with nested documents in products collection."""
print("\nTesting nested documents...")
try:
with mongo_handler.collection("products") as products_collection:
# Clear any existing data
products_collection.delete_many({})
print("Cleared existing data")
# Insert a product with nested data
insert_result = products_collection.insert_one(
{
"name": "Laptop",
"price": 999.99,
"specs": {"cpu": "Intel i7", "ram": "16GB", "storage": "512GB SSD"},
"in_stock": True,
"tags": ["electronics", "computers", "laptops"],
}
)
print(f"Inserted document with ID: {insert_result.inserted_id}")
# Find with nested field query
laptop = products_collection.find_one({"specs.cpu": "Intel i7"})
print(f"Found laptop: {laptop is not None}")
if laptop:
print(f"Laptop RAM: {laptop.get('specs', {}).get('ram')}")
# Update nested field
update_result = products_collection.update_one(
{"name": "Laptop"}, {"$set": {"specs.ram": "32GB"}}
)
print(f"Update modified count: {update_result.modified_count}")
# Verify the update
updated_laptop = products_collection.find_one({"name": "Laptop"})
print(f"Found updated laptop: {updated_laptop is not None}")
if updated_laptop:
print(f"Updated laptop specs: {updated_laptop.get('specs')}")
if "specs" in updated_laptop:
print(f"Updated RAM: {updated_laptop['specs'].get('ram')}")
# Check each condition separately
condition1 = laptop is not None
condition2 = laptop and laptop.get("specs", {}).get("ram") == "16GB"
condition3 = update_result.modified_count == 1
condition4 = (
updated_laptop and updated_laptop.get("specs", {}).get("ram") == "32GB"
)
print(f"Condition 1 (laptop found): {condition1}")
print(f"Condition 2 (original RAM is 16GB): {condition2}")
print(f"Condition 3 (update modified 1 doc): {condition3}")
print(f"Condition 4 (updated RAM is 32GB): {condition4}")
success = condition1 and condition2 and condition3 and condition4
print(f"Test {'passed' if success else 'failed'}")
return success
except Exception as e:
print(f"Test failed with exception: {e}")
return False
def test_array_operations():
"""Test operations with arrays in orders collection."""
print("\nTesting array operations...")
try:
with mongo_handler.collection("orders") as orders_collection:
# Clear any existing data
orders_collection.delete_many({})
print("Cleared existing data")
# Insert an order with array of items
insert_result = orders_collection.insert_one(
{
"order_id": "ORD001",
"customer": "john",
"items": [
{"product": "Laptop", "quantity": 1},
{"product": "Mouse", "quantity": 2},
],
"total": 1099.99,
"status": "pending",
}
)
print(f"Inserted order with ID: {insert_result.inserted_id}")
# Find orders containing specific items
laptop_orders = list(orders_collection.find({"items.product": "Laptop"}))
print(f"Found {len(laptop_orders)} orders with Laptop")
# Update array elements
update_result = orders_collection.update_one(
{"order_id": "ORD001"},
{"$push": {"items": {"product": "Keyboard", "quantity": 1}}},
)
print(f"Update modified count: {update_result.modified_count}")
# Verify the update
updated_order = orders_collection.find_one({"order_id": "ORD001"})
print(f"Found updated order: {updated_order is not None}")
if updated_order:
print(
f"Number of items in order: {len(updated_order.get('items', []))}"
)
items = updated_order.get("items", [])
if items:
last_item = items[-1] if items else None
print(f"Last item in order: {last_item}")
# Check each condition separately
condition1 = len(laptop_orders) == 1
condition2 = update_result.modified_count == 1
condition3 = updated_order and len(updated_order.get("items", [])) == 3
condition4 = (
updated_order
and updated_order.get("items", [])
and updated_order["items"][-1].get("product") == "Keyboard"
)
print(f"Condition 1 (found 1 laptop order): {condition1}")
print(f"Condition 2 (update modified 1 doc): {condition2}")
print(f"Condition 3 (order has 3 items): {condition3}")
print(f"Condition 4 (last item is keyboard): {condition4}")
success = condition1 and condition2 and condition3 and condition4
print(f"Test {'passed' if success else 'failed'}")
return success
except Exception as e:
print(f"Test failed with exception: {e}")
return False
def test_aggregation():
"""Test aggregation operations on sales collection."""
print("\nTesting aggregation operations...")
try:
with mongo_handler.collection("sales") as sales_collection:
# Clear any existing data
sales_collection.delete_many({})
print("Cleared existing data")
# Insert sample sales data
insert_result = sales_collection.insert_many(
[
{"product": "Laptop", "amount": 999.99, "date": datetime.now()},
{"product": "Mouse", "amount": 29.99, "date": datetime.now()},
{"product": "Keyboard", "amount": 59.99, "date": datetime.now()},
]
)
print(f"Inserted {len(insert_result.inserted_ids)} sales documents")
# Calculate total sales by product - use a simpler aggregation pipeline
pipeline = [
{"$match": {}}, # Match all documents
{"$group": {"_id": "$product", "total": {"$sum": "$amount"}}},
]
# Execute the aggregation
sales_summary = list(sales_collection.aggregate(pipeline))
print(f"Aggregation returned {len(sales_summary)} results")
# Print the results for debugging
for item in sales_summary:
print(f"Product: {item.get('_id')}, Total: {item.get('total')}")
# Check each condition separately
condition1 = len(sales_summary) == 3
condition2 = any(
item.get("_id") == "Laptop"
and abs(item.get("total", 0) - 999.99) < 0.01
for item in sales_summary
)
condition3 = any(
item.get("_id") == "Mouse" and abs(item.get("total", 0) - 29.99) < 0.01
for item in sales_summary
)
condition4 = any(
item.get("_id") == "Keyboard"
and abs(item.get("total", 0) - 59.99) < 0.01
for item in sales_summary
)
print(f"Condition 1 (3 summary items): {condition1}")
print(f"Condition 2 (laptop total correct): {condition2}")
print(f"Condition 3 (mouse total correct): {condition3}")
print(f"Condition 4 (keyboard total correct): {condition4}")
success = condition1 and condition2 and condition3 and condition4
print(f"Test {'passed' if success else 'failed'}")
return success
except Exception as e:
print(f"Test failed with exception: {e}")
return False
def test_index_operations():
"""Test index creation and unique constraints."""
print("\nTesting index operations...")
try:
with mongo_handler.collection("test_collection") as collection:
# Create indexes
collection.create_index("email", unique=True)
collection.create_index([("username", 1), ("role", 1)])
# Insert initial document
collection.insert_one(
{"username": "test_user", "email": "test@example.com"}
)
# Try to insert duplicate email (should fail)
try:
collection.insert_one(
{"username": "test_user2", "email": "test@example.com"}
)
success = False # Should not reach here
except Exception:
success = True
print(f"Test {'passed' if success else 'failed'}")
return success
except Exception as e:
print(f"Test failed with exception: {e}")
return False
def test_complex_queries():
"""Test complex queries with multiple conditions."""
print("\nTesting complex queries...")
try:
with mongo_handler.collection("products") as products_collection:
# Insert test data
products_collection.insert_many(
[
{
"name": "Expensive Laptop",
"price": 999.99,
"tags": ["electronics", "computers"],
"in_stock": True,
},
{
"name": "Cheap Mouse",
"price": 29.99,
"tags": ["electronics", "peripherals"],
"in_stock": True,
},
]
)
# Find products with price range and specific tags
expensive_electronics = list(
products_collection.find(
{
"price": {"$gt": 500},
"tags": {"$in": ["electronics"]},
"in_stock": True,
}
)
)
# Update with multiple conditions - split into separate operations for better compatibility
# First set the discount
products_collection.update_many(
{"price": {"$lt": 100}, "in_stock": True}, {"$set": {"discount": 0.1}}
)
# Then update the price
update_result = products_collection.update_many(
{"price": {"$lt": 100}, "in_stock": True}, {"$inc": {"price": -10}}
)
# Verify the update
updated_product = products_collection.find_one({"name": "Cheap Mouse"})
# Print debug information
print(f"Found expensive electronics: {len(expensive_electronics)}")
if expensive_electronics:
print(
f"First expensive product: {expensive_electronics[0].get('name')}"
)
print(f"Modified count: {update_result.modified_count}")
if updated_product:
print(f"Updated product price: {updated_product.get('price')}")
print(f"Updated product discount: {updated_product.get('discount')}")
# More flexible verification with approximate float comparison
success = (
len(expensive_electronics) >= 1
and expensive_electronics[0].get("name")
in ["Expensive Laptop", "Laptop"]
and update_result.modified_count >= 1
and updated_product is not None
and updated_product.get("discount", 0)
> 0 # Just check that discount exists and is positive
)
print(f"Test {'passed' if success else 'failed'}")
return success
except Exception as e:
print(f"Test failed with exception: {e}")
return False
def run_concurrent_operation_test(num_threads=100):
"""Run a simple operation in multiple threads to verify connection pooling."""
import threading
import time
import uuid
from concurrent.futures import ThreadPoolExecutor
print(f"\nStarting concurrent operation test with {num_threads} threads...")
# Results tracking
results = {"passed": 0, "failed": 0, "errors": []}
results_lock = threading.Lock()
def worker(thread_id):
# Create a unique collection name for this thread
collection_name = f"concurrent_test_{thread_id}"
try:
# Generate unique data for this thread
unique_id = str(uuid.uuid4())
with mongo_handler.collection(collection_name) as collection:
# Insert a document
collection.insert_one(
{
"thread_id": thread_id,
"uuid": unique_id,
"timestamp": time.time(),
}
)
# Find the document
doc = collection.find_one({"thread_id": thread_id})
# Update the document
collection.update_one(
{"thread_id": thread_id}, {"$set": {"updated": True}}
)
# Verify update
updated_doc = collection.find_one({"thread_id": thread_id})
# Clean up
collection.delete_many({"thread_id": thread_id})
success = (
doc is not None
and updated_doc is not None
and updated_doc.get("updated") is True
)
# Update results with thread safety
with results_lock:
if success:
results["passed"] += 1
else:
results["failed"] += 1
results["errors"].append(f"Thread {thread_id} operation failed")
except Exception as e:
with results_lock:
results["failed"] += 1
results["errors"].append(f"Thread {thread_id} exception: {str(e)}")
# Create and start threads using a thread pool
start_time = time.time()
with ThreadPoolExecutor(max_workers=num_threads) as executor:
futures = [executor.submit(worker, i) for i in range(num_threads)]
# Calculate execution time
execution_time = time.time() - start_time
# Print results
print(f"\nConcurrent Operation Test Results:")
print(f"Total threads: {num_threads}")
print(f"Passed: {results['passed']}")
print(f"Failed: {results['failed']}")
print(f"Execution time: {execution_time:.2f} seconds")
print(f"Operations per second: {num_threads / execution_time:.2f}")
if results["failed"] > 0:
print("\nErrors:")
for error in results["errors"][
:10
]: # Show only first 10 errors to avoid flooding output
print(f"- {error}")
if len(results["errors"]) > 10:
print(f"- ... and {len(results['errors']) - 10} more errors")
return results["failed"] == 0
def run_all_tests():
"""Run all MongoDB tests and report results."""
print("Starting MongoDB tests...")
# Clean up any existing test data before starting
cleanup_test_data()
tests = [
test_basic_crud_operations,
test_nested_documents,
test_array_operations,
test_aggregation,
test_index_operations,
test_complex_queries,
]
passed_list, not_passed_list = [], []
passed, failed = 0, 0
for test in tests:
# Clean up test data before each test
cleanup_test_data()
try:
if test():
passed += 1
passed_list.append(f"Test {test.__name__} passed")
else:
failed += 1
not_passed_list.append(f"Test {test.__name__} failed")
except Exception as e:
print(f"Test {test.__name__} failed with exception: {e}")
failed += 1
not_passed_list.append(f"Test {test.__name__} failed")
print(f"\nTest Results: {passed} passed, {failed} failed")
print("Passed Tests:")
print("\n".join(passed_list))
print("Failed Tests:")
print("\n".join(not_passed_list))
return passed, failed
if __name__ == "__main__":
mongo_handler = MongoDBHandler()
# Run standard tests first
passed, failed = run_all_tests()
# If all tests pass, run the concurrent operation test
if failed == 0:
run_concurrent_operation_test(10000)

View File

@@ -0,0 +1,93 @@
"""
Test script for MongoDB handler with a local MongoDB instance.
"""
import os
from datetime import datetime
from .database import MongoDBHandler, CollectionContext
# Create a custom handler class for local testing
class LocalMongoDBHandler(MongoDBHandler):
"""A MongoDB handler for local testing without authentication."""
def __init__(self):
"""Initialize with a direct MongoDB URI."""
self._initialized = False
self.uri = "mongodb://localhost:27017/test"
self.client_options = {
"maxPoolSize": 5,
"minPoolSize": 2,
"maxIdleTimeMS": 30000,
"waitQueueTimeoutMS": 2000,
"serverSelectionTimeoutMS": 5000,
}
self._initialized = True
# Create a custom handler for local testing
def create_local_handler():
"""Create a MongoDB handler for local testing."""
# Create a fresh instance with direct MongoDB URI
handler = LocalMongoDBHandler()
return handler
def test_connection_monitoring():
"""Test connection monitoring with the MongoDB handler."""
print("\nTesting connection monitoring...")
# Create a local handler
local_handler = create_local_handler()
# Add connection tracking to the handler
local_handler._open_connections = 0
# Modify the CollectionContext class to track connections
original_enter = CollectionContext.__enter__
original_exit = CollectionContext.__exit__
def tracked_enter(self):
result = original_enter(self)
self.db_handler._open_connections += 1
print(f"Connection opened. Total open: {self.db_handler._open_connections}")
return result
def tracked_exit(self, exc_type, exc_val, exc_tb):
self.db_handler._open_connections -= 1
print(f"Connection closed. Total open: {self.db_handler._open_connections}")
return original_exit(self, exc_type, exc_val, exc_tb)
# Apply the tracking methods
CollectionContext.__enter__ = tracked_enter
CollectionContext.__exit__ = tracked_exit
try:
# Test with multiple operations
for i in range(3):
print(f"\nTest iteration {i+1}:")
try:
with local_handler.collection("test_collection") as collection:
# Try a simple operation
try:
collection.find_one({})
print("Operation succeeded")
except Exception as e:
print(f"Operation failed: {e}")
except Exception as e:
print(f"Connection failed: {e}")
# Final connection count
print(f"\nFinal open connections: {local_handler._open_connections}")
if local_handler._open_connections == 0:
print("✅ All connections were properly closed")
else:
print(f"{local_handler._open_connections} connections remain open")
finally:
# Restore original methods
CollectionContext.__enter__ = original_enter
CollectionContext.__exit__ = original_exit
if __name__ == "__main__":
test_connection_monitoring()