-
Notifications
You must be signed in to change notification settings - Fork 32
Added update tests for $unset #570
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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.
101 changes: 101 additions & 0 deletions
101
...sts/compatibility/tests/core/operator/update/fields/unset/test_unset_argument_handling.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,101 @@ | ||
| """ | ||
| Tests for $unset update operator - argument handling. | ||
|
|
||
| Covers empty operand, empty operand with upsert, single field, multiple fields, | ||
| numeric-string field names, partial field removal, and mixed existing/missing fields. | ||
| """ | ||
|
|
||
| import pytest | ||
|
|
||
| from documentdb_tests.compatibility.tests.core.operator.update.utils.update_test_case import ( | ||
| UpdateTestCase, | ||
| ) | ||
| from documentdb_tests.framework.assertions import assertSuccess, assertSuccessPartial | ||
| from documentdb_tests.framework.executor import execute_command | ||
| from documentdb_tests.framework.parametrize import pytest_params | ||
|
|
||
| UNSET_ARGUMENT_SUCCESS_TESTS: list[UpdateTestCase] = [ | ||
| UpdateTestCase( | ||
| id="single_field", | ||
| setup_docs=[{"_id": 1, "a": 1, "b": 2}], | ||
| query={"_id": 1}, | ||
| update={"$unset": {"a": ""}}, | ||
| expected=[{"_id": 1, "b": 2}], | ||
| msg="Should remove single field", | ||
| ), | ||
| UpdateTestCase( | ||
| id="multiple_fields", | ||
| setup_docs=[{"_id": 1, "a": 1, "b": 2, "c": 3}], | ||
| query={"_id": 1}, | ||
| update={"$unset": {"a": "", "b": "", "c": ""}}, | ||
| expected=[{"_id": 1}], | ||
| msg="Should remove all specified fields", | ||
| ), | ||
| UpdateTestCase( | ||
| id="numeric_string_field_names", | ||
| setup_docs=[{"_id": 1, "10": "ten", "2": "two", "1": "one"}], | ||
| query={"_id": 1}, | ||
| update={"$unset": {"10": "", "2": "", "1": ""}}, | ||
| expected=[{"_id": 1}], | ||
| msg="Should remove fields with numeric-string names", | ||
| ), | ||
| UpdateTestCase( | ||
| id="subset_of_fields", | ||
| setup_docs=[{"_id": 1, "a": 1, "b": 2, "c": 3, "d": 4}], | ||
| query={"_id": 1}, | ||
| update={"$unset": {"a": "", "c": ""}}, | ||
| expected=[{"_id": 1, "b": 2, "d": 4}], | ||
| msg="Should remove only specified fields, leaving others intact", | ||
| ), | ||
| UpdateTestCase( | ||
| id="existing_and_missing_field", | ||
| setup_docs=[{"_id": 1, "a": 1, "b": 2}], | ||
| query={"_id": 1}, | ||
| update={"$unset": {"a": "", "missing": ""}}, | ||
| expected=[{"_id": 1, "b": 2}], | ||
| msg="Should remove existing field and silently ignore missing field", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test", pytest_params(UNSET_ARGUMENT_SUCCESS_TESTS)) | ||
| def test_unset_argument_success(collection, test): | ||
| """Test $unset argument handling - success cases.""" | ||
| collection.insert_many(test.setup_docs) | ||
| execute_command( | ||
| collection, | ||
| { | ||
| "update": collection.name, | ||
| "updates": [{"q": test.query, "u": test.update}], | ||
| }, | ||
| ) | ||
| result = execute_command(collection, {"find": collection.name, "filter": {"_id": 1}}) | ||
| assertSuccess(result, test.expected, msg=test.msg) | ||
|
|
||
|
|
||
| def test_unset_empty_operand_noop(collection): | ||
| """Test $unset with empty operand {} is a no-op.""" | ||
| collection.insert_one({"_id": 1, "a": 1}) | ||
| result = execute_command( | ||
| collection, | ||
| { | ||
| "update": collection.name, | ||
| "updates": [{"q": {"_id": 1}, "u": {"$unset": {}}}], | ||
| }, | ||
| ) | ||
| assertSuccessPartial( | ||
| result, {"n": 1, "nModified": 0, "ok": 1.0}, msg="Empty $unset should be no-op" | ||
| ) | ||
|
|
||
|
|
||
| def test_unset_empty_operand_upsert_creates_from_query(collection): | ||
| """Test upsert with empty $unset creates doc from query.""" | ||
| execute_command( | ||
| collection, | ||
| { | ||
| "update": collection.name, | ||
| "updates": [{"q": {"_id": 1}, "u": {"$unset": {}}, "upsert": True}], | ||
| }, | ||
| ) | ||
| result = execute_command(collection, {"find": collection.name, "filter": {"_id": 1}}) | ||
| assertSuccess(result, [{"_id": 1}], msg="Upsert with empty $unset should create doc from query") | ||
147 changes: 147 additions & 0 deletions
147
..._tests/compatibility/tests/core/operator/update/fields/unset/test_unset_array_behavior.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,147 @@ | ||
| """ | ||
| Tests for $unset update operator - array element behavior. | ||
|
|
||
| Covers null replacement on array elements, positional operators, and arrayFilters. | ||
| """ | ||
|
|
||
| import pytest | ||
|
|
||
| from documentdb_tests.compatibility.tests.core.operator.update.utils.update_test_case import ( | ||
| UpdateTestCase, | ||
| ) | ||
| from documentdb_tests.framework.assertions import assertSuccess | ||
| from documentdb_tests.framework.executor import execute_command | ||
| from documentdb_tests.framework.parametrize import pytest_params | ||
|
|
||
| UNSET_ARRAY_TESTS: list[UpdateTestCase] = [ | ||
| UpdateTestCase( | ||
| id="first_element", | ||
| setup_docs=[{"_id": 1, "arr": [1, 2, 3]}], | ||
| query={"_id": 1}, | ||
| update={"$unset": {"arr.0": ""}}, | ||
| expected=[{"_id": 1, "arr": [None, 2, 3]}], | ||
| msg="$unset first element should replace with null", | ||
| ), | ||
| UpdateTestCase( | ||
| id="middle_element", | ||
| setup_docs=[{"_id": 1, "arr": [1, 2, 3]}], | ||
| query={"_id": 1}, | ||
| update={"$unset": {"arr.1": ""}}, | ||
| expected=[{"_id": 1, "arr": [1, None, 3]}], | ||
| msg="$unset middle element should replace with null", | ||
| ), | ||
| UpdateTestCase( | ||
| id="last_element", | ||
| setup_docs=[{"_id": 1, "arr": [1, 2, 3]}], | ||
| query={"_id": 1}, | ||
| update={"$unset": {"arr.2": ""}}, | ||
| expected=[{"_id": 1, "arr": [1, 2, None]}], | ||
| msg="$unset last element should replace with null", | ||
| ), | ||
| UpdateTestCase( | ||
| id="out_of_bounds", | ||
| setup_docs=[{"_id": 1, "arr": [1, 2, 3]}], | ||
| query={"_id": 1}, | ||
| update={"$unset": {"arr.5": ""}}, | ||
| expected=[{"_id": 1, "arr": [1, 2, 3]}], | ||
| msg="$unset out-of-bounds index should be no-op", | ||
| ), | ||
| UpdateTestCase( | ||
| id="embedded_field_in_array", | ||
| setup_docs=[{"_id": 1, "arr": [{"field": 1, "other": 2}]}], | ||
| query={"_id": 1}, | ||
| update={"$unset": {"arr.0.field": ""}}, | ||
| expected=[{"_id": 1, "arr": [{"other": 2}]}], | ||
| msg="$unset embedded field in array element should remove only that field", | ||
| ), | ||
| UpdateTestCase( | ||
| id="positional_operator", | ||
| setup_docs=[{"_id": 1, "arr": [1, 2, 3]}], | ||
| query={"_id": 1, "arr": 2}, | ||
| update={"$unset": {"arr.$": ""}}, | ||
| expected=[{"_id": 1, "arr": [1, None, 3]}], | ||
| msg="$ positional should set matching element to null", | ||
| ), | ||
| UpdateTestCase( | ||
| id="all_positional_field", | ||
| setup_docs=[{"_id": 1, "arr": [{"field": 1, "keep": "a"}, {"field": 2, "keep": "b"}]}], | ||
| query={"_id": 1}, | ||
| update={"$unset": {"arr.$[].field": ""}}, | ||
| expected=[{"_id": 1, "arr": [{"keep": "a"}, {"keep": "b"}]}], | ||
| msg="$[] should remove field from all elements", | ||
| ), | ||
| UpdateTestCase( | ||
| id="multiple_elements", | ||
| setup_docs=[{"_id": 1, "arr": [1, 2, 3, 4, 5]}], | ||
| query={"_id": 1}, | ||
| update={"$unset": {"arr.0": "", "arr.2": "", "arr.4": ""}}, | ||
| expected=[{"_id": 1, "arr": [None, 2, None, 4, None]}], | ||
| msg="$unset multiple elements in same update should null each", | ||
| ), | ||
| UpdateTestCase( | ||
| id="nested_array_element", | ||
| setup_docs=[{"_id": 1, "arr": [[10, 20, 30], [40, 50]]}], | ||
| query={"_id": 1}, | ||
| update={"$unset": {"arr.0.1": ""}}, | ||
| expected=[{"_id": 1, "arr": [[10, None, 30], [40, 50]]}], | ||
| msg="$unset on nested array element should null inner element", | ||
| ), | ||
| UpdateTestCase( | ||
| id="negative_index", | ||
| setup_docs=[{"_id": 1, "arr": [1, 2, 3]}], | ||
| query={"_id": 1}, | ||
| update={"$unset": {"arr.-1": ""}}, | ||
| expected=[{"_id": 1, "arr": [1, 2, 3]}], | ||
| msg="$unset with negative index should be no-op", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test", pytest_params(UNSET_ARRAY_TESTS)) | ||
| def test_unset_array_behavior(collection, test): | ||
| """Test $unset on array elements.""" | ||
| collection.insert_many(test.setup_docs) | ||
| execute_command( | ||
| collection, | ||
| { | ||
| "update": collection.name, | ||
| "updates": [{"q": test.query, "u": test.update}], | ||
| }, | ||
| ) | ||
| result = execute_command(collection, {"find": collection.name, "filter": {"_id": 1}}) | ||
| assertSuccess(result, test.expected, msg=test.msg) | ||
|
|
||
|
|
||
| UNSET_ARRAY_FILTER_TESTS: list[UpdateTestCase] = [ | ||
| UpdateTestCase( | ||
| id="filtered_positional_removes_field", | ||
| setup_docs=[ | ||
| {"_id": 1, "arr": [{"x": 1, "y": "a"}, {"x": 5, "y": "b"}, {"x": 3, "y": "c"}]} | ||
| ], | ||
| query={"_id": 1}, | ||
| update={"$unset": {"arr.$[elem].y": ""}}, | ||
| expected=[{"_id": 1, "arr": [{"x": 1, "y": "a"}, {"x": 5}, {"x": 3}]}], | ||
| msg="$unset with arrayFilters should remove field from matching elements only", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("test", pytest_params(UNSET_ARRAY_FILTER_TESTS)) | ||
| def test_unset_with_array_filters(collection, test): | ||
| """Test $unset with arrayFilters.""" | ||
| collection.insert_many(test.setup_docs) | ||
| execute_command( | ||
| collection, | ||
| { | ||
| "update": collection.name, | ||
| "updates": [ | ||
| { | ||
| "q": test.query, | ||
| "u": test.update, | ||
| "arrayFilters": [{"elem.x": {"$gt": 2}}], | ||
| } | ||
| ], | ||
| }, | ||
| ) | ||
| result = execute_command(collection, {"find": collection.name, "filter": {"_id": 1}}) | ||
| assertSuccess(result, test.expected, msg=test.msg) |
47 changes: 47 additions & 0 deletions
47
.../compatibility/tests/core/operator/update/fields/unset/test_unset_bson_type_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,47 @@ | ||
| """ | ||
| Tests for $unset update operator - BSON type validation. | ||
|
|
||
| Verifies that $unset accepts all BSON types as the field value (value is ignored). | ||
| """ | ||
|
|
||
| import pytest | ||
| from bson import Binary | ||
|
|
||
| from documentdb_tests.framework.assertions import assertSuccess | ||
| from documentdb_tests.framework.bson_type_validator import ( | ||
| BsonType, | ||
| BsonTypeTestCase, | ||
| generate_bson_acceptance_test_cases, | ||
| ) | ||
| from documentdb_tests.framework.executor import execute_command | ||
|
|
||
| # $unset ignores the field value — any BSON type is accepted, so all types are valid. | ||
| UNSET_BSON_PARAMS = [ | ||
| BsonTypeTestCase( | ||
| id="unset_field_value", | ||
| msg="$unset field value should accept all BSON types (value is ignored)", | ||
| keyword="a", | ||
| valid_types=list(BsonType), | ||
| # Driver returns raw bytes when using Binary subtype 0 | ||
| valid_inputs={BsonType.BIN_DATA: Binary(b"\x00\x01\x02", 128)}, | ||
| ), | ||
| ] | ||
|
|
||
| ACCEPTANCE_CASES = generate_bson_acceptance_test_cases(UNSET_BSON_PARAMS) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("bson_type,sample_value,spec", ACCEPTANCE_CASES) | ||
| def test_unset_bson_type_accepted(collection, bson_type, sample_value, spec): | ||
| """Test $unset accepts all BSON types as field value.""" | ||
| collection.insert_one({"_id": 1, "a": 1}) | ||
| execute_command( | ||
| collection, | ||
| { | ||
| "update": collection.name, | ||
| "updates": [{"q": {"_id": 1}, "u": {"$unset": {"a": sample_value}}}], | ||
| }, | ||
| ) | ||
| result = execute_command(collection, {"find": collection.name, "filter": {"_id": 1}}) | ||
| assertSuccess( | ||
| result, [{"_id": 1}], msg=f"$unset should accept {bson_type.value} as field value" | ||
| ) |
66 changes: 66 additions & 0 deletions
66
...ests/compatibility/tests/core/operator/update/fields/unset/test_unset_command_contexts.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,66 @@ | ||
| """ | ||
| Tests for $unset update operator - command contexts. | ||
|
|
||
| Covers updateOne, updateMany, upsert, and nModified wiring. | ||
| """ | ||
|
|
||
| from documentdb_tests.framework.assertions import assertSuccess, assertSuccessPartial | ||
| from documentdb_tests.framework.executor import execute_command | ||
|
|
||
|
|
||
| def test_unset_upsert_no_match(collection): | ||
| """Test upsert with $unset on non-existent doc creates doc without field.""" | ||
| execute_command( | ||
| collection, | ||
| { | ||
| "update": collection.name, | ||
| "updates": [{"q": {"_id": 1}, "u": {"$unset": {"missing": ""}}, "upsert": True}], | ||
| }, | ||
| ) | ||
| result = execute_command(collection, {"find": collection.name, "filter": {"_id": 1}}) | ||
| assertSuccess(result, [{"_id": 1}], msg="Upsert with $unset should create doc without field") | ||
|
|
||
|
|
||
| def test_unset_update_one(collection): | ||
| """Test $unset in updateOne removes field from one doc.""" | ||
| collection.insert_many([{"_id": 1, "a": 1, "b": 2}, {"_id": 2, "a": 1, "b": 2}]) | ||
| result = execute_command( | ||
| collection, | ||
| { | ||
| "update": collection.name, | ||
| "updates": [{"q": {"a": 1}, "u": {"$unset": {"b": ""}}}], | ||
| }, | ||
| ) | ||
| assertSuccessPartial(result, {"n": 1, "nModified": 1, "ok": 1.0}, msg="Should unset in one doc") | ||
|
|
||
|
|
||
| def test_unset_nmodified_zero_when_field_missing(collection): | ||
| """Test $unset on non-existent field returns nModified: 0.""" | ||
| collection.insert_one({"_id": 1, "a": 1}) | ||
| result = execute_command( | ||
| collection, | ||
| { | ||
| "update": collection.name, | ||
| "updates": [{"q": {"_id": 1}, "u": {"$unset": {"missing": ""}}}], | ||
| }, | ||
| ) | ||
| assertSuccessPartial( | ||
| result, | ||
| {"n": 1, "nModified": 0, "ok": 1.0}, | ||
| msg="Unset non-existent field should have nModified: 0", | ||
| ) | ||
|
|
||
|
|
||
| def test_unset_update_many(collection): | ||
| """Test $unset in updateMany removes field from all matching.""" | ||
| collection.insert_many([{"_id": 1, "a": 1, "b": 2}, {"_id": 2, "a": 1, "b": 3}]) | ||
| result = execute_command( | ||
| collection, | ||
| { | ||
| "update": collection.name, | ||
| "updates": [{"q": {"a": 1}, "u": {"$unset": {"b": ""}}, "multi": True}], | ||
| }, | ||
| ) | ||
| assertSuccessPartial( | ||
| result, {"n": 2, "nModified": 2, "ok": 1.0}, msg="Should unset from all matching" | ||
| ) |
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.