-
Notifications
You must be signed in to change notification settings - Fork 34
Add indexes type tests for wildcard #679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vic-tsang
wants to merge
6
commits into
documentdb:main
Choose a base branch
from
vic-tsang:indexes/types/wildcard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dfb7fcd
Add indexes type tests for wildcard
8cb12af
added missing test cases based on reviewer comments
75d9592
fix binary test case failure
ed4bcea
address reviewer comments
6d0b209
split query file into 3 different files
e9a3151
update doc string
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
717 changes: 717 additions & 0 deletions
717
...mentdb_tests/compatibility/tests/core/indexes/types/wildcard/test_wildcard_basic_query.py
Large diffs are not rendered by default.
Oops, something went wrong.
92 changes: 92 additions & 0 deletions
92
...db_tests/compatibility/tests/core/indexes/types/wildcard/test_wildcard_bson_validation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
|
|
||
| WILDCARD_BSON_PARAMS = [ | ||
|
vic-tsang marked this conversation as resolved.
|
||
| BsonTypeTestCase( | ||
|
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}") | ||
114 changes: 114 additions & 0 deletions
114
documentdb_tests/compatibility/tests/core/indexes/types/wildcard/test_wildcard_commands.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| ) |
86 changes: 86 additions & 0 deletions
86
...tdb_tests/compatibility/tests/core/indexes/types/wildcard/test_wildcard_compound_query.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.