Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
"""Canonical BSON type coercion matrix for validate boolean parameters.

Uses 'full' as the representative parameter. All boolean parameters
(full, metadata, checkBSONConformance, repair, fixMultikey) share the
same coercion logic; other per-parameter files contain only wiring tests.
"""

from __future__ import annotations

from datetime import datetime, timezone

import pytest
from bson import Binary, Code, Decimal128, Int64, MaxKey, MinKey, ObjectId, Regex, Timestamp

from documentdb_tests.compatibility.tests.system.diagnostic.utils.diagnostic_test_case import (
DiagnosticTestCase,
)
from documentdb_tests.framework.assertions import assertProperties
from documentdb_tests.framework.executor import execute_command
from documentdb_tests.framework.parametrize import pytest_params
from documentdb_tests.framework.property_checks import Eq

# Property [Type Coercion]: validate accepts all BSON types for the full parameter via coercion.
ACCEPTED_TYPE_TESTS: list[DiagnosticTestCase] = [

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.

🟠 [Major] The per-parameter coercion files are enormous and partly redundant against §20's canonical matrix. Five files (full, metadata, checkBSONConformance, repair, fixMultikey) each test the same ~21-type coercion matrix (~105 near-identical cases). Per TEST_COVERAGE.md §20, boolean-param coercion should mirror the canonical matrix (bypassDocumentValidation), but it
doesn't require re-testing all 21 BSON types five times for one command — the coercion logic is shared across these boolean params. Consider collapsing to one representative coercion matrix plus per-param confirmation that each is wired to it (a few cases each). As written this is a lot of duplicated surface for one behavior; TEST_FORMAT.md "don't ask for exhaustive
BSON matrix on simple shared params" cuts toward trimming.

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 the exhaustive tests

DiagnosticTestCase(
"bool_true",
command={"full": True},
checks={"ok": Eq(1.0)},
msg="full should accept bool true",
),
DiagnosticTestCase(
"bool_false",
command={"full": False},
checks={"ok": Eq(1.0)},
msg="full should accept bool false",
),
DiagnosticTestCase(
"int32_1",
command={"full": 1},
checks={"ok": Eq(1.0)},
msg="full should accept int32 1 (coerces to true)",
),
DiagnosticTestCase(
"int32_0",
command={"full": 0},
checks={"ok": Eq(1.0)},
msg="full should accept int32 0 (coerces to false)",
),
DiagnosticTestCase(
"double_1",
command={"full": 1.0},
checks={"ok": Eq(1.0)},
msg="full should accept double 1.0 (coerces to true)",
),
DiagnosticTestCase(
"double_0",
command={"full": 0.0},
checks={"ok": Eq(1.0)},
msg="full should accept double 0.0 (coerces to false)",
),
DiagnosticTestCase(
"int64_1",
command={"full": Int64(1)},
checks={"ok": Eq(1.0)},
msg="full should accept Int64(1) (coerces to true)",
),
DiagnosticTestCase(
"int64_0",
command={"full": Int64(0)},
checks={"ok": Eq(1.0)},
msg="full should accept Int64(0) (coerces to false)",
),
DiagnosticTestCase(
"decimal128_1",
command={"full": Decimal128("1")},
checks={"ok": Eq(1.0)},
msg="full should accept Decimal128('1') (coerces to true)",
),
DiagnosticTestCase(
"decimal128_0",
command={"full": Decimal128("0")},
checks={"ok": Eq(1.0)},
msg="full should accept Decimal128('0') (coerces to false)",
),
DiagnosticTestCase(
"null",
command={"full": None},
checks={"ok": Eq(1.0)},
msg="full should accept null (treated as omitted/false)",
),
DiagnosticTestCase(
"string",
command={"full": "true"},
checks={"ok": Eq(1.0)},
msg="full should accept string (coerces to truthy)",
),
DiagnosticTestCase(
"object",
command={"full": {}},
checks={"ok": Eq(1.0)},
msg="full should accept object (coerces to truthy)",
),
DiagnosticTestCase(
"array",
command={"full": []},
checks={"ok": Eq(1.0)},
msg="full should accept array (coerces to truthy)",
),
DiagnosticTestCase(
"binary",
command={"full": Binary(b"")},
checks={"ok": Eq(1.0)},
msg="full should accept Binary (coerces to truthy)",
),
DiagnosticTestCase(
"objectid",
command={"full": ObjectId()},
checks={"ok": Eq(1.0)},
msg="full should accept ObjectId (coerces to truthy)",
),
DiagnosticTestCase(
"datetime",
command={"full": datetime(2024, 1, 1, tzinfo=timezone.utc)},
checks={"ok": Eq(1.0)},
msg="full should accept datetime (coerces to truthy)",
),
DiagnosticTestCase(
"regex",
command={"full": Regex(".*")},
checks={"ok": Eq(1.0)},
msg="full should accept Regex (coerces to truthy)",
),
DiagnosticTestCase(
"timestamp",
command={"full": Timestamp(0, 0)},
checks={"ok": Eq(1.0)},
msg="full should accept Timestamp (coerces to truthy)",
),
DiagnosticTestCase(
"code",
command={"full": Code("function(){}")},
checks={"ok": Eq(1.0)},
msg="full should accept JavaScript Code (coerces to truthy)",
),
DiagnosticTestCase(
"minkey",
command={"full": MinKey()},
checks={"ok": Eq(1.0)},
msg="full should accept MinKey (coerces to truthy)",
),
DiagnosticTestCase(
"maxkey",
command={"full": MaxKey()},
checks={"ok": Eq(1.0)},
msg="full should accept MaxKey (coerces to truthy)",
),
]


