Skip to content

PYTHON-4786 - Fix UpdateResult.did_upsert TypeError #1878

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 8 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 2 additions & 2 deletions pymongo/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ def upserted_id(self) -> Any:

@property
def did_upsert(self) -> bool:
"""Whether or not an upsert took place."""
"""Whether an upsert took place."""
Copy link
Member

@ShaneHarvey ShaneHarvey Oct 1, 2024

Choose a reason for hiding this comment

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

Please add .. 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
41 changes: 41 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,47 @@ 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
@unittest.skipUnless(_HAVE_PYMONGOCRYPT, "pymongocrypt is not installed")
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
41 changes: 41 additions & 0 deletions test/test_client_bulk_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,47 @@ 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
@unittest.skipUnless(_HAVE_PYMONGOCRYPT, "pymongocrypt is not installed")
Copy link
Member

Choose a reason for hiding this comment

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

Why require _HAVE_PYMONGOCRYPT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops, artifact of copy-and-paste.

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
Loading