Skip to content

Add $toInt, $toDouble, $toLong, $toDecimal tests#684

Open
PatersonProjects wants to merge 17 commits into
documentdb:mainfrom
PatersonProjects:toNumber_tests
Open

Add $toInt, $toDouble, $toLong, $toDecimal tests#684
PatersonProjects wants to merge 17 commits into
documentdb:mainfrom
PatersonProjects:toNumber_tests

Conversation

@PatersonProjects

@PatersonProjects PatersonProjects commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

This PR adds the tests for $toInt, $toDouble, $toLong, and $toDecimal. It was originally authored by @danielfrankcom.

Ref: Issue #15, #328, #327, #329, #326

Signed-off-by: PatersonProjects <keldonhoff@gmail.com>
Signed-off-by: PatersonProjects <keldonhoff@gmail.com>
Signed-off-by: PatersonProjects <keldonhoff@gmail.com>
Signed-off-by: PatersonProjects <keldonhoff@gmail.com>
Signed-off-by: PatersonProjects <keldonhoff@gmail.com>
…ed magic number with constants

Signed-off-by: PatersonProjects <keldonhoff@gmail.com>
Migrates  tests from old_tests/ to documentdb_tests/compatibility/
tests/core/operator/expressions/type/toLong/ following the migration harness.

Files added:
- __init__.py
- test_smoke_toLong.py (was already present)
- test_toLong_arity.py       (arity + invalid field path)
- test_toLong_numeric.py     (null/missing, bool, int32, int64, double)
- test_toLong_decimal128.py  (Decimal128 truncation and overflow)
- test_toLong_datetime_binary.py (datetime, binary, unsupported types)
- test_toLong_string.py      (string conversion + size limit)
- test_toLong_return_type.py (return type invariant + idempotency)
- test_toLong_field_ref.py   (field references + expression input)

Signed-off-by: PatersonProjects <keldonhoff@gmail.com>
…idator

- Add generate_bson_rejection_test_cases() to test_toLong_return_type.py
  for ObjectId, Regex, Timestamp, JavaScript/Code, MinKey, MaxKey types
- Skip NULL (returns null, not error), OBJECT and ARRAY (need $literal
  wrapping) from generated rejection — keep hand-written cases for those
- Remove the six now-redundant hand-written ExpressionTestCase rejection
  entries from test_toLong_datetime_binary.py
- Drop unused bson imports (Code, MaxKey, MinKey, ObjectId, Regex, Timestamp)

Signed-off-by: PatersonProjects <keldonhoff@gmail.com>
Add two cases missing from the new test suite vs the old:

1. type_code_with_scope: CodeWithScope (BSON 0x0f) is a distinct type from
   JavaScript/Code (0x0d) and is not covered by bson_type_validator samples.
   Verified it produces CONVERSION_FAILURE_ERROR like plain Code.

2. type_nested_array: $toLong([[\x22 42\x22]]) — the outer single-element literal
   array is unwrapped at parse time, leaving the inner [\x2242\x22] (a runtime array)
   as the actual argument, which is a conversion failure.

Signed-off-by: PatersonProjects <keldonhoff@gmail.com>
Replace three bare numeric literals flagged by find_numerical_constant_replacements.py:
- 0 (int32) -> INT32_ZERO
- 0.5         -> DOUBLE_HALF
- -0.5        -> DOUBLE_NEGATIVE_HALF

Signed-off-by: PatersonProjects <keldonhoff@gmail.com>
…nstants

framework/test_constants.py:
- Add INT64_FROM_DOUBLE_MAX_FITTING = Int64(9223372036854774784)
  (int64 truncation of DOUBLE_MAX_FITTING_INT64)
- Add DOUBLE_BELOW_INT64_MIN = math.nextafter(-float(2**63), float('-inf'))
  (first IEEE 754 double strictly below int64 min)
- Add 'import math' for the above

test_toLong_numeric.py:
- Replace inline Int64(9223372036854774784) with INT64_FROM_DOUBLE_MAX_FITTING
- Replace math.nextafter(...) expression with DOUBLE_BELOW_INT64_MIN
- Drop now-redundant 'import math'

Signed-off-by: PatersonProjects <keldonhoff@gmail.com>
Restore math.nextafter(-float(2**63), FLOAT_NEGATIVE_INFINITY) inline in
test_toLong_numeric.py and remove the constant (and import math) that
were added to test_constants.py in the previous commit.

Signed-off-by: PatersonProjects <keldonhoff@gmail.com>
Signed-off-by: PatersonProjects <keldonhoff@gmail.com>
@PatersonProjects PatersonProjects requested a review from a team as a code owner July 14, 2026 15:47
@PatersonProjects PatersonProjects changed the title Add toInt, toDouble, toLong, toDecimal tests Add $toInt, $toDouble, $toLong, $toDecimal tests Jul 14, 2026
)

# Property [Datetime]: $toDecimal converts datetime to milliseconds since Unix epoch as Decimal128.
_TODECIMAL_DATETIME_TESTS: list[ExpressionTestCase] = [

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: different files have inconsistent use of underscore prefixed internal constants vs normal constants. Would be good to be consistent both internally, and with established pattern from other operators.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed underscores on test list constants, should now be consistent usage

@documentdb-triage-tool documentdb-triage-tool Bot added compatibility test Compatibility test related enhancement New feature or request labels Jul 14, 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 P2 · Effort XL · Status Needs Review
Confidence: 0.90 (mixed)

Reasoning

component from path globs (test-coverage, test-framework); effort from diff stats (5654+0 LOC, 36 files); LLM: Adds compatibility test coverage for four type conversion aggregation operators ($toInt, $toDouble, $toLong, $toDecimal), expanding test suite for tracked issues.

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.

Signed-off-by: PatersonProjects <keldonhoff@gmail.com>
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