Skip to content

Commit

Permalink
PYTHON-4786 - Fix UpdateResult.did_upsert TypeError
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahStapp committed Sep 27, 2024
1 parent e03f8f2 commit 5cca021
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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


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

0 comments on commit 5cca021

Please sign in to comment.