Skip to content

Commit

Permalink
PYTHON-4786 - Fix UpdateResult.did_upsert TypeError (#1878)
Browse files Browse the repository at this point in the history
(cherry picked from commit 7848feb)
  • Loading branch information
NoahStapp authored and blink1073 committed Oct 1, 2024
1 parent b74d643 commit 24f28de
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pymongo/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,12 @@ def upserted_id(self) -> Any:

@property
def did_upsert(self) -> bool:
"""Whether or not an upsert took place."""
"""Whether an upsert took place.
.. versionadded:: 4.9
"""
assert self.__raw_result is not None
return len(self.__raw_result.get("upserted", {})) > 0
return "upserted" in self.__raw_result


class DeleteResult(_WriteResult):
Expand Down
40 changes: 40 additions & 0 deletions test/asynchronous/test_client_bulk_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,46 @@ async def test_returns_error_if_auto_encryption_configured(self):
"bulk_write does not currently support automatic encryption", context.exception._message
)

@async_client_context.require_version_min(8, 0, 0, -24)
@async_client_context.require_no_serverless
async def test_upserted_result(self):
client = await self.async_rs_or_single_client()

collection = client.db["coll"]
self.addAsyncCleanup(collection.drop)
await collection.drop()

models = []
models.append(
UpdateOne(
namespace="db.coll",
filter={"_id": "a"},
update={"$set": {"x": 1}},
upsert=True,
)
)
models.append(
UpdateOne(
namespace="db.coll",
filter={"_id": None},
update={"$set": {"x": 1}},
upsert=True,
)
)
models.append(
UpdateOne(
namespace="db.coll",
filter={"_id": None},
update={"$set": {"x": 1}},
)
)
result = await client.bulk_write(models=models, verbose_results=True)

self.assertEqual(result.upserted_count, 2)
self.assertEqual(result.update_results[0].did_upsert, True)
self.assertEqual(result.update_results[1].did_upsert, True)
self.assertEqual(result.update_results[2].did_upsert, False)


# https://github.com/mongodb/specifications/blob/master/source/client-side-operations-timeout/tests/README.md#11-multi-batch-bulkwrites
class TestClientBulkWriteCSOT(AsyncIntegrationTest):
Expand Down
13 changes: 13 additions & 0 deletions test/asynchronous/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,19 @@ async def test_update_one(self):
self.assertRaises(InvalidOperation, lambda: result.upserted_id)
self.assertFalse(result.acknowledged)

async def test_update_result(self):
db = self.db
await db.drop_collection("test")

result = await db.test.update_one({"x": 0}, {"$inc": {"x": 1}}, upsert=True)
self.assertEqual(result.did_upsert, True)

result = await db.test.update_one({"_id": None, "x": 0}, {"$inc": {"x": 1}}, upsert=True)
self.assertEqual(result.did_upsert, True)

result = await db.test.update_one({"_id": None}, {"$inc": {"x": 1}})
self.assertEqual(result.did_upsert, False)

async def test_update_many(self):
db = self.db
await db.drop_collection("test")
Expand Down
40 changes: 40 additions & 0 deletions test/test_client_bulk_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,46 @@ def test_returns_error_if_auto_encryption_configured(self):
"bulk_write does not currently support automatic encryption", context.exception._message
)

@client_context.require_version_min(8, 0, 0, -24)
@client_context.require_no_serverless
def test_upserted_result(self):
client = self.rs_or_single_client()

collection = client.db["coll"]
self.addCleanup(collection.drop)
collection.drop()

models = []
models.append(
UpdateOne(
namespace="db.coll",
filter={"_id": "a"},
update={"$set": {"x": 1}},
upsert=True,
)
)
models.append(
UpdateOne(
namespace="db.coll",
filter={"_id": None},
update={"$set": {"x": 1}},
upsert=True,
)
)
models.append(
UpdateOne(
namespace="db.coll",
filter={"_id": None},
update={"$set": {"x": 1}},
)
)
result = client.bulk_write(models=models, verbose_results=True)

self.assertEqual(result.upserted_count, 2)
self.assertEqual(result.update_results[0].did_upsert, True)
self.assertEqual(result.update_results[1].did_upsert, True)
self.assertEqual(result.update_results[2].did_upsert, False)


# https://github.com/mongodb/specifications/blob/master/source/client-side-operations-timeout/tests/README.md#11-multi-batch-bulkwrites
class TestClientBulkWriteCSOT(IntegrationTest):
Expand Down
13 changes: 13 additions & 0 deletions test/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,19 @@ def test_update_one(self):
self.assertRaises(InvalidOperation, lambda: result.upserted_id)
self.assertFalse(result.acknowledged)

def test_update_result(self):
db = self.db
db.drop_collection("test")

result = db.test.update_one({"x": 0}, {"$inc": {"x": 1}}, upsert=True)
self.assertEqual(result.did_upsert, True)

result = db.test.update_one({"_id": None, "x": 0}, {"$inc": {"x": 1}}, upsert=True)
self.assertEqual(result.did_upsert, True)

result = db.test.update_one({"_id": None}, {"$inc": {"x": 1}})
self.assertEqual(result.did_upsert, False)

def test_update_many(self):
db = self.db
db.drop_collection("test")
Expand Down
22 changes: 22 additions & 0 deletions test/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,28 @@ 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, True)

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

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

raw_result_3 = {
"n": 1,
"nModified": 1,
}
self.repr_test(UpdateResult, raw_result_3)

result = UpdateResult(raw_result_3, True)
self.assertEqual(result.did_upsert, False)

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

0 comments on commit 24f28de

Please sign in to comment.