Add $lastN compatibility tests#680
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 |
| LASTN_ERROR_TESTS = ( | ||
| LASTN_MISSING_N_ERROR_TESTS + LASTN_NON_POSITIVE_N_ERROR_TESTS + LASTN_NON_INTEGER_N_ERROR_TESTS | ||
| ) |
|
🤖 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 |
What
Adds compatibility test coverage for the
$lastNaccumulator, which previouslyhad only a smoke test. Two new files under
documentdb_tests/compatibility/tests/core/operator/accumulators/lastN/:test_accumulator_lastN.py— success behavior (17 parametrized cases):ngreater than / equal to / less than group size,n = 1(returns a single-element array, not a scalar), and
nsupplied as along.null),not skipped — matching
$last.(
NaN,Infinity,Decimal128("NaN")) passed through unchanged.test_accumulator_lastN_errors.py— error behavior (5 parametrized cases):missing
n,n = 0, negativen, non-integer double, and non-integerDecimal128.Why
Expands aggregation compatibility coverage for the
$firstN/$lastNfamily.$lastNwas only smoke-tested; this brings it to a full success + error suite, consistent with
the sibling
first/andlast/accumulator suites.Error-code behavior
Assertions match what the reference engine (MongoDB 8.2.4) actually returns:
n→N_ACCUMULATOR_MISSING_N_FIRSTN_FAMILY_ERROR(5787906)n(zero, negative, or non-integer) → the genericN_ACCUMULATOR_INVALID_N_ERROR(7548606)This is consistent with the existing
$groupN-accumulator error tests(
stages/group/test_group_n_accumulator_errors.py). No new error-code constants wereneeded. An empty
__init__.pywas added to thelastN/package to match the otherfull-suite accumulator directories.
How to test
# start the reference engine (matches CI) docker run --rm -d -p 27017:27017 --name mongo mongo:8.2.4 pytest documentdb_tests/compatibility/tests/core/operator/accumulators/lastN/ \ --connection-string mongodb://localhost:27017 --engine-name mongodb -vVerified locally against mongo:8.2.4: 23 passed (17 success + 5 error + 1 smoke).
Lint/format/type gates (pre-commit run --all-files) and pytest -m unit pass.