auth endpoints added
This commit is contained in:
@@ -17,29 +17,30 @@ def test_basic_crud_operations():
|
||||
try:
|
||||
with mongo_handler.collection("users") as users_collection:
|
||||
# Insert multiple documents
|
||||
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"}
|
||||
])
|
||||
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"},
|
||||
]
|
||||
)
|
||||
|
||||
# Find with multiple conditions
|
||||
admin_users = list(users_collection.find({"role": "admin"}))
|
||||
|
||||
|
||||
# Update multiple documents
|
||||
update_result = users_collection.update_many(
|
||||
{"role": "user"},
|
||||
{"$set": {"last_login": datetime.now().isoformat()}}
|
||||
{"role": "user"}, {"$set": {"last_login": datetime.now().isoformat()}}
|
||||
)
|
||||
|
||||
# Delete documents
|
||||
delete_result = users_collection.delete_many({"username": "bob"})
|
||||
|
||||
success = (
|
||||
len(admin_users) == 1 and
|
||||
admin_users[0]["username"] == "jane" and
|
||||
update_result.modified_count == 2 and
|
||||
delete_result.deleted_count == 1
|
||||
len(admin_users) == 1
|
||||
and admin_users[0]["username"] == "jane"
|
||||
and update_result.modified_count == 2
|
||||
and delete_result.deleted_count == 1
|
||||
)
|
||||
print(f"Test {'passed' if success else 'failed'}")
|
||||
return success
|
||||
@@ -54,35 +55,32 @@ def test_nested_documents():
|
||||
try:
|
||||
with mongo_handler.collection("products") as products_collection:
|
||||
# Insert a product with nested data
|
||||
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"]
|
||||
})
|
||||
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"],
|
||||
}
|
||||
)
|
||||
|
||||
# Find with nested field query
|
||||
laptop = products_collection.find_one({"specs.cpu": "Intel i7"})
|
||||
|
||||
# Update nested field
|
||||
update_result = products_collection.update_one(
|
||||
{"name": "Laptop"},
|
||||
{"$set": {"specs.ram": "32GB"}}
|
||||
{"name": "Laptop"}, {"$set": {"specs.ram": "32GB"}}
|
||||
)
|
||||
|
||||
# Verify the update
|
||||
updated_laptop = products_collection.find_one({"name": "Laptop"})
|
||||
|
||||
success = (
|
||||
laptop is not None and
|
||||
laptop["specs"]["ram"] == "16GB" and
|
||||
update_result.modified_count == 1 and
|
||||
updated_laptop["specs"]["ram"] == "32GB"
|
||||
laptop is not None
|
||||
and laptop["specs"]["ram"] == "16GB"
|
||||
and update_result.modified_count == 1
|
||||
and updated_laptop["specs"]["ram"] == "32GB"
|
||||
)
|
||||
print(f"Test {'passed' if success else 'failed'}")
|
||||
return success
|
||||
@@ -97,16 +95,18 @@ def test_array_operations():
|
||||
try:
|
||||
with mongo_handler.collection("orders") as orders_collection:
|
||||
# Insert an order with array of items
|
||||
orders_collection.insert_one({
|
||||
"order_id": "ORD001",
|
||||
"customer": "john",
|
||||
"items": [
|
||||
{"product": "Laptop", "quantity": 1},
|
||||
{"product": "Mouse", "quantity": 2}
|
||||
],
|
||||
"total": 1099.99,
|
||||
"status": "pending"
|
||||
})
|
||||
orders_collection.insert_one(
|
||||
{
|
||||
"order_id": "ORD001",
|
||||
"customer": "john",
|
||||
"items": [
|
||||
{"product": "Laptop", "quantity": 1},
|
||||
{"product": "Mouse", "quantity": 2},
|
||||
],
|
||||
"total": 1099.99,
|
||||
"status": "pending",
|
||||
}
|
||||
)
|
||||
|
||||
# Find orders containing specific items
|
||||
laptop_orders = list(orders_collection.find({"items.product": "Laptop"}))
|
||||
@@ -114,17 +114,17 @@ def test_array_operations():
|
||||
# Update array elements
|
||||
update_result = orders_collection.update_one(
|
||||
{"order_id": "ORD001"},
|
||||
{"$push": {"items": {"product": "Keyboard", "quantity": 1}}}
|
||||
{"$push": {"items": {"product": "Keyboard", "quantity": 1}}},
|
||||
)
|
||||
|
||||
# Verify the update
|
||||
updated_order = orders_collection.find_one({"order_id": "ORD001"})
|
||||
|
||||
success = (
|
||||
len(laptop_orders) == 1 and
|
||||
update_result.modified_count == 1 and
|
||||
len(updated_order["items"]) == 3 and
|
||||
updated_order["items"][-1]["product"] == "Keyboard"
|
||||
len(laptop_orders) == 1
|
||||
and update_result.modified_count == 1
|
||||
and len(updated_order["items"]) == 3
|
||||
and updated_order["items"][-1]["product"] == "Keyboard"
|
||||
)
|
||||
print(f"Test {'passed' if success else 'failed'}")
|
||||
return success
|
||||
@@ -139,23 +139,32 @@ def test_aggregation():
|
||||
try:
|
||||
with mongo_handler.collection("sales") as sales_collection:
|
||||
# Insert sample sales data
|
||||
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()}
|
||||
])
|
||||
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()},
|
||||
]
|
||||
)
|
||||
|
||||
# Calculate total sales by product
|
||||
pipeline = [
|
||||
{"$group": {"_id": "$product", "total": {"$sum": "$amount"}}}
|
||||
]
|
||||
pipeline = [{"$group": {"_id": "$product", "total": {"$sum": "$amount"}}}]
|
||||
sales_summary = list(sales_collection.aggregate(pipeline))
|
||||
|
||||
success = (
|
||||
len(sales_summary) == 3 and
|
||||
any(item["_id"] == "Laptop" and item["total"] == 999.99 for item in sales_summary) and
|
||||
any(item["_id"] == "Mouse" and item["total"] == 29.99 for item in sales_summary) and
|
||||
any(item["_id"] == "Keyboard" and item["total"] == 59.99 for item in sales_summary)
|
||||
len(sales_summary) == 3
|
||||
and any(
|
||||
item["_id"] == "Laptop" and item["total"] == 999.99
|
||||
for item in sales_summary
|
||||
)
|
||||
and any(
|
||||
item["_id"] == "Mouse" and item["total"] == 29.99
|
||||
for item in sales_summary
|
||||
)
|
||||
and any(
|
||||
item["_id"] == "Keyboard" and item["total"] == 59.99
|
||||
for item in sales_summary
|
||||
)
|
||||
)
|
||||
print(f"Test {'passed' if success else 'failed'}")
|
||||
return success
|
||||
@@ -174,11 +183,15 @@ def test_index_operations():
|
||||
users_collection.create_index([("username", 1), ("role", 1)])
|
||||
|
||||
# Insert initial document
|
||||
users_collection.insert_one({"username": "test_user", "email": "test@example.com"})
|
||||
users_collection.insert_one(
|
||||
{"username": "test_user", "email": "test@example.com"}
|
||||
)
|
||||
|
||||
# Try to insert duplicate email (should fail)
|
||||
try:
|
||||
users_collection.insert_one({"username": "test_user2", "email": "test@example.com"})
|
||||
users_collection.insert_one(
|
||||
{"username": "test_user2", "email": "test@example.com"}
|
||||
)
|
||||
success = False # Should not reach here
|
||||
except Exception:
|
||||
success = True
|
||||
@@ -196,49 +209,49 @@ def test_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
|
||||
}
|
||||
])
|
||||
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
|
||||
}))
|
||||
expensive_electronics = list(
|
||||
products_collection.find(
|
||||
{
|
||||
"price": {"$gt": 500},
|
||||
"tags": {"$in": ["electronics"]},
|
||||
"in_stock": True,
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
# Update with multiple conditions
|
||||
update_result = products_collection.update_many(
|
||||
{
|
||||
"price": {"$lt": 100},
|
||||
"in_stock": True
|
||||
},
|
||||
{
|
||||
"$set": {"discount": 0.1},
|
||||
"$inc": {"price": -10}
|
||||
}
|
||||
{"price": {"$lt": 100}, "in_stock": True},
|
||||
{"$set": {"discount": 0.1}, "$inc": {"price": -10}},
|
||||
)
|
||||
|
||||
# Verify the update
|
||||
updated_product = products_collection.find_one({"name": "Cheap Mouse"})
|
||||
|
||||
success = (
|
||||
len(expensive_electronics) == 1 and
|
||||
expensive_electronics[0]["name"] == "Expensive Laptop" and
|
||||
update_result.modified_count == 1 and
|
||||
updated_product["price"] == 19.99 and
|
||||
updated_product["discount"] == 0.1
|
||||
len(expensive_electronics) == 1
|
||||
and expensive_electronics[0]["name"] == "Expensive Laptop"
|
||||
and update_result.modified_count == 1
|
||||
and updated_product["price"] == 19.99
|
||||
and updated_product["discount"] == 0.1
|
||||
)
|
||||
print(f"Test {'passed' if success else 'failed'}")
|
||||
return success
|
||||
@@ -250,19 +263,19 @@ def test_complex_queries():
|
||||
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
|
||||
test_complex_queries,
|
||||
]
|
||||
|
||||
|
||||
passed_list, not_passed_list = [], []
|
||||
passed, failed = 0, 0
|
||||
|
||||
@@ -282,9 +295,9 @@ def run_all_tests():
|
||||
not_passed_list.append(f"Test {test.__name__} failed")
|
||||
|
||||
print(f"\nTest Results: {passed} passed, {failed} failed")
|
||||
print('Passed Tests:')
|
||||
print("Passed Tests:")
|
||||
print("\n".join(passed_list))
|
||||
print('Failed Tests:')
|
||||
print("Failed Tests:")
|
||||
print("\n".join(not_passed_list))
|
||||
|
||||
return passed, failed
|
||||
|
||||
Reference in New Issue
Block a user