Skip to content

Add $lastN compatibility tests#680

Draft
sbhattap wants to merge 1 commit into
documentdb:mainfrom
sbhattap:feature/add-lastn-compatibility-test
Draft

Add $lastN compatibility tests#680
sbhattap wants to merge 1 commit into
documentdb:mainfrom
sbhattap:feature/add-lastn-compatibility-test

Conversation

@sbhattap

@sbhattap sbhattap commented Jul 12, 2026

Copy link
Copy Markdown

What

Adds compatibility test coverage for the $lastN accumulator, which previously
had 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):

    • n vs group size: n greater than / equal to / less than group size, n = 1
      (returns a single-element array, not a scalar), and n supplied as a long.
    • Sort-order dependence: ascending, descending, secondary-field, and compound sort.
    • Null / missing handling: nulls and missing fields are included (as null),
      not skipped — matching $last.
    • Mixed BSON types: heterogeneous values, nested arrays, and special numerics
      (NaN, Infinity, Decimal128("NaN")) passed through unchanged.
    • Empty group: empty collection produces no groups.
  • test_accumulator_lastN_errors.py — error behavior (5 parametrized cases):
    missing n, n = 0, negative n, non-integer double, and non-integer Decimal128.

Why

Expands aggregation compatibility coverage for the $firstN/$lastN family. $lastN
was only smoke-tested; this brings it to a full success + error suite, consistent with
the sibling first/ and last/ accumulator suites.

Error-code behavior

Assertions match what the reference engine (MongoDB 8.2.4) actually returns:

  • Missing nN_ACCUMULATOR_MISSING_N_FIRSTN_FAMILY_ERROR (5787906)
  • Any other invalid n (zero, negative, or non-integer) → the generic
    N_ACCUMULATOR_INVALID_N_ERROR (7548606)

This is consistent with the existing $group N-accumulator error tests
(stages/group/test_group_n_accumulator_errors.py). No new error-code constants were
needed. An empty __init__.py was added to the lastN/ package to match the other
full-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 -v

Verified 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.

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
Comment on lines +98 to +100
LASTN_ERROR_TESTS = (
LASTN_MISSING_N_ERROR_TESTS + LASTN_NON_POSITIVE_N_ERROR_TESTS + LASTN_NON_INTEGER_N_ERROR_TESTS
)
@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.

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.

2 participants