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
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
"""Tests for distinct command collation field syntax validation."""

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.core.collections.commands.utils.command_test_case import (
CommandContext,
CommandTestCase,
)
from documentdb_tests.framework.assertions import assertResult
from documentdb_tests.framework.error_codes import TYPE_MISMATCH_ERROR
from documentdb_tests.framework.executor import execute_command
from documentdb_tests.framework.parametrize import pytest_params

# Property [Collation Acceptance]: the collation field accepts null and
# a document type.
DISTINCT_COLLATION_ACCEPTANCE_TESTS: list[CommandTestCase] = [
CommandTestCase(
"collation_null",
docs=[{"_id": 1, "x": "a"}],
command=lambda ctx: {"distinct": ctx.collection, "key": "x", "collation": None},
expected={"values": ["a"], "ok": 1.0},
msg="distinct should accept null collation",
),
CommandTestCase(
"collation_empty_doc",
docs=[{"_id": 1, "x": "a"}],
command=lambda ctx: {"distinct": ctx.collection, "key": "x", "collation": {}},
expected={"values": ["a"], "ok": 1.0},
msg="distinct should accept empty document collation",
),
]

# Property [Collation Type Rejection]: all non-document, non-null BSON types
# for the collation field produce a type mismatch error.
DISTINCT_COLLATION_TYPE_REJECTION_TESTS: list[CommandTestCase] = [
CommandTestCase(
f"collation_type_{tid}",
docs=[{"_id": 1, "x": "a"}],
command=lambda ctx, v=val: {
"distinct": ctx.collection,
"key": "x",
"collation": v,
},
error_code=TYPE_MISMATCH_ERROR,
msg=f"distinct should reject {tid} as collation",
)
for tid, val in [
("string", "en"),
("int32", 42),
("int64", Int64(1)),
("double", 3.14),
("decimal128", Decimal128("1")),
("bool", True),
("array", [1, 2]),
("objectid", ObjectId()),
("datetime", datetime(2024, 1, 1, tzinfo=timezone.utc)),
("timestamp", Timestamp(1, 1)),
("binary", Binary(b"data")),
("regex", Regex("abc")),
("code", Code("function(){}")),
("code_with_scope", Code("function(){}", {"x": 1})),
("minkey", MinKey()),
("maxkey", MaxKey()),
]
]

DISTINCT_COLLATION_TESTS: list[CommandTestCase] = (
DISTINCT_COLLATION_ACCEPTANCE_TESTS + DISTINCT_COLLATION_TYPE_REJECTION_TESTS
)


@pytest.mark.parametrize("test", pytest_params(DISTINCT_COLLATION_TESTS))
def test_distinct_collation(database_client, collection, test):
"""Test distinct command collation field syntax validation."""
collection = test.prepare(database_client, collection)
ctx = CommandContext.from_collection(collection)
result = execute_command(collection, test.build_command(ctx))
assertResult(
result,
expected=test.build_expected(ctx),
error_code=test.error_code,
msg=test.msg,
raw_res=True,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""Tests for distinct command collection type acceptance."""

from __future__ import annotations

from datetime import datetime, timezone

import pytest

from documentdb_tests.compatibility.tests.core.collections.commands.utils.command_test_case import (
CommandContext,
CommandTestCase,
)
from documentdb_tests.framework.assertions import assertResult
from documentdb_tests.framework.executor import execute_command
from documentdb_tests.framework.parametrize import pytest_params
from documentdb_tests.framework.target_collection import (
CappedCollection,
ClusteredCollection,
TimeseriesCollection,
ViewCollection,
)

# Property [Collection Type Acceptance]: distinct produces correct results
# regardless of the underlying collection type.
DISTINCT_COLLECTION_TYPE_TESTS: list[CommandTestCase] = [
CommandTestCase(
"regular",
docs=[{"_id": i, "x": i % 3} for i in range(5)],
command=lambda ctx: {"distinct": ctx.collection, "key": "x"},
expected={"values": [0, 1, 2], "ok": 1.0},
ignore_order_in=["values"],
msg="distinct should work on a regular collection",
),
CommandTestCase(
"view",
target_collection=ViewCollection(
options={"pipeline": [{"$match": {"x": {"$gte": 1}}}]},
suffix="_view",
),
docs=[{"_id": i, "x": i % 3} for i in range(5)],
command=lambda ctx: {"distinct": ctx.collection, "key": "x"},
expected={"values": [1, 2], "ok": 1},
ignore_order_in=["values"],
msg="distinct on view should only see documents passing the view pipeline",
),
CommandTestCase(
"capped",
target_collection=CappedCollection(size=100_000),
docs=[{"_id": i, "x": i % 3} for i in range(5)],
command=lambda ctx: {"distinct": ctx.collection, "key": "x"},
expected={"values": [0, 1, 2], "ok": 1.0},
ignore_order_in=["values"],
msg="distinct should work on a capped collection",
),
CommandTestCase(
"timeseries",
target_collection=TimeseriesCollection(),
docs=[
{"ts": datetime(2024, 1, i, tzinfo=timezone.utc), "meta": "a", "x": i % 3}
for i in range(1, 6)
],
command=lambda ctx: {"distinct": ctx.collection, "key": "x"},
expected={"values": [0, 1, 2], "ok": 1},
ignore_order_in=["values"],
msg="distinct should work on a timeseries collection",
),
CommandTestCase(
"clustered",
target_collection=ClusteredCollection(),
docs=[{"_id": i, "x": i % 3} for i in range(5)],
command=lambda ctx: {"distinct": ctx.collection, "key": "x"},
expected={"values": [0, 1, 2], "ok": 1.0},
ignore_order_in=["values"],
msg="distinct should work on a clustered collection",
),
]


@pytest.mark.parametrize("test", pytest_params(DISTINCT_COLLECTION_TYPE_TESTS))
def test_distinct_collection_types(database_client, collection, test):
"""Test distinct command collection type acceptance."""
collection = test.prepare(database_client, collection)
ctx = CommandContext.from_collection(collection)
result = execute_command(collection, test.build_command(ctx))
assertResult(
result,
expected=test.build_expected(ctx),
error_code=test.error_code,
msg=test.msg,
raw_res=True,
ignore_order_in=test.ignore_order_in,
)
Loading
Loading