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

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
"""
Tests for $setWindowFields multi-output behavior.

These are operator-agnostic — they test the stage's ability to handle
multiple output fields with different accumulators and window specs.

Covers: multiple different operators in one output clause, same operator
with different window specs, and output field that overwrites _id.
"""

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


def test_mixed_operators_multi_output(collection):
"""Multiple different operators ($sum + $avg + $count) in one output clause."""
docs = [
{"_id": 1, "partition": "A", "value": 10},
{"_id": 2, "partition": "A", "value": 20},
{"_id": 3, "partition": "A", "value": 30},
]
collection.insert_many(docs)
result = execute_command(
collection,
{
"aggregate": collection.name,
"pipeline": [
{
"$setWindowFields": {
"partitionBy": "$partition",
"sortBy": {"_id": 1},
"output": {
"total": {
"$sum": "$value",
"window": {"documents": ["unbounded", "unbounded"]},
},
"avg": {
"$avg": "$value",
"window": {"documents": ["unbounded", "unbounded"]},
},
"cnt": {
"$count": {},
"window": {"documents": ["unbounded", "unbounded"]},
},
},
}
}
],
"cursor": {},
},
)
expected = [
{"_id": 1, "partition": "A", "value": 10, "total": 60, "avg": 20.0, "cnt": 3},
{"_id": 2, "partition": "A", "value": 20, "total": 60, "avg": 20.0, "cnt": 3},
{"_id": 3, "partition": "A", "value": 30, "total": 60, "avg": 20.0, "cnt": 3},
]
assertSuccess(result, expected, msg="mixed operators in one output clause")


def test_same_operator_different_windows(collection):
"""Same operator ($sum) with different window specs in one output clause."""
docs = [
{"_id": 1, "partition": "A", "value": 10},
{"_id": 2, "partition": "A", "value": 20},
{"_id": 3, "partition": "A", "value": 30},
]
collection.insert_many(docs)
result = execute_command(
collection,
{
"aggregate": collection.name,
"pipeline": [
{
"$setWindowFields": {
"partitionBy": "$partition",
"sortBy": {"_id": 1},
"output": {
"cumulative": {
"$sum": "$value",
"window": {"documents": ["unbounded", "current"]},
},
"whole": {
"$sum": "$value",
"window": {"documents": ["unbounded", "unbounded"]},
},
},
}
}
],
"cursor": {},
},
)
expected = [
{"_id": 1, "partition": "A", "value": 10, "cumulative": 10, "whole": 60},
{"_id": 2, "partition": "A", "value": 20, "cumulative": 30, "whole": 60},
{"_id": 3, "partition": "A", "value": 30, "cumulative": 60, "whole": 60},
]
assertSuccess(result, expected, msg="same operator with different windows in one output")


def test_output_field_name_id(collection):
"""Output field named '_id' overwrites original document _id."""
docs = [
{"_id": 1, "partition": "A", "value": 10},
{"_id": 2, "partition": "A", "value": 20},
{"_id": 3, "partition": "A", "value": 30},
]
collection.insert_many(docs)
result = execute_command(
collection,
{
"aggregate": collection.name,
"pipeline": [
{
"$setWindowFields": {
"partitionBy": "$partition",
"sortBy": {"_id": 1},
"output": {
"_id": {
"$sum": "$value",
"window": {"documents": ["unbounded", "unbounded"]},
}
},
}
}
],
"cursor": {},
},
)
expected = [
{"_id": 60, "partition": "A", "value": 10},
{"_id": 60, "partition": "A", "value": 20},
{"_id": 60, "partition": "A", "value": 30},
]
assertSuccess(result, expected, msg="output field _id overwrites document _id")
Loading
Loading