@pytest.mark.parametrize("test", pytest_params(ACCEPTED_TYPE_TESTS))
def test_validate_full_accepted_types(collection, test):
"""Test that validate accepts all BSON types for the full parameter."""
collection.insert_one({"_id": 1})
result = execute_command(
collection,
{"validate": collection.name, **test.command},
)
assertProperties(result, test.checks, msg=test.msg, raw_res=True)
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"""Wiring tests for validate command 'checkBSONConformance' parameter type coercion.

Confirms checkBSONConformance uses the same boolean coercion as other params.
The full BSON type matrix is in test_validate_bool_param_coercion.py.
"""

from __future__ import annotations

import pytest

from documentdb_tests.compatibility.tests.system.diagnostic.utils.diagnostic_test_case import (
DiagnosticTestCase,
)
from documentdb_tests.framework.assertions import assertProperties
from documentdb_tests.framework.executor import execute_command
from documentdb_tests.framework.parametrize import pytest_params
from documentdb_tests.framework.property_checks import Eq

# Property [Type Coercion Wiring]: checkBSONConformance delegates to shared boolean coercion.
WIRING_TESTS: list[DiagnosticTestCase] = [
DiagnosticTestCase(
"bool_true",
command={"checkBSONConformance": True},
checks={"ok": Eq(1.0)},
msg="checkBSONConformance should accept bool true",
),
DiagnosticTestCase(
"int32_0",
command={"checkBSONConformance": 0},
checks={"ok": Eq(1.0)},
msg="checkBSONConformance should accept int32 0 (coerces to false)",
),
DiagnosticTestCase(
"string",
command={"checkBSONConformance": "true"},
checks={"ok": Eq(1.0)},
msg="checkBSONConformance should accept string (coerces to truthy)",
),
]


@pytest.mark.parametrize("test", pytest_params(WIRING_TESTS))
def test_validate_checkBSONConformance_accepted_types(collection, test):
"""Test that checkBSONConformance uses shared boolean coercion."""
collection.insert_one({"_id": 1})
result = execute_command(
collection,
{"validate": collection.name, **test.command},
)
assertProperties(result, test.checks, msg=test.msg, raw_res=True)
Loading
Loading