Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ from tests.common.assertions import (
assert_document_match(actual, expected, ignore_id=True)

# Compare lists of documents
assert_documents_match(actual_list, expected_list, ignore_order=True)
assert_documents_match(actual_list, expected_list, ignore_doc_order=True)

# Check field existence
assert_field_exists(document, "user.name")
Expand Down
19 changes: 0 additions & 19 deletions documentdb_tests/compatibility/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,4 @@
DocumentDB Functional Tests
End-to-end functional testing suite for DocumentDB.
This package provides test utilities and common assertions for testing
DocumentDB functionality.
"""

from documentdb_tests.framework.assertions import (
assert_count,
assert_document_match,
assert_documents_match,
assert_field_exists,
assert_field_not_exists,
)

__all__ = [
"assert_count",
"assert_document_match",
"assert_documents_match",
"assert_field_exists",
"assert_field_not_exists",
]
Original file line number Diff line number Diff line change
@@ -1,43 +1,18 @@
"""
Tests for capped collection operations.

NOTE: This test was added by design to demonstrate how the Result Analyzer
automatically detects and categorizes unsupported features (error code 115).
When run against DocumentDB, this test will fail with error code 115, which
the analyzer will correctly categorize as UNSUPPORTED rather than FAIL.
Capped collections are fixed-size collections that maintain insertion order.
This feature may not be supported on all engines.
"""

import pytest
from pymongo.errors import OperationFailure

from documentdb_tests.framework.executor import execute_command
from documentdb_tests.framework.assertions import assertSuccessPartial


@pytest.mark.collection_mgmt
def test_create_capped_collection(database_client):
"""
Test creating a capped collection.

Capped collections are fixed-size collections that maintain insertion order.
This feature is not supported in DocumentDB and should return error code 115.

Expected behavior:
- Creates a capped collection successfully
"""
collection_name = "capped_test_collection"

try:
# Attempt to create capped collection
database_client.create_collection(
collection_name,
capped=True,
size=100000
)

# Verify it's actually capped
collection_info = database_client[collection_name].options()
assert collection_info.get("capped") is True, "Collection should be capped"

# Cleanup
database_client.drop_collection(collection_name)

except OperationFailure as e:
raise
def test_create_capped_collection(collection):
"""Test creating a capped collection."""
result = execute_command(collection, {"create": collection.name + "_capped", "capped": True, "size": 100000})
assertSuccessPartial(result, {"ok": 1.0}, "Should create capped collection")
Original file line number Diff line number Diff line change
Expand Up @@ -6,123 +6,94 @@

import pytest

from documentdb_tests.framework.executor import execute_command
from documentdb_tests.framework.assertions import assertSuccess


@pytest.mark.aggregate
@pytest.mark.smoke
def test_group_with_count(collection):
"""Test $group stage with count aggregation."""
# Arrange - Insert test data
collection.insert_many([
{"name": "Alice", "department": "Engineering", "salary": 100000},
{"name": "Bob", "department": "Engineering", "salary": 90000},
{"name": "Charlie", "department": "Sales", "salary": 80000},
{"name": "David", "department": "Sales", "salary": 75000},
{"a": "A", "b": 100},
{"a": "A", "b": 90},
{"a": "B", "b": 80},
{"a": "B", "b": 75},
])
pipeline = [{"$group": {"_id": "$a", "count": {"$sum": 1}}}]
result = execute_command(collection, {"aggregate": collection.name, "pipeline": pipeline, "cursor": {}})

# Act - Execute aggregation to count documents by department
pipeline = [{"$group": {"_id": "$department", "count": {"$sum": 1}}}]
result = list(collection.aggregate(pipeline))

# Assert - Verify results
assert len(result) == 2, "Expected 2 departments"

# Convert to dict for easier verification
dept_counts = {doc["_id"]: doc["count"] for doc in result}
assert dept_counts["Engineering"] == 2, "Expected 2 employees in Engineering"
assert dept_counts["Sales"] == 2, "Expected 2 employees in Sales"
expected = [
{"_id": "A", "count": 2},
{"_id": "B", "count": 2}
]
assertSuccess(result, expected, "Should group and count by a", ignore_doc_order=True)


@pytest.mark.aggregate
def test_group_with_sum(collection):
"""Test $group stage with sum aggregation."""
# Arrange - Insert test data
collection.insert_many([
{"name": "Alice", "department": "Engineering", "salary": 100000},
{"name": "Bob", "department": "Engineering", "salary": 90000},
{"name": "Charlie", "department": "Sales", "salary": 80000},
{"a": "A", "b": 100},
{"a": "A", "b": 90},
{"a": "B", "b": 80},
])
pipeline = [{"$group": {"_id": "$a", "total": {"$sum": "$b"}}}]
result = execute_command(collection, {"aggregate": collection.name, "pipeline": pipeline, "cursor": {}})

# Act - Execute aggregation to sum salaries by department
pipeline = [{"$group": {"_id": "$department", "totalSalary": {"$sum": "$salary"}}}]
result = list(collection.aggregate(pipeline))

# Assert - Verify results
assert len(result) == 2, "Expected 2 departments"

# Convert to dict for easier verification
dept_salaries = {doc["_id"]: doc["totalSalary"] for doc in result}
assert dept_salaries["Engineering"] == 190000, "Expected total Engineering salary of 190000"
assert dept_salaries["Sales"] == 80000, "Expected total Sales salary of 80000"
expected = [
{"_id": "A", "total": 190},
{"_id": "B", "total": 80}
]
assertSuccess(result, expected, "Should sum by group", ignore_doc_order=True)


@pytest.mark.aggregate
def test_group_with_avg(collection):
"""Test $group stage with average aggregation."""
# Arrange - Insert test data
collection.insert_many([
{"name": "Alice", "department": "Engineering", "salary": 100000},
{"name": "Bob", "department": "Engineering", "salary": 90000},
{"name": "Charlie", "department": "Sales", "salary": 80000},
{"a": "A", "b": 100},
{"a": "A", "b": 90},
{"a": "B", "b": 80},
])
pipeline = [{"$group": {"_id": "$a", "avg": {"$avg": "$b"}}}]
result = execute_command(collection, {"aggregate": collection.name, "pipeline": pipeline, "cursor": {}})

# Act - Execute aggregation to calculate average salary by department
pipeline = [{"$group": {"_id": "$department", "avgSalary": {"$avg": "$salary"}}}]
result = list(collection.aggregate(pipeline))

# Assert - Verify results
assert len(result) == 2, "Expected 2 departments"

# Convert to dict for easier verification
dept_avg = {doc["_id"]: doc["avgSalary"] for doc in result}
assert dept_avg["Engineering"] == 95000, "Expected average Engineering salary of 95000"
assert dept_avg["Sales"] == 80000, "Expected average Sales salary of 80000"
expected = [
{"_id": "A", "avg": 95.0},
{"_id": "B", "avg": 80.0}
]
assertSuccess(result, expected, "Should calculate average by group", ignore_doc_order=True)


@pytest.mark.aggregate
def test_group_with_min_max(collection):
"""Test $group stage with min and max aggregations."""
# Arrange - Insert test data
collection.insert_many([
{"name": "Alice", "department": "Engineering", "salary": 100000},
{"name": "Bob", "department": "Engineering", "salary": 90000},
{"name": "Charlie", "department": "Sales", "salary": 80000},
{"a": "A", "b": 100},
{"a": "A", "b": 90},
{"a": "B", "b": 80},
])
pipeline = [{"$group": {"_id": "$a", "min": {"$min": "$b"}, "max": {"$max": "$b"}}}]
result = execute_command(collection, {"aggregate": collection.name, "pipeline": pipeline, "cursor": {}})

# Act - Execute aggregation to find min and max salary by department
pipeline = [
{
"$group": {
"_id": "$department",
"minSalary": {"$min": "$salary"},
"maxSalary": {"$max": "$salary"},
}
}
expected = [
{"_id": "A", "min": 90, "max": 100},
{"_id": "B", "min": 80, "max": 80}
]
result = list(collection.aggregate(pipeline))

# Assert - Verify results
assert len(result) == 2, "Expected 2 departments"

# Verify Engineering department
eng_dept = next(doc for doc in result if doc["_id"] == "Engineering")
assert eng_dept["minSalary"] == 90000, "Expected min Engineering salary of 90000"
assert eng_dept["maxSalary"] == 100000, "Expected max Engineering salary of 100000"
assertSuccess(result, expected, "Should find min and max by group", ignore_doc_order=True)


@pytest.mark.aggregate
def test_group_all_documents(collection):
"""Test $group stage grouping all documents (using null as _id)."""
# Arrange - Insert test data
collection.insert_many([
{"item": "A", "quantity": 5},
{"item": "B", "quantity": 10},
{"item": "A", "quantity": 3},
{"a": "A", "b": 5},
{"a": "B", "b": 10},
{"a": "A", "b": 3},
])
pipeline = [{"$group": {"_id": None, "total": {"$sum": "$b"}}}]
result = execute_command(collection, {"aggregate": collection.name, "pipeline": pipeline, "cursor": {}})

# Act - Execute aggregation to sum quantities across all documents
pipeline = [{"$group": {"_id": None, "totalQuantity": {"$sum": "$quantity"}}}]
result = list(collection.aggregate(pipeline))

# Assert - Verify results
assert len(result) == 1, "Expected single result grouping all documents"
assert result[0]["totalQuantity"] == 18, "Expected total quantity of 18"
expected = [{"_id": None, "total": 18}]
assertSuccess(result, expected, "Should sum across all documents")
Original file line number Diff line number Diff line change
Expand Up @@ -6,81 +6,68 @@

import pytest

from documentdb_tests.framework.executor import execute_command
from documentdb_tests.framework.assertions import assertSuccess


@pytest.mark.aggregate
@pytest.mark.smoke
def test_match_simple_filter(collection):
"""Test $match stage with simple equality filter."""
# Arrange - Insert test data
collection.insert_many([
{"name": "Alice", "age": 30, "status": "active"},
{"name": "Bob", "age": 25, "status": "active"},
{"name": "Charlie", "age": 35, "status": "inactive"},
{"_id": 0, "a": "A", "b": 30, "c": "active"},
{"_id": 1, "a": "B", "b": 25, "c": "active"},
{"_id": 2, "a": "C", "b": 35, "c": "inactive"},
])

# Act - Execute aggregation with $match stage
pipeline = [{"$match": {"status": "active"}}]
result = list(collection.aggregate(pipeline))

# Assert - Verify results
assert len(result) == 2, "Expected 2 active users"
names = {doc["name"] for doc in result}
assert names == {"Alice", "Bob"}, "Expected Alice and Bob"
result = execute_command(collection, {"aggregate": collection.name, "pipeline": [{"$match": {"c": "active"}}], "cursor": {}})

expected = [
{"_id": 0, "a": "A", "b": 30, "c": "active"},
{"_id": 1, "a": "B", "b": 25, "c": "active"}
]
assertSuccess(result, expected, "Should match active documents")


@pytest.mark.aggregate
def test_match_with_comparison_operator(collection):
"""Test $match stage with comparison operators."""
# Arrange - Insert test data
collection.insert_many([
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
{"name": "Charlie", "age": 35},
{"_id": 0, "a": "A", "b": 30},
{"_id": 1, "a": "B", "b": 25},
{"_id": 2, "a": "C", "b": 35},
])

# Act - Execute aggregation with $match using $gt
pipeline = [{"$match": {"age": {"$gt": 25}}}]
result = list(collection.aggregate(pipeline))

# Assert - Verify results
assert len(result) == 2, "Expected 2 users with age > 25"
names = {doc["name"] for doc in result}
assert names == {"Alice", "Charlie"}, "Expected Alice and Charlie"
result = execute_command(collection, {"aggregate": collection.name, "pipeline": [{"$match": {"b": {"$gt": 25}}}], "cursor": {}})

expected = [
{"_id": 0, "a": "A", "b": 30},
{"_id": 2, "a": "C", "b": 35}
]
assertSuccess(result, expected, "Should match documents with b > 25")


@pytest.mark.aggregate
def test_match_multiple_conditions(collection):
"""Test $match stage with multiple filter conditions."""
# Arrange - Insert test data
collection.insert_many([
{"name": "Alice", "age": 30, "city": "NYC"},
{"name": "Bob", "age": 25, "city": "SF"},
{"name": "Charlie", "age": 35, "city": "NYC"},
{"_id": 0, "a": "A", "b": 30, "c": "NYC"},
{"_id": 1, "a": "B", "b": 25, "c": "SF"},
{"_id": 2, "a": "C", "b": 35, "c": "SF"},
])

# Act - Execute aggregation with multiple conditions in $match
pipeline = [{"$match": {"city": "NYC", "age": {"$gte": 30}}}]
result = list(collection.aggregate(pipeline))

# Assert - Verify results
assert len(result) == 2, "Expected 2 users from NYC with age >= 30"
names = {doc["name"] for doc in result}
assert names == {"Alice", "Charlie"}, "Expected Alice and Charlie"
result = execute_command(collection, {"aggregate": collection.name, "pipeline": [{"$match": {"c": "NYC", "b": {"$gte": 30}}}], "cursor": {}})

expected = [
{"_id": 0, "a": "A", "b": 30, "c": "NYC"}
]
assertSuccess(result, expected, "Should match multiple conditions")


@pytest.mark.aggregate
@pytest.mark.find
def test_match_empty_result(collection):
"""Test $match stage that matches no documents."""
# Arrange - Insert test data
collection.insert_many([
{"name": "Alice", "status": "active"},
{"name": "Bob", "status": "active"},
{"_id": 0, "a": "A", "b": "active"},
{"_id": 1, "a": "B", "b": "active"},
])

# Act - Execute aggregation with $match that matches nothing
pipeline = [{"$match": {"status": "inactive"}}]
result = list(collection.aggregate(pipeline))

# Assert - Verify empty result
assert result == [], "Expected empty result when no documents match"
result = execute_command(collection, {"aggregate": collection.name, "pipeline": [{"$match": {"b": "inactive"}}], "cursor": {}})

assertSuccess(result, [], "Should return empty result when no match")
Loading
Loading