Skip to content
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

PYTHON-4786 - Fix UpdateResult.did_upsert TypeError #1878

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
5 changes: 3 additions & 2 deletions pymongo/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,10 @@ def upserted_id(self) -> Any:

@property
def did_upsert(self) -> bool:
"""Whether or not an upsert took place."""
"""Whether an upsert took place."""
assert self.__raw_result is not None
return len(self.__raw_result.get("upserted", {})) > 0
result = self.__raw_result.get("upserted")
return result is not None and len(str(result)) > 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't handle the None case correctly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a few more tests. For both client.bulk_write and collection.update_one() that tests upserting a document with "_id": None.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we expect something besides False if the result is None?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we allow upserting a document's id to be None?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes None is a valid id



class DeleteResult(_WriteResult):
Expand Down
13 changes: 13 additions & 0 deletions test/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ def test_update_result(self):
self.assertEqual(raw_result["n"], result.matched_count)
self.assertEqual(raw_result["nModified"], result.modified_count)
self.assertEqual(raw_result["upserted"], result.upserted_id)
self.assertEqual(result.did_upsert, False)

raw_result_upserted = {
"n": 1,
"nModified": 1,
"upserted": [
{"index": 5, "_id": 1},
],
}
self.repr_test(UpdateResult, raw_result_upserted)

result = UpdateResult(raw_result_upserted, True)
self.assertEqual(result.did_upsert, True)

result = UpdateResult(raw_result, False)
self.assertEqual(raw_result, result.raw_result)
Expand Down
Loading