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
@@ -0,0 +1,31 @@
"""
Validation tests for $minN-array-element n argument.
"""

import pytest

from documentdb_tests.framework.assertions import assertFailureCode
from documentdb_tests.framework.error_codes import N_ARRAY_ELEMENT_NON_POSITIVE_N_ERROR
from documentdb_tests.framework.executor import execute_command

pytestmark = pytest.mark.aggregate


def test_minN_array_element_rejects_zero_n(collection):
"""Test $minN-array-element rejects n=0."""
collection.insert_one({"_id": 1, "values": [1, 2]})

result = execute_command(
collection,
{
"aggregate": collection.name,
"pipeline": [{"$project": {"minValues": {"$minN": {"n": 0, "input": "$values"}}}}],
"cursor": {},
},
)

assertFailureCode(
result,
N_ARRAY_ELEMENT_NON_POSITIVE_N_ERROR,
msg="$minN-array-element should reject n=0",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Compatibility tests for $minN-array-element null filtering behavior.

Tests that $minN-array-element filters null values when n exceeds the number of
available non-null values.
"""

import pytest

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

pytestmark = [pytest.mark.aggregate, pytest.mark.smoke]


def test_minN_array_element_filters_null_values_when_n_exceeds_available_values(collection):
"""Test $minN-array-element filters null values when n exceeds available values."""
collection.insert_many(
[
{"_id": 1, "values": [None, 8, 2, None, 5]},
{"_id": 2, "values": [None]},
{"_id": 3, "values": []},
]
)

result = execute_command(
collection,
{
"aggregate": collection.name,
"pipeline": [{"$project": {"minValues": {"$minN": {"n": 5, "input": "$values"}}}}],
"cursor": {},
},
)

expected = [
{"_id": 1, "minValues": [2, 5, 8]},
{"_id": 2, "minValues": []},
{"_id": 3, "minValues": []},
]
assertSuccess(
result,
expected,
msg="$minN-array-element should filter null values when n exceeds available values",
)
1 change: 1 addition & 0 deletions documentdb_tests/framework/error_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@
MERGE_INTO_EMPTY_STRING_ERROR = 5786800
MERGE_INTO_COLL_NULL_ERROR = 5786801
N_ACCUMULATOR_MISSING_N_FIRSTN_FAMILY_ERROR = 5787906
N_ARRAY_ELEMENT_NON_POSITIVE_N_ERROR = 5787908
N_ACCUMULATOR_MISSING_N_TOPN_FAMILY_ERROR = 5788003
GEO_NEAR_NEAR_REQUIRED_ERROR = 5860400
GEO_NEAR_NEAR_TYPE_ERROR = 5860401
Expand Down