Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""Tests for wildcard index BSON type validation."""

import pytest
from bson import Int64
from bson.decimal128 import Decimal128

from documentdb_tests.framework.assertions import assertFailureCode, assertNotError
from documentdb_tests.framework.bson_type_validator import (
BsonType,
BsonTypeTestCase,
generate_bson_acceptance_test_cases,
generate_bson_rejection_test_cases,
)
from documentdb_tests.framework.error_codes import (
CANNOT_CREATE_INDEX_ERROR,
TYPE_MISMATCH_ERROR,
)
from documentdb_tests.framework.executor import execute_command

pytestmark = pytest.mark.index


Comment thread
vic-tsang marked this conversation as resolved.
WILDCARD_BSON_PARAMS = [
Comment thread
vic-tsang marked this conversation as resolved.
BsonTypeTestCase(
Comment thread
vic-tsang marked this conversation as resolved.
id="wildcardProjection",
msg="wildcardProjection should reject non-document types",
keyword="wildcardProjection",
valid_types=[BsonType.OBJECT],
default_error_code=TYPE_MISMATCH_ERROR,
valid_inputs={BsonType.OBJECT: {"a": 1}},
),
BsonTypeTestCase(
id="wildcard_key_value",
msg="wildcard index key value should reject non-numeric types",
keyword="key_value",
valid_types=[BsonType.DOUBLE, BsonType.INT, BsonType.LONG, BsonType.DECIMAL],
default_error_code=CANNOT_CREATE_INDEX_ERROR,
# STRING is skipped here because some strings name valid special index
# types (e.g. "2d", "2dsphere", "hashed", "wildcard"); string key values
# are covered explicitly in test_wildcard_errors.py.
skip_rejection_types=[BsonType.STRING],
valid_inputs={
BsonType.DOUBLE: 1.0,
BsonType.INT: 1,
BsonType.LONG: Int64(1),
BsonType.DECIMAL: Decimal128("1"),
},
),
]

REJECTION_CASES = generate_bson_rejection_test_cases(WILDCARD_BSON_PARAMS)
ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(WILDCARD_BSON_PARAMS)


def _build_command(collection, spec, sample_value):
"""Build the appropriate createIndexes command for the given spec and sample value."""
if spec.id == "wildcardProjection":
return execute_command(
collection,
{
"createIndexes": collection.name,
"indexes": [
{
"key": {"$**": 1},
"name": "wc_bson_test",
"wildcardProjection": sample_value,
}
],
},
)
else:
return execute_command(
collection,
{
"createIndexes": collection.name,
"indexes": [{"key": {"$**": sample_value}, "name": "wc_bson_test"}],
},
)


@pytest.mark.parametrize("bson_type,sample_value,spec", REJECTION_CASES)
def test_wildcard_rejects_invalid_bson_type(collection, bson_type, sample_value, spec):
"""Test wildcard index options reject invalid BSON types."""
result = _build_command(collection, spec, sample_value)
assertFailureCode(result, spec.expected_code(bson_type), msg=spec.msg)


@pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES)
def test_wildcard_accepts_valid_bson_type(collection, bson_type, sample_value, spec):
"""Test wildcard index options accept valid BSON types."""
result = _build_command(collection, spec, sample_value)
assertNotError(result, msg=f"{spec.id} should accept {bson_type.value}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
"""Tests for wildcard index usage across CRUD and write commands: count, update, findAndModify,
and delete."""

import pytest

from documentdb_tests.compatibility.tests.core.indexes.commands.utils.index_test_case import (
IndexTestCase,
)
from documentdb_tests.framework.assertions import assertSuccess, assertSuccessPartial
from documentdb_tests.framework.executor import execute_command
from documentdb_tests.framework.parametrize import pytest_params

pytestmark = pytest.mark.index


COMMAND_WIRING_TESTS: list[IndexTestCase] = [
IndexTestCase(
id="count",
doc=({"_id": 1, "a": 1}, {"_id": 2, "a": None}, {"_id": 3}, {"_id": 4, "a": 3}),
input="count",
command_options={"query": {"a": {"$exists": True}}, "hint": "wc_all"},
expected={"n": 3, "ok": 1.0},
msg="count hinted to the wildcard index excludes docs missing the field",
),
IndexTestCase(
id="update",
doc=({"_id": 1, "a": 1}, {"_id": 2, "a": 2}),
input="update",
command_options={"updates": [{"q": {"a": 2}, "u": {"$set": {"a": 99}}, "hint": "wc_all"}]},
expected={"n": 1, "nModified": 1, "ok": 1.0},
msg="update hinted to the wildcard index modifies the targeted document",
),
IndexTestCase(
id="upsert",
doc=(),
input="update",
command_options={
"updates": [
{"q": {"a": 42}, "u": {"$set": {"a": 42}}, "upsert": True, "hint": "wc_all"}
]
},
expected={"n": 1, "upserted": [{"index": 0}], "ok": 1.0},
msg="upsert hinted to the wildcard index inserts the targeted document",
),
IndexTestCase(
id="findAndModify_update",
doc=({"_id": 1, "a": 1}, {"_id": 2, "a": 2}),
input="findAndModify",
command_options={
"query": {"a": 2},
"update": {"$set": {"a": 99}},
"new": True,
"hint": "wc_all",
},
expected={"value": {"_id": 2, "a": 99}, "ok": 1.0},
msg="findAndModify hinted to the wildcard index updates and returns the document",
),
IndexTestCase(
id="findAndModify_remove",
doc=({"_id": 1, "a": 1}, {"_id": 2, "a": 2}),
input="findAndModify",
command_options={"query": {"a": 2}, "remove": True, "hint": "wc_all"},
expected={"value": {"_id": 2, "a": 2}, "ok": 1.0},
msg="findAndModify remove hinted to the wildcard index deletes and returns the document",
),
IndexTestCase(
id="delete",
doc=({"_id": 1, "a": 1}, {"_id": 2, "a": 2}),
input="delete",
command_options={"deletes": [{"q": {"a": 2}, "limit": 1, "hint": "wc_all"}]},
expected={"n": 1, "ok": 1.0},
msg="delete hinted to the wildcard index removes the targeted document",
),
]


@pytest.mark.parametrize("test", pytest_params(COMMAND_WIRING_TESTS))
def test_wildcard_command_wiring(collection, test):
"""Verify each command that accepts a hint can use a wildcard index."""
execute_command(
collection,
{"createIndexes": collection.name, "indexes": [{"key": {"$**": 1}, "name": "wc_all"}]},
)
if test.doc:
collection.insert_many(list(test.doc))
cmd = {test.input: collection.name, **test.command_options}
result = execute_command(collection, cmd)
assertSuccessPartial(result, test.expected, msg=test.msg)


def test_wildcard_upsert_indexed_queryable(collection):
"""A document inserted via a hinted upsert is queryable via the wildcard index."""
execute_command(
collection,
{"createIndexes": collection.name, "indexes": [{"key": {"$**": 1}, "name": "wc_all"}]},
)
execute_command(
collection,
{
"update": collection.name,
"updates": [
{"q": {"a": 42}, "u": {"$set": {"a": 42}}, "upsert": True, "hint": "wc_all"}
],
},
)
result = execute_command(
collection, {"find": collection.name, "filter": {"a": 42}, "hint": "wc_all"}
)
assertSuccess(
result,
[{"a": 42}],
transform=lambda batch: [{"a": d["a"]} for d in batch],
msg="Upserted document is queryable via wildcard index",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"""Tests for compound wildcard index queries: filtering on the non-wildcard prefix field
together with a wildcard-covered field."""

import pytest

from documentdb_tests.compatibility.tests.core.indexes.commands.utils.index_test_case import (
IndexQueryTestCase,
)
from documentdb_tests.framework.assertions import assertSuccess
from documentdb_tests.framework.executor import execute_command
from documentdb_tests.framework.parametrize import pytest_params

pytestmark = pytest.mark.index

COMPOUND_WILDCARD_QUERY_TESTS: list[IndexQueryTestCase] = [
IndexQueryTestCase(
id="prefix_and_wildcard_field_nonwildcard_first",
indexes=({"key": {"a": 1, "$**": 1}, "name": "cwi_a_wc", "wildcardProjection": {"a": 0}},),
doc=(
{"_id": 1, "a": 1, "b": 10},
{"_id": 2, "a": 1, "b": 20},
{"_id": 3, "a": 2, "b": 10},
),
filter={"a": 1, "b": 10},
hint="cwi_a_wc",
expected=[{"_id": 1, "a": 1, "b": 10}],
msg="Compound wildcard (non-wildcard first) serves a prefix + wildcard-field query",
),
IndexQueryTestCase(
id="prefix_and_wildcard_field_wildcard_first",
indexes=({"key": {"$**": 1, "a": 1}, "name": "cwi_wc_a", "wildcardProjection": {"a": 0}},),
doc=(
{"_id": 1, "a": 1, "b": 10},
{"_id": 2, "a": 1, "b": 20},
{"_id": 3, "a": 2, "b": 10},
),
filter={"a": 1, "b": 10},
hint="cwi_wc_a",
expected=[{"_id": 1, "a": 1, "b": 10}],
msg="Compound wildcard (wildcard first) serves a prefix + wildcard-field query",
),
IndexQueryTestCase(
id="prefix_equality_wildcard_range",
indexes=({"key": {"a": 1, "$**": 1}, "name": "cwi_a_wc", "wildcardProjection": {"a": 0}},),
doc=(
{"_id": 1, "a": 1, "b": 5},
{"_id": 2, "a": 1, "b": 15},
{"_id": 3, "a": 2, "b": 15},
),
filter={"a": 1, "b": {"$gte": 10}},
hint="cwi_a_wc",
sort={"_id": 1},
expected=[{"_id": 2, "a": 1, "b": 15}],
msg="Compound wildcard serves prefix equality plus a range on the wildcard field",
),
IndexQueryTestCase(
id="prefix_only_query",
indexes=({"key": {"a": 1, "$**": 1}, "name": "cwi_a_wc", "wildcardProjection": {"a": 0}},),
doc=(
{"_id": 1, "a": 1, "b": 10},
{"_id": 2, "a": 1, "b": 20},
{"_id": 3, "a": 2, "b": 30},
),
filter={"a": 1},
hint="cwi_a_wc",
sort={"_id": 1},
expected=[{"_id": 1, "a": 1, "b": 10}, {"_id": 2, "a": 1, "b": 20}],
msg="Compound wildcard serves a query on the non-wildcard prefix field alone",
),
]


@pytest.mark.parametrize("test", pytest_params(COMPOUND_WILDCARD_QUERY_TESTS))
def test_compound_wildcard_query(collection, test):
"""Verify a compound wildcard index serves a query filtering on the non-wildcard prefix
field together with a wildcard-covered field."""
execute_command(
collection,
{"createIndexes": collection.name, "indexes": list(test.indexes)},
)
collection.insert_many(list(test.doc))
result = execute_command(
collection,
{"find": collection.name, "filter": test.filter, "hint": test.hint},
)
assertSuccess(result, test.expected, msg=test.msg)
Loading
Loading