Skip to content

Add $lastN accumulator and expression compatibility tests (#473, #199) - #680

Open
sbhattap wants to merge 5 commits into
documentdb:mainfrom
sbhattap:feature/add-lastn-compatibility-test
Open

sbhattap wants to merge 5 commits into
documentdb:mainfrom
sbhattap:feature/add-lastn-compatibility-test

Conversation

@sbhattap

@sbhattap sbhattap commented Jul 12, 2026 •

Copy link
Copy Markdown

PR Description:

Summary

  • Add compatibility tests for $lastN in both of its forms: the $group accumulator and the array
    expression used inside $project
  • Add the error-code constants these tests assert against

Covers tests for #473 (accumulator form) and #199 (array-expression form).

Test Coverage

$lastN has two independent implementations that behave differently, so each is covered separately:

File Dimension
accumulators/lastN/test_accumulator_lastN.py n vs group size, per-group accumulation, sort-order dependency, null/missing inclusion, mixed BSON types, empty and filtered-to-empty groups
accumulators/lastN/test_accumulator_lastN_errors.py Malformed specification (scalar and array), unknown argument field, missing n and input, non-positive and non-integer n, non-coercible n
expressions/array/lastN/test_expression_lastN.py n vs array length, n typing (int/long/double/expression), element preservation without traversal, n and input as field paths, invalid n and non-array input
framework/error_codes.py 7 new constants for $lastN spec, n, and input validation (5787801, 5787901, 5787902, 5787903, 5787907, 5787908, 5788200)

Key behavioral differences tested:

  • Accumulator includes null and missing values in the result; the expression rejects a null, missing,
    or non-array input outright (5788200)
  • Invalid n collapses to a single generic code (7548606) in the accumulator, but reports granular
    codes (5787902 / 5787903 / 5787908) in the expression
  • An array specification reports 40237 in the accumulator and 5787801 in the expression, for
    identical input
  • n = NaN, infinity, or an out-of-range double reports 31109 in both forms — the one
    invalid-n path where they agree

Test plan

  • All 71 tests pass against the reference server (MongoDB 8.2.4)
  • Pre-commit hooks pass (black, isort, flake8, mypy, DCO sign-off)
  • All error codes verified against the reference server

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactoring / code cleanup
  • DevOps / tooling
  • Test improvement
  • Other: ___________

AI Disclosure

  • AI tools were used to assist with this PR (please complete below)
    • Tool(s) used: Claude Code (Claude Opus 5)
    • How it was used: Reviewed the repo structure, identified test coverage gaps, drafted new test cases

Checklist

  • I can explain every change in this PR if asked during review
  • I have tested these changes locally
  • I have added or updated tests as appropriate
  • All commits are signed off (DCO: git commit -s)
  • I have read the CONTRIBUTING.md guidelines]

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>
@sbhattap
sbhattap requested a review from a team as a code owner July 12, 2026 18:19
Copilot AI review requested due to automatic review settings July 12, 2026 18:19
@sbhattap sbhattap changed the title Add $lastN accumulator compatibility tests Add $lastN compatibility tests Jul 12, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 $lastN success suite covering group-size boundaries, sort-order dependence, null/missing inclusion semantics, mixed-type preservation, and empty-collection behavior.
  • Added a parametrized $lastN error suite covering missing n and invalid n values with assertions against established error-code constants.
  • Added an (empty) __init__.py to align the lastN/ 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.

Comment on lines +73 to +88
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",
),
Comment on lines +11 to +12
import pytest
from bson import Decimal128
@sbhattap
sbhattap marked this pull request as draft July 12, 2026 18:34
@documentdb-triage-tool documentdb-triage-tool Bot added compatibility test Compatibility test related enhancement New feature or request labels Jul 12, 2026
@documentdb-triage-tool

Copy link
Copy Markdown

🤖 Auto-triaged by documentdb-triage-tool.

Applied: compatibility test, enhancement
Project fields suggested: Component test-coverage · Priority P3 · Effort M · Status In Progress
Confidence: 0.95 (mixed)

Reasoning

component 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 @patty-chow so the rules can be tuned. The bot will not re-label items that already have component labels.

sbhattap added 3 commits July 21, 2026 17:56
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>
@sbhattap sbhattap changed the title Add $lastN compatibility tests Add $lastN accumulator and expression compatibility tests Aug 9, 2026
@sbhattap sbhattap changed the title Add $lastN accumulator and expression compatibility tests Add $lastN accumulator and expression compatibility tests (#473, #199) Aug 9, 2026
@sbhattap
sbhattap marked this pull request as ready for review September 30, 2026 04:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compatibility test Compatibility test related enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants