Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,12 @@ def test_let_nested_combinations(collection, test):
# ---------------------------------------------------------------------------
def test_let_two_lets_same_projection(collection):
"""Test two separate $let expressions in same projection with same variable name."""
collection.insert_one({})
result = execute_command(
collection,
{
"aggregate": 1,
"aggregate": collection.name,
"pipeline": [
{"$documents": [{}]},
{
"$project": {
"_id": 0,
Expand Down Expand Up @@ -248,12 +248,12 @@ def test_let_across_multiple_documents(collection):

def test_let_error_cross_let_variable_ref(collection):
"""Test $let where variable defined in one $let is referenced in sibling $let."""
collection.insert_one({})
result = execute_command(
collection,
{
"aggregate": 1,
"aggregate": collection.name,
"pipeline": [
{"$documents": [{}]},
{
"$project": {
"_id": 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def execute_project(collection, project):
"""
Execute a projection with literal input values.

Evaluates the projection against a single empty document. The document is
inserted into the collection and the pipeline runs over that collection,
rather than synthesizing the row with a ``$documents`` stage. This keeps the
helper free of any dependency on ``$documents`` support while producing the
same single-row input the projection sees.

Args:
collection: MongoDB collection object
project: Fields to project. Do not include _id; the function always
Expand All @@ -51,12 +57,12 @@ def execute_project(collection, project):
>>> execute_project(collection, {"sum": {"$add": [1, 2]}})
# Returns result with {"sum": 3} in firstBatch
"""
collection.insert_one({})
return execute_command(
collection,
{
"aggregate": 1,
"aggregate": collection.name,
"pipeline": [
{"$documents": [{}]},
{"$project": {**materialize(project), "_id": 0}},
],
"cursor": {},
Expand Down Expand Up @@ -100,10 +106,15 @@ def execute_project_with_insert(collection, document, project):

def execute_expression(collection, expression):
"""
Execute an aggregation expression using $documents stage.
Execute an aggregation expression against a single empty document.

Evaluates an expression against an empty document using the $documents
stage. Useful for testing expressions with literal values.
Evaluates an expression against an empty document. The document is inserted
into the collection and the pipeline runs over that collection, rather than
synthesizing the row with a ``$documents`` stage. This keeps the helper free
of any dependency on ``$documents`` support while producing the same
single-row input the expression is evaluated against. Useful for testing
expressions with literal values; field references resolve to missing, just
as they would against a ``$documents: [{}]`` row.

Args:
collection: MongoDB collection object
Expand All @@ -117,12 +128,12 @@ def execute_expression(collection, expression):
>>> execute_expression(collection, {"$add": [1, 2]})
# Returns result with {"result": 3} in firstBatch
"""
collection.insert_one({})
return execute_command(
collection,
{
"aggregate": 1,
"aggregate": collection.name,
"pipeline": [
{"$documents": [{}]},
{"$project": {"_id": 0, "result": expression}},
],
"cursor": {},
Expand Down