From bd0b3e5ca241597f940e6c56bc7cd24e79891ce3 Mon Sep 17 00:00:00 2001 From: Andranik Avagyan Date: Sun, 21 Jun 2026 23:29:55 -0700 Subject: [PATCH] Add minN array element compatibility tests Signed-off-by: Andranik Avagyan --- .../test_minN-array-element_n_validation.py | 31 +++++++++++++ .../test_minN-array-element_null_filtering.py | 44 +++++++++++++++++++ documentdb_tests/framework/error_codes.py | 1 + 3 files changed, 76 insertions(+) create mode 100644 documentdb_tests/compatibility/tests/core/operator/expressions/array/minN-array-element/test_minN-array-element_n_validation.py create mode 100644 documentdb_tests/compatibility/tests/core/operator/expressions/array/minN-array-element/test_minN-array-element_null_filtering.py 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 21b2ce59d..0e94a935b 100644 --- a/documentdb_tests/framework/error_codes.py +++ b/documentdb_tests/framework/error_codes.py @@ -443,6 +443,7 @@ GEO_NEAR_KEY_DEPTH_LIMIT_ERROR = 5729100 MODULO_DECIMAL128_ZERO_REMAINDER_ERROR = 5733415 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