PYTHON-4938 Clarify write concern rules in the transactions spec (#2231)

This commit is contained in:
Steven Silvester 2025-03-31 19:23:32 -05:00 committed by GitHub
parent 4bffc4e492
commit 711a45a0e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 48 additions and 0 deletions

View File

@ -586,5 +586,29 @@ class TestTransactionsConvenientAPI(AsyncTransactionsBase):
self.assertFalse(s.in_transaction)
class TestOptionsInsideTransactionProse(AsyncTransactionsBase):
@async_client_context.require_transactions
@async_client_context.require_no_standalone
async def test_case_1(self):
# Write concern not inherited from collection object inside transaction
# Create a MongoClient running against a configured sharded/replica set/load balanced cluster.
client = async_client_context.client
coll = client[self.db.name].test
await coll.delete_many({})
# Start a new session on the client.
async with client.start_session() as s:
# Start a transaction on the session.
await s.start_transaction()
# Instantiate a collection object in the driver with a default write concern of { w: 0 }.
inner_coll = coll.with_options(write_concern=WriteConcern(w=0))
# Insert the document { n: 1 } on the instantiated collection.
result = await inner_coll.insert_one({"n": 1}, session=s)
# Commit the transaction.
await s.commit_transaction()
# End the session.
# Ensure the document was inserted and no error was thrown from the transaction.
assert result.inserted_id is not None
if __name__ == "__main__":
unittest.main()

View File

@ -574,5 +574,29 @@ class TestTransactionsConvenientAPI(TransactionsBase):
self.assertFalse(s.in_transaction)
class TestOptionsInsideTransactionProse(TransactionsBase):
@client_context.require_transactions
@client_context.require_no_standalone
def test_case_1(self):
# Write concern not inherited from collection object inside transaction
# Create a MongoClient running against a configured sharded/replica set/load balanced cluster.
client = client_context.client
coll = client[self.db.name].test
coll.delete_many({})
# Start a new session on the client.
with client.start_session() as s:
# Start a transaction on the session.
s.start_transaction()
# Instantiate a collection object in the driver with a default write concern of { w: 0 }.
inner_coll = coll.with_options(write_concern=WriteConcern(w=0))
# Insert the document { n: 1 } on the instantiated collection.
result = inner_coll.insert_one({"n": 1}, session=s)
# Commit the transaction.
s.commit_transaction()
# End the session.
# Ensure the document was inserted and no error was thrown from the transaction.
assert result.inserted_id is not None
if __name__ == "__main__":
unittest.main()