8000 Raise a WriteError when trying to update with an empty $set. by pcorpet · Pull Request #623 · mongomock/mongomock · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Raise a WriteError when trying to update with an empty $set. #623

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 1 commit into from
Jun 17, 2020
Merged
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
6 changes: 6 additions & 0 deletions mongomock/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,12 @@ def _update(self, spec, document, upsert=False, manipulate=False,
document = helpers.patch_datetime_awareness_in_document(document)
validate_is_mapping('spec', spec)
validate_is_mapping('document', document)
for operator in _updaters:
if not document.get(operator, True):
raise WriteError(
"'%s' is empty. You must specify a field like so: {%s: {<field>: ...}}"
% (operator, operator),
)

updated_existing = False
upserted_id = None
Expand Down
9 changes: 9 additions & 0 deletions tests/test__collection_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5271,3 +5271,12 @@ def test__aggregate_array_to_object(self):
for item in items:
with self.assertRaises(mongomock.OperationFailure):
collection.aggregate(item)

def test_set_no_content(self):
collection = self.db.collection
collection.insert_one({'a': 1})
with self.assertRaises(mongomock.WriteError):
collection.update_one({}, {'$set': {}})

with self.assertRaises(mongomock.WriteError):
collection.update_one({'b': 'will-never-exist'}, {'$set': {}})
4 changes: 4 additions & 0 deletions tests/test__mongomock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,10 @@ def test__update_one(self):
upsert=True)
self.cmp.compare.find()

self.cmp.compare_exceptions.update_one({}, {'$set': {}})
self.cmp.compare_exceptions.update_one({'a': 'does-not-exist'}, {'$set': {}})
self.cmp.compare_exceptions.update_one({'a': 'does-not-exist'}, {'$set': {}}, upsert=True)

def test__update_many(self):
self.cmp.do.insert_many([{'a': 1, 'b': 0},
{'a': 2, 'b': 0}])
Expand Down
0