black shift

This commit is contained in:
2025-04-22 11:10:29 +03:00
parent d7f1da8de8
commit e5f88f2eb4
30 changed files with 671 additions and 521 deletions

View File

@@ -63,7 +63,7 @@ class MongoDBHandler:
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"
@@ -76,7 +76,7 @@ class MongoDBHandler:
# 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,
@@ -132,11 +132,13 @@ class CollectionContext:
# 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)
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
@@ -148,7 +150,7 @@ class CollectionContext:
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
@@ -159,13 +161,17 @@ class CollectionContext:
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.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:
@@ -181,54 +187,56 @@ class CollectionContext:
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")
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'):
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)}"
if "_id" not in document:
document["_id"] = f"mock_id_{len(mock_data)}"
mock_data.append(document)
result = MagicMock()
result.inserted_id = document['_id']
result.inserted_id = document["_id"]
return result
def mock_insert_many(documents, *args, **kwargs):
inserted_ids = []
for doc in documents:
@@ -237,40 +245,40 @@ class CollectionContext:
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
@@ -283,10 +291,10 @@ class CollectionContext:
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
@@ -322,17 +330,25 @@ class CollectionContext:
"""
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'
"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):
@@ -340,7 +356,7 @@ class CollectionContext:
setattr(
mock_collection,
method_name,
retry_operation(max_retries=1, retry_interval=0)(original_method)
retry_operation(max_retries=1, retry_interval=0)(original_method),
)
def __exit__(self, exc_type, exc_val, exc_tb):

View File

@@ -22,7 +22,7 @@ def test_basic_crud_operations():
# First, clear any existing data
users_collection.delete_many({})
print("Cleared existing data")
# Insert multiple documents
insert_result = users_collection.insert_many(
[
@@ -58,7 +58,7 @@ def test_basic_crud_operations():
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}")
@@ -80,7 +80,7 @@ def test_nested_documents():
# Clear any existing data
products_collection.delete_many({})
print("Cleared existing data")
# Insert a product with nested data
insert_result = products_collection.insert_one(
{
@@ -110,15 +110,17 @@ def test_nested_documents():
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:
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"
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"
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}")
@@ -140,7 +142,7 @@ def test_array_operations():
# 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(
{
@@ -170,10 +172,12 @@ def test_array_operations():
# 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', [])
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}")
@@ -181,9 +185,13 @@ def test_array_operations():
# 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"
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}")
@@ -205,7 +213,7 @@ def test_aggregation():
# Clear any existing data
sales_collection.delete_many({})
print("Cleared existing data")
# Insert sample sales data
insert_result = sales_collection.insert_many(
[
@@ -219,13 +227,13 @@ def test_aggregation():
# Calculate total sales by product - use a simpler aggregation pipeline
pipeline = [
{"$match": {}}, # Match all documents
{"$group": {"_id": "$product", "total": {"$sum": "$amount"}}}
{"$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')}")
@@ -233,7 +241,8 @@ def test_aggregation():
# 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
item.get("_id") == "Laptop"
and abs(item.get("total", 0) - 999.99) < 0.01
for item in sales_summary
)
condition3 = any(
@@ -241,10 +250,11 @@ def test_aggregation():
for item in sales_summary
)
condition4 = any(
item.get("_id") == "Keyboard" and abs(item.get("total", 0) - 59.99) < 0.01
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}")
@@ -325,35 +335,37 @@ def test_complex_queries():
# 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}}
{"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}}
{"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"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 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
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
@@ -368,48 +380,51 @@ def run_concurrent_operation_test(num_threads=100):
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()
})
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}}
{"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)
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:
@@ -421,15 +436,15 @@ def run_concurrent_operation_test(num_threads=100):
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}")
@@ -437,14 +452,16 @@ def run_concurrent_operation_test(num_threads=100):
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
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
@@ -493,10 +510,10 @@ def run_all_tests():
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

@@ -1,14 +1,16 @@
"""
Test script for MongoDB handler with a local MongoDB instance.
"""
import os
from Controllers.Mongo.database import MongoDBHandler, CollectionContext
from datetime import datetime
# 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
@@ -22,6 +24,7 @@ class LocalMongoDBHandler(MongoDBHandler):
}
self._initialized = True
# Create a custom handler for local testing
def create_local_handler():
"""Create a MongoDB handler for local testing."""
@@ -29,35 +32,36 @@ def create_local_handler():
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):
@@ -72,18 +76,19 @@ def test_connection_monitoring():
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()