diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/array/minN-array-element/test_minN-array-element_n_validation.py b/documentdb_tests/compatibility/tests/core/operator/expressions/array/minN-array-element/test_minN-array-element_n_validation.py new file mode 100644 index 000000000..67c002bf9 --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/array/minN-array-element/test_minN-array-element_n_validation.py @@ -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", + ) diff --git a/documentdb_tests/compatibility/tests/core/operator/expressions/array/minN-array-element/test_minN-array-element_null_filtering.py b/documentdb_tests/compatibility/tests/core/operator/expressions/array/minN-array-element/test_minN-array-element_null_filtering.py new file mode 100644 index 000000000..70d5658ce --- /dev/null +++ b/documentdb_tests/compatibility/tests/core/operator/expressions/array/minN-array-element/test_minN-array-element_null_filtering.py @@ -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", + ) diff --git a/documentdb_tests/framework/error_codes.py b/documentdb_tests/framework/error_codes.py index 423b3fe65..09b0201ed 100644 --- a/documentdb_tests/framework/error_codes.py +++ b/documentdb_tests/framework/error_codes.py @@ -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