Conversation
Add compatibility tests for the $lastN accumulator, covering both success and error behavior: - test_accumulator_lastN.py: n vs group size, sort-order dependence, null/missing handling (included, not skipped), mixed BSON types, and empty groups. - test_accumulator_lastN_errors.py: missing, zero, negative, and non-integer n. Missing n fails with N_ACCUMULATOR_MISSING_N_FIRSTN_FAMILY_ERROR (5787906); any other invalid n fails with N_ACCUMULATOR_INVALID_N_ERROR (7548606), matching the existing $group N-accumulator error tests. Signed-off-by: Sunita Bhattacharya <sunitab55@gmail.com>
There was a problem hiding this comment.
Pull request overview
Adds a full compatibility test suite for the $lastN accumulator under the existing accumulator test hierarchy, expanding coverage beyond the existing smoke test to include both success and validation-error behaviors.
Changes:
- Added a parametrized
$lastNsuccess suite covering group-size boundaries, sort-order dependence, null/missing inclusion semantics, mixed-type preservation, and empty-collection behavior. - Added a parametrized
$lastNerror suite covering missingnand invalidnvalues with assertions against established error-code constants. - Added an (empty)
__init__.pyto align thelastN/directory with other accumulator test package layouts.
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| documentdb_tests/compatibility/tests/core/operator/accumulators/lastN/test_accumulator_lastN.py | New parametrized $lastN success-behavior coverage across ordering, boundaries, and type/null semantics. |
| documentdb_tests/compatibility/tests/core/operator/accumulators/lastN/test_accumulator_lastN_errors.py | New parametrized $lastN negative tests asserting correct error codes for invalid/missing n. |
| documentdb_tests/compatibility/tests/core/operator/accumulators/lastN/init.py | Adds package marker file for directory consistency with other accumulator suites. |
| AccumulatorTestCase( | ||
| "n_as_long", | ||
| docs=[{"_id": 0, "v": 10}, {"_id": 1, "v": 20}, {"_id": 2, "v": 30}], | ||
| pipeline=[ | ||
| {"$sort": {"_id": 1}}, | ||
| { | ||
| "$group": { | ||
| "_id": None, | ||
| "result": {"$lastN": {"n": {"$toLong": 2}, "input": "$v"}}, | ||
| } | ||
| }, | ||
| {"$project": {"_id": 0, "result": 1}}, | ||
| ], | ||
| expected=[{"result": [20, 30]}], | ||
| msg="$lastN should accept a long-typed n value", | ||
| ), |
| import pytest | ||
| from bson import Decimal128 |
|
🤖 Auto-triaged by documentdb-triage-tool. Applied: Reasoningcomponent from path globs (test-coverage); effort from diff stats (391+0 LOC, 3 files); LLM: Adds new compatibility test files for the $lastN accumulator under the test-coverage paths, expanding coverage with no functional or schema changes. If a label is wrong, remove it manually and ping |
Signed-off-by: Sunita Bhattacharya <sunitab55@gmail.com>
test_accumulator_lastN_errors.py: add error cases for malformed specification, missing input, and non-coercible n. test_accumulator_lastN.py: add behavior cases for per-group accumulation and filtered-to-empty groups. Signed-off-by: Sunita Bhattacharya <sunitab55@gmail.com>
test_expression_lastN.py: cover n-vs-length, n typing, element preservation, field references, and invalid n / input handling. Includes the non-coercible n path (NaN, infinity, overflow), where the expression and accumulator forms report the same code. Signed-off-by: Sunita Bhattacharya <sunitab55@gmail.com>
PR Description:
Summary
$lastNin both of its forms: the$groupaccumulator and the arrayexpression used inside
$projectCovers tests for #473 (accumulator form) and #199 (array-expression form).
Test Coverage
$lastNhas two independent implementations that behave differently, so each is covered separately:accumulators/lastN/test_accumulator_lastN.pyaccumulators/lastN/test_accumulator_lastN_errors.pynandinput, non-positive and non-integern, non-coerciblenexpressions/array/lastN/test_expression_lastN.pyntyping (int/long/double/expression), element preservation without traversal,nandinputas field paths, invalidnand non-arrayinputframework/error_codes.py$lastNspec,n, andinputvalidation (5787801, 5787901, 5787902, 5787903, 5787907, 5787908, 5788200)Key behavioral differences tested:
or non-array
inputoutright (5788200)ncollapses to a single generic code (7548606) in the accumulator, but reports granularcodes (
5787902/5787903/5787908) in the expression40237in the accumulator and5787801in the expression, foridentical input
n= NaN, infinity, or an out-of-range double reports31109in both forms — the oneinvalid-
npath where they agreeTest plan
Type of Change
AI Disclosure
Checklist
git commit -s)