Skip to content

Commit abce7a4

Browse files
committed
PYTHON-4938 Clarify write concern rules in the transactions spec
1 parent 2149567 commit abce7a4

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

test/asynchronous/test_transactions.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,5 +578,29 @@ async def callback(session):
578578
self.assertFalse(s.in_transaction)
579579

580580

581+
class TestOptionsInsideTransactionProse(AsyncTransactionsBase):
582+
@async_client_context.require_transactions
583+
@async_client_context.require_no_standalone
584+
async def test_case_1(self):
585+
# Write concern not inherited from collection object inside transaction
586+
# Create a MongoClient running against a configured sharded/replica set/load balanced cluster.
587+
client = async_client_context.client
588+
coll = client[self.db.name].test
589+
coll.delete_many({})
590+
# Start a new session on the client.
591+
async with client.start_session() as s:
592+
# Start a transaction on the session.
593+
await s.start_transaction()
594+
# Instantiate a collection object in the driver with a default write concern of { w: 0 }.
595+
inner_coll = coll.with_options(write_concern=WriteConcern(w=0))
596+
# Insert the document { n: 1 } on the instantiated collection.
597+
inner_coll.insert_one({"n": 1})
598+
# Commit the transaction.
599+
await s.commit_transaction()
600+
# End the session.
601+
# Ensure the document was inserted and no error was thrown from the transaction.
602+
assert coll.find_one({}) == {"n": 1}
603+
604+
581605
if __name__ == "__main__":
582606
unittest.main()

test/test_transactions.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,5 +566,29 @@ def callback(session):
566566
self.assertFalse(s.in_transaction)
567567

568568

569+
class TestOptionsInsideTransactionProse(TransactionsBase):
570+
@client_context.require_transactions
571+
@client_context.require_no_standalone
572+
def test_case_1(self):
573+
# Write concern not inherited from collection object inside transaction
574+
# Create a MongoClient running against a configured sharded/replica set/load balanced cluster.
575+
client = client_context.client
576+
coll = client[self.db.name].test
577+
coll.delete_many({})
578+
# Start a new session on the client.
579+
with client.start_session() as s:
580+
# Start a transaction on the session.
581+
s.start_transaction()
582+
# Instantiate a collection object in the driver with a default write concern of { w: 0 }.
583+
inner_coll = coll.with_options(write_concern=WriteConcern(w=0))
584+
# Insert the document { n: 1 } on the instantiated collection.
585+
inner_coll.insert_one({"n": 1})
586+
# Commit the transaction.
587+
s.commit_transaction()
588+
# End the session.
589+
# Ensure the document was inserted and no error was thrown from the transaction.
590+
assert coll.find_one({}) == {"n": 1}
591+
592+
569593
if __name__ == "__main__":
570594
unittest.main()

0 commit comments

Comments
 (0)