From abce7a43c582ca4fa062c8a0b825131fd060cfa6 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Tue, 25 Mar 2025 21:46:13 -0500 Subject: [PATCH 1/8] PYTHON-4938 Clarify write concern rules in the transactions spec --- test/asynchronous/test_transactions.py | 24 ++++++++++++++++++++++++ test/test_transactions.py | 24 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/test/asynchronous/test_transactions.py b/test/asynchronous/test_transactions.py index 884110cd45..fd486970cd 100644 --- a/test/asynchronous/test_transactions.py +++ b/test/asynchronous/test_transactions.py @@ -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 + 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. + inner_coll.insert_one({"n": 1}) + # Commit the transaction. + await s.commit_transaction() + # End the session. + # Ensure the document was inserted and no error was thrown from the transaction. + assert coll.find_one({}) == {"n": 1} + + if __name__ == "__main__": unittest.main() diff --git a/test/test_transactions.py b/test/test_transactions.py index 80b3e3765e..aa4f964aa3 100644 --- a/test/test_transactions.py +++ b/test/test_transactions.py @@ -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. + inner_coll.insert_one({"n": 1}) + # Commit the transaction. + s.commit_transaction() + # End the session. + # Ensure the document was inserted and no error was thrown from the transaction. + assert coll.find_one({}) == {"n": 1} + + if __name__ == "__main__": unittest.main() From e3726521ab13f215f7c5410cbfdb0c4100815ac7 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Tue, 25 Mar 2025 21:47:33 -0500 Subject: [PATCH 2/8] fix usage of session --- test/asynchronous/test_transactions.py | 2 +- test/test_transactions.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/asynchronous/test_transactions.py b/test/asynchronous/test_transactions.py index fd486970cd..526bfbe1c7 100644 --- a/test/asynchronous/test_transactions.py +++ b/test/asynchronous/test_transactions.py @@ -594,7 +594,7 @@ async def test_case_1(self): # 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. - inner_coll.insert_one({"n": 1}) + inner_coll.insert_one({"n": 1}, session=s) # Commit the transaction. await s.commit_transaction() # End the session. diff --git a/test/test_transactions.py b/test/test_transactions.py index aa4f964aa3..b2b52b8f33 100644 --- a/test/test_transactions.py +++ b/test/test_transactions.py @@ -582,7 +582,7 @@ def test_case_1(self): # 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. - inner_coll.insert_one({"n": 1}) + inner_coll.insert_one({"n": 1}, session=s) # Commit the transaction. s.commit_transaction() # End the session. From a3375fb95bc5f6aa7ee85c80870fcc3f8faf080f Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Wed, 26 Mar 2025 07:57:21 -0500 Subject: [PATCH 3/8] update tests --- test/asynchronous/test_transactions.py | 2 +- test/test_transactions.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/asynchronous/test_transactions.py b/test/asynchronous/test_transactions.py index 526bfbe1c7..977fb50537 100644 --- a/test/asynchronous/test_transactions.py +++ b/test/asynchronous/test_transactions.py @@ -599,7 +599,7 @@ async def test_case_1(self): await s.commit_transaction() # End the session. # Ensure the document was inserted and no error was thrown from the transaction. - assert coll.find_one({}) == {"n": 1} + assert coll.count_documents() == 1 if __name__ == "__main__": diff --git a/test/test_transactions.py b/test/test_transactions.py index b2b52b8f33..1eb7abca80 100644 --- a/test/test_transactions.py +++ b/test/test_transactions.py @@ -587,7 +587,7 @@ def test_case_1(self): s.commit_transaction() # End the session. # Ensure the document was inserted and no error was thrown from the transaction. - assert coll.find_one({}) == {"n": 1} + assert coll.count_documents() == 1 if __name__ == "__main__": From e2ccde7acf71c4c70b17e879b86753128296fe5c Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Wed, 26 Mar 2025 08:05:02 -0500 Subject: [PATCH 4/8] update tests --- test/asynchronous/test_transactions.py | 2 +- test/test_transactions.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/asynchronous/test_transactions.py b/test/asynchronous/test_transactions.py index 977fb50537..963903129e 100644 --- a/test/asynchronous/test_transactions.py +++ b/test/asynchronous/test_transactions.py @@ -599,7 +599,7 @@ async def test_case_1(self): await s.commit_transaction() # End the session. # Ensure the document was inserted and no error was thrown from the transaction. - assert coll.count_documents() == 1 + assert coll.count_documents({}) == 1 if __name__ == "__main__": diff --git a/test/test_transactions.py b/test/test_transactions.py index 1eb7abca80..c7d2dec3a9 100644 --- a/test/test_transactions.py +++ b/test/test_transactions.py @@ -587,7 +587,7 @@ def test_case_1(self): s.commit_transaction() # End the session. # Ensure the document was inserted and no error was thrown from the transaction. - assert coll.count_documents() == 1 + assert coll.count_documents({}) == 1 if __name__ == "__main__": From e3c6daba122006b34c1fa00802cd7ab27a280533 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Wed, 26 Mar 2025 08:20:45 -0500 Subject: [PATCH 5/8] fix test --- test/asynchronous/test_transactions.py | 2 +- test/test_transactions.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/asynchronous/test_transactions.py b/test/asynchronous/test_transactions.py index 963903129e..f7d87cc11f 100644 --- a/test/asynchronous/test_transactions.py +++ b/test/asynchronous/test_transactions.py @@ -599,7 +599,7 @@ async def test_case_1(self): await s.commit_transaction() # End the session. # Ensure the document was inserted and no error was thrown from the transaction. - assert coll.count_documents({}) == 1 + assert (await coll.count_documents({})) == 1 if __name__ == "__main__": diff --git a/test/test_transactions.py b/test/test_transactions.py index c7d2dec3a9..c43cfb9716 100644 --- a/test/test_transactions.py +++ b/test/test_transactions.py @@ -587,7 +587,7 @@ def test_case_1(self): s.commit_transaction() # End the session. # Ensure the document was inserted and no error was thrown from the transaction. - assert coll.count_documents({}) == 1 + assert (coll.count_documents({})) == 1 if __name__ == "__main__": From a0eae81f1f1a9a12bfaee917999b3239d85e36ea Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Wed, 26 Mar 2025 10:09:59 -0500 Subject: [PATCH 6/8] update test --- test/asynchronous/test_transactions.py | 2 +- test/test_transactions.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/asynchronous/test_transactions.py b/test/asynchronous/test_transactions.py index f7d87cc11f..68bfc8e024 100644 --- a/test/asynchronous/test_transactions.py +++ b/test/asynchronous/test_transactions.py @@ -599,7 +599,7 @@ async def test_case_1(self): await s.commit_transaction() # End the session. # Ensure the document was inserted and no error was thrown from the transaction. - assert (await coll.count_documents({})) == 1 + assert (await coll.find_one({"n": 1})) is not None if __name__ == "__main__": diff --git a/test/test_transactions.py b/test/test_transactions.py index c43cfb9716..90c2ebb755 100644 --- a/test/test_transactions.py +++ b/test/test_transactions.py @@ -587,7 +587,7 @@ def test_case_1(self): s.commit_transaction() # End the session. # Ensure the document was inserted and no error was thrown from the transaction. - assert (coll.count_documents({})) == 1 + assert (coll.find_one({"n": 1})) is not None if __name__ == "__main__": From 33ff947b505b169278db093dc7e21609d7e07528 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Wed, 26 Mar 2025 10:29:20 -0500 Subject: [PATCH 7/8] update test --- test/asynchronous/test_transactions.py | 4 ++-- test/test_transactions.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/asynchronous/test_transactions.py b/test/asynchronous/test_transactions.py index 68bfc8e024..6421af0103 100644 --- a/test/asynchronous/test_transactions.py +++ b/test/asynchronous/test_transactions.py @@ -594,12 +594,12 @@ async def test_case_1(self): # 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. - inner_coll.insert_one({"n": 1}, session=s) + 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 (await coll.find_one({"n": 1})) is not None + assert result.inserted_id is not None if __name__ == "__main__": diff --git a/test/test_transactions.py b/test/test_transactions.py index 90c2ebb755..95bcec54a6 100644 --- a/test/test_transactions.py +++ b/test/test_transactions.py @@ -582,12 +582,12 @@ def test_case_1(self): # 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. - inner_coll.insert_one({"n": 1}, session=s) + 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 (coll.find_one({"n": 1})) is not None + assert result.inserted_id is not None if __name__ == "__main__": From 1fdf8c6865a6a8e19dd5782cbeb1bbaeac27c564 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Wed, 26 Mar 2025 12:40:08 -0500 Subject: [PATCH 8/8] fix test --- test/asynchronous/test_transactions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/asynchronous/test_transactions.py b/test/asynchronous/test_transactions.py index 6421af0103..005eae6c84 100644 --- a/test/asynchronous/test_transactions.py +++ b/test/asynchronous/test_transactions.py @@ -586,7 +586,7 @@ async def test_case_1(self): # Create a MongoClient running against a configured sharded/replica set/load balanced cluster. client = async_client_context.client coll = client[self.db.name].test - coll.delete_many({}) + await coll.delete_many({}) # Start a new session on the client. async with client.start_session() as s: # Start a transaction on the session.