-
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
1
commit 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
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.
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 = [ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we have case , BSON type validation of values inside wildcardProjection (e.g. {"a": "yes"} instead of 0/1), may be in error case ? |
||
| BsonTypeTestCase( | ||
| 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}") | ||
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.