Skip to content

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

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
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 24 additions & 0 deletions test/asynchronous/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,5 +578,29 @@ async def callback(session):
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()
24 changes: 24 additions & 0 deletions test/test_transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,5 +566,29 @@ def callback(session):
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()
Loading