Skip to content

Commit 711a45a

Browse files
authored
PYTHON-4938 Clarify write concern rules in the transactions spec (#2231)
1 parent 4bffc4e commit 711a45a

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
@@ -586,5 +586,29 @@ async def callback(session):
586586
self.assertFalse(s.in_transaction)
587587

588588

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

test/test_transactions.py

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

576576

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

0 commit comments

Comments
 (0)