From cac0d5548fb34eb65e29116c08ea736b3145daa7 Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Tue, 22 May 2018 12:09:11 -0700 Subject: [PATCH] PYTHON-1508 Remove autoStartTransaction and resync tests Also use the test file's database name and collection name. --- pymongo/client_session.py | 27 +- pymongo/mongo_client.py | 5 +- test/test_transactions.py | 14 +- test/transactions/abort.json | 2 + test/transactions/auto-start.json | 741 --------------------- test/transactions/bulk.json | 61 +- test/transactions/causal-consistency.json | 2 + test/transactions/commit.json | 2 + test/transactions/delete.json | 2 + test/transactions/errors.json | 2 + test/transactions/findOneAndDelete.json | 2 + test/transactions/findOneAndReplace.json | 2 + test/transactions/findOneAndUpdate.json | 2 + test/transactions/insert.json | 2 + test/transactions/isolation.json | 2 + test/transactions/read-pref.json | 2 + test/transactions/reads.json | 2 + test/transactions/retryable-writes.json | 2 + test/transactions/transaction-options.json | 2 + test/transactions/update.json | 2 + test/transactions/write-concern.json | 2 + 21 files changed, 84 insertions(+), 796 deletions(-) delete mode 100644 test/transactions/auto-start.json diff --git a/pymongo/client_session.py b/pymongo/client_session.py index b76f7c05b..3836ac844 100644 --- a/pymongo/client_session.py +++ b/pymongo/client_session.py @@ -105,17 +105,13 @@ class SessionOptions(object): :Parameters: - `causal_consistency` (optional): If True (the default), read operations are causally ordered within the session. - - `auto_start_transaction` (optional): If True, any operation using - the session automatically starts a transaction. - `default_transaction_options` (optional): The default TransactionOptions to use for transactions started on this session. """ def __init__(self, causal_consistency=True, - auto_start_transaction=False, default_transaction_options=None): self._causal_consistency = causal_consistency - self._auto_start_transaction = auto_start_transaction if default_transaction_options is not None: if not isinstance(default_transaction_options, TransactionOptions): raise TypeError( @@ -129,15 +125,6 @@ class SessionOptions(object): """Whether causal consistency is configured.""" return self._causal_consistency - @property - def auto_start_transaction(self): - """Whether any operation using the session automatically starts a - transaction. - - .. versionadded:: 3.7 - """ - return self._auto_start_transaction - @property def default_transaction_options(self): """The default TransactionOptions to use for transactions started on @@ -349,7 +336,7 @@ class ClientSession(object): def _finish_transaction(self, command_name): self._check_ended() - if not self._in_transaction_or_auto_start(): + if not self._in_transaction: raise InvalidOperation("No transaction started") try: @@ -424,24 +411,14 @@ class ClientSession(object): """True if this session has an active multi-statement transaction.""" return self._transaction is not None - def _in_transaction_or_auto_start(self): - """True if this session has an active transaction or will have one.""" - if self._in_transaction: - return True - if self.options.auto_start_transaction: - self.start_transaction() - return True - return False - def _txn_read_preference(self): """Return read preference of this transaction or None.""" - if self._in_transaction_or_auto_start(): + if self._in_transaction: return self._transaction.opts.read_preference return None def _apply_to(self, command, is_retryable, read_preference): self._check_ended() - self._in_transaction_or_auto_start() self._server_session.last_use = monotonic.time() command['lsid'] = self._server_session.session_id diff --git a/pymongo/mongo_client.py b/pymongo/mongo_client.py index bcaa890db..cd910598a 100644 --- a/pymongo/mongo_client.py +++ b/pymongo/mongo_client.py @@ -1070,8 +1070,7 @@ class MongoClient(common.BaseObject): Re-raises any exception thrown by func(). """ retryable = (retryable and self.retry_writes - and session - and not session._in_transaction_or_auto_start()) + and session and not session._in_transaction) last_error = None retrying = False @@ -1361,7 +1360,6 @@ class MongoClient(common.BaseObject): def start_session(self, causal_consistency=True, - auto_start_transaction=False, default_transaction_options=None): """Start a logical session. @@ -1393,7 +1391,6 @@ class MongoClient(common.BaseObject): server_session = self._get_server_session() opts = client_session.SessionOptions( causal_consistency=causal_consistency, - auto_start_transaction=auto_start_transaction, default_transaction_options=default_transaction_options) return client_session.ClientSession( self, server_session, opts, authset) diff --git a/test/test_transactions.py b/test/test_transactions.py index 559e0e3b9..f89db5527 100644 --- a/test/test_transactions.py +++ b/test/test_transactions.py @@ -299,14 +299,16 @@ def create_test(scenario_def, test): # "operation was interrupted" by killing the command's own session. pass + database_name = scenario_def['database_name'] + collection_name = scenario_def['collection_name'] write_concern_db = client.get_database( - 'transaction-tests', write_concern=WriteConcern(w='majority')) - - write_concern_db.test.drop() - write_concern_db.create_collection('test') + database_name, write_concern=WriteConcern(w='majority')) + write_concern_coll = write_concern_db[collection_name] + write_concern_coll.drop() + write_concern_db.create_collection(collection_name) if scenario_def['data']: # Load data. - write_concern_db.test.insert_many(scenario_def['data']) + write_concern_coll.insert_many(scenario_def['data']) # Create session0 and session1. sessions = {} @@ -349,7 +351,7 @@ def create_test(scenario_def, test): self.addCleanup(end_sessions, sessions) listener.results.clear() - collection = client['transaction-tests'].test + collection = client[database_name][collection_name] for op in test['operations']: expected_result = op.get('result') diff --git a/test/transactions/abort.json b/test/transactions/abort.json index b27a11816..e63462491 100644 --- a/test/transactions/abort.json +++ b/test/transactions/abort.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [], "tests": [ { diff --git a/test/transactions/auto-start.json b/test/transactions/auto-start.json deleted file mode 100644 index c9a65475a..000000000 --- a/test/transactions/auto-start.json +++ /dev/null @@ -1,741 +0,0 @@ -{ - "data": [], - "tests": [ - { - "description": "commit", - "sessionOptions": { - "session0": { - "autoStartTransaction": true - } - }, - "operations": [ - { - "name": "insertOne", - "arguments": { - "document": { - "_id": 1 - }, - "session": "session0" - }, - "result": { - "insertedId": 1 - } - }, - { - "name": "commitTransaction", - "arguments": { - "session": "session0" - } - }, - { - "name": "insertOne", - "arguments": { - "document": { - "_id": 2 - }, - "session": "session0" - }, - "result": { - "insertedId": 2 - } - }, - { - "name": "insertOne", - "arguments": { - "document": { - "_id": 3 - }, - "session": "session0" - }, - "result": { - "insertedId": 3 - } - }, - { - "name": "commitTransaction", - "arguments": { - "session": "session0" - } - } - ], - "expectations": [ - { - "command_started_event": { - "command": { - "insert": "test", - "documents": [ - { - "_id": 1 - } - ], - "ordered": true, - "readConcern": null, - "lsid": "session0", - "txnNumber": { - "$numberLong": "1" - }, - "startTransaction": true, - "autocommit": false, - "writeConcern": null - }, - "command_name": "insert", - "database_name": "transaction-tests" - } - }, - { - "command_started_event": { - "command": { - "commitTransaction": 1, - "lsid": "session0", - "txnNumber": { - "$numberLong": "1" - }, - "startTransaction": null, - "autocommit": false, - "writeConcern": null - }, - "command_name": "commitTransaction", - "database_name": "admin" - } - }, - { - "command_started_event": { - "command": { - "insert": "test", - "documents": [ - { - "_id": 2 - } - ], - "ordered": true, - "readConcern": { - "afterClusterTime": 42 - }, - "lsid": "session0", - "txnNumber": { - "$numberLong": "2" - }, - "startTransaction": true, - "autocommit": false, - "writeConcern": null - }, - "command_name": "insert", - "database_name": "transaction-tests" - } - }, - { - "command_started_event": { - "command": { - "insert": "test", - "documents": [ - { - "_id": 3 - } - ], - "ordered": true, - "lsid": "session0", - "txnNumber": { - "$numberLong": "2" - }, - "startTransaction": null, - "autocommit": false, - "writeConcern": null - }, - "command_name": "insert", - "database_name": "transaction-tests" - } - }, - { - "command_started_event": { - "command": { - "commitTransaction": 1, - "lsid": "session0", - "txnNumber": { - "$numberLong": "2" - }, - "startTransaction": null, - "autocommit": false, - "writeConcern": null - }, - "command_name": "commitTransaction", - "database_name": "admin" - } - } - ], - "outcome": { - "collection": { - "data": [ - { - "_id": 1 - }, - { - "_id": 2 - }, - { - "_id": 3 - } - ] - } - } - }, - { - "description": "explicit start succeeds", - "sessionOptions": { - "session0": { - "autoStartTransaction": true - } - }, - "operations": [ - { - "name": "startTransaction", - "arguments": { - "session": "session0" - } - }, - { - "name": "commitTransaction", - "arguments": { - "session": "session0" - } - }, - { - "name": "startTransaction", - "arguments": { - "session": "session0" - } - }, - { - "name": "abortTransaction", - "arguments": { - "session": "session0" - } - }, - { - "name": "startTransaction", - "arguments": { - "session": "session0" - } - }, - { - "name": "insertOne", - "arguments": { - "document": { - "_id": 1 - }, - "session": "session0" - }, - "result": { - "insertedId": 1 - } - }, - { - "name": "commitTransaction", - "arguments": { - "session": "session0" - } - } - ], - "expectations": [ - { - "command_started_event": { - "command": { - "insert": "test", - "documents": [ - { - "_id": 1 - } - ], - "ordered": true, - "readConcern": null, - "lsid": "session0", - "txnNumber": { - "$numberLong": "3" - }, - "startTransaction": true, - "autocommit": false, - "writeConcern": null - }, - "command_name": "insert", - "database_name": "transaction-tests" - } - }, - { - "command_started_event": { - "command": { - "commitTransaction": 1, - "lsid": "session0", - "txnNumber": { - "$numberLong": "3" - }, - "startTransaction": null, - "autocommit": false, - "writeConcern": null - }, - "command_name": "commitTransaction", - "database_name": "admin" - } - } - ], - "outcome": { - "collection": { - "data": [ - { - "_id": 1 - } - ] - } - } - }, - { - "description": "explicit start fails", - "sessionOptions": { - "session0": { - "autoStartTransaction": true - } - }, - "operations": [ - { - "name": "insertOne", - "arguments": { - "document": { - "_id": 1 - }, - "session": "session0" - }, - "result": { - "insertedId": 1 - } - }, - { - "name": "startTransaction", - "arguments": { - "session": "session0" - }, - "result": { - "errorContains": "transaction already in progress" - } - }, - { - "name": "commitTransaction", - "arguments": { - "session": "session0" - } - } - ], - "expectations": [ - { - "command_started_event": { - "command": { - "insert": "test", - "documents": [ - { - "_id": 1 - } - ], - "ordered": true, - "readConcern": null, - "lsid": "session0", - "txnNumber": { - "$numberLong": "1" - }, - "startTransaction": true, - "autocommit": false, - "writeConcern": null - }, - "command_name": "insert", - "database_name": "transaction-tests" - } - }, - { - "command_started_event": { - "command": { - "commitTransaction": 1, - "lsid": "session0", - "txnNumber": { - "$numberLong": "1" - }, - "startTransaction": null, - "autocommit": false, - "writeConcern": null - }, - "command_name": "commitTransaction", - "database_name": "admin" - } - } - ], - "outcome": { - "collection": { - "data": [ - { - "_id": 1 - } - ] - } - } - }, - { - "description": "abort", - "sessionOptions": { - "session0": { - "autoStartTransaction": true - } - }, - "operations": [ - { - "name": "insertOne", - "arguments": { - "document": { - "_id": 1 - }, - "session": "session0" - }, - "result": { - "insertedId": 1 - } - }, - { - "name": "abortTransaction", - "arguments": { - "session": "session0" - } - }, - { - "name": "insertOne", - "arguments": { - "document": { - "_id": 2 - }, - "session": "session0" - }, - "result": { - "insertedId": 2 - } - }, - { - "name": "commitTransaction", - "arguments": { - "session": "session0" - } - } - ], - "expectations": [ - { - "command_started_event": { - "command": { - "insert": "test", - "documents": [ - { - "_id": 1 - } - ], - "ordered": true, - "readConcern": null, - "lsid": "session0", - "txnNumber": { - "$numberLong": "1" - }, - "startTransaction": true, - "autocommit": false, - "writeConcern": null - }, - "command_name": "insert", - "database_name": "transaction-tests" - } - }, - { - "command_started_event": { - "command": { - "abortTransaction": 1, - "lsid": "session0", - "txnNumber": { - "$numberLong": "1" - }, - "startTransaction": null, - "autocommit": false, - "writeConcern": null - }, - "command_name": "abortTransaction", - "database_name": "admin" - } - }, - { - "command_started_event": { - "command": { - "insert": "test", - "documents": [ - { - "_id": 2 - } - ], - "ordered": true, - "readConcern": { - "afterClusterTime": 42 - }, - "lsid": "session0", - "txnNumber": { - "$numberLong": "2" - }, - "startTransaction": true, - "autocommit": false, - "writeConcern": null - }, - "command_name": "insert", - "database_name": "transaction-tests" - } - }, - { - "command_started_event": { - "command": { - "commitTransaction": 1, - "lsid": "session0", - "txnNumber": { - "$numberLong": "2" - }, - "startTransaction": null, - "autocommit": false, - "writeConcern": null - }, - "command_name": "commitTransaction", - "database_name": "admin" - } - } - ], - "outcome": { - "collection": { - "data": [ - { - "_id": 2 - } - ] - } - } - }, - { - "description": "commit empty transaction", - "sessionOptions": { - "session0": { - "autoStartTransaction": true - } - }, - "operations": [ - { - "name": "commitTransaction", - "arguments": { - "session": "session0" - } - }, - { - "name": "insertOne", - "arguments": { - "document": { - "_id": 1 - }, - "session": "session0" - }, - "result": { - "insertedId": 1 - } - }, - { - "name": "commitTransaction", - "arguments": { - "session": "session0" - } - }, - { - "name": "commitTransaction", - "arguments": { - "session": "session0" - } - } - ], - "expectations": [ - { - "command_started_event": { - "command": { - "insert": "test", - "documents": [ - { - "_id": 1 - } - ], - "ordered": true, - "readConcern": null, - "lsid": "session0", - "txnNumber": { - "$numberLong": "2" - }, - "startTransaction": true, - "autocommit": false, - "writeConcern": null - }, - "command_name": "insert", - "database_name": "transaction-tests" - } - }, - { - "command_started_event": { - "command": { - "commitTransaction": 1, - "lsid": "session0", - "txnNumber": { - "$numberLong": "2" - }, - "startTransaction": null, - "autocommit": false, - "writeConcern": null - }, - "command_name": "commitTransaction", - "database_name": "admin" - } - } - ], - "outcome": { - "collection": { - "data": [ - { - "_id": 1 - } - ] - } - } - }, - { - "description": "isolation", - "sessionOptions": { - "session0": { - "autoStartTransaction": true - } - }, - "operations": [ - { - "name": "insertOne", - "arguments": { - "document": { - "_id": 1 - }, - "session": "session0" - }, - "result": { - "insertedId": 1 - } - }, - { - "name": "find", - "arguments": { - "filter": { - "_id": 1 - }, - "session": "session1" - }, - "result": [] - }, - { - "name": "commitTransaction", - "arguments": { - "session": "session0" - } - }, - { - "name": "find", - "arguments": { - "filter": { - "_id": 1 - }, - "session": "session1" - }, - "result": [ - { - "_id": 1 - } - ] - } - ], - "expectations": [ - { - "command_started_event": { - "command": { - "insert": "test", - "documents": [ - { - "_id": 1 - } - ], - "ordered": true, - "readConcern": null, - "lsid": "session0", - "txnNumber": { - "$numberLong": "1" - }, - "startTransaction": true, - "autocommit": false, - "writeConcern": null - }, - "command_name": "insert", - "database_name": "transaction-tests" - } - }, - { - "command_started_event": { - "command": { - "find": "test", - "filter": { - "_id": 1 - }, - "lsid": "session1", - "txnNumber": null, - "startTransaction": null, - "autocommit": null - }, - "command_name": "find", - "database_name": "transaction-tests" - } - }, - { - "command_started_event": { - "command": { - "commitTransaction": 1, - "lsid": "session0", - "txnNumber": { - "$numberLong": "1" - }, - "startTransaction": null, - "autocommit": false, - "writeConcern": null - }, - "command_name": "commitTransaction", - "database_name": "admin" - } - }, - { - "command_started_event": { - "command": { - "find": "test", - "filter": { - "_id": 1 - }, - "readConcern": { - "afterClusterTime": 42 - }, - "lsid": "session1", - "txnNumber": null, - "startTransaction": null, - "autocommit": null - }, - "command_name": "find", - "database_name": "transaction-tests" - } - } - ], - "outcome": { - "collection": { - "data": [ - { - "_id": 1 - } - ] - } - } - } - ] -} diff --git a/test/transactions/bulk.json b/test/transactions/bulk.json index ffdaf6076..45986b66f 100644 --- a/test/transactions/bulk.json +++ b/test/transactions/bulk.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [], "tests": [ { @@ -151,16 +153,6 @@ } } }, - { - "name": "deleteMany", - "arguments": { - "filter": { - "_id": { - "$gte": 6 - } - } - } - }, { "name": "updateMany", "arguments": { @@ -175,6 +167,16 @@ } } } + }, + { + "name": "deleteMany", + "arguments": { + "filter": { + "_id": { + "$gte": 6 + } + } + } } ], "session": "session0" @@ -189,8 +191,8 @@ "6": 6, "7": 7 }, - "matchedCount": 5, - "modifiedCount": 5, + "matchedCount": 7, + "modifiedCount": 7, "upsertedCount": 1, "upsertedIds": { "2": 2 @@ -407,14 +409,6 @@ "_id": 4 }, "limit": 1 - }, - { - "q": { - "_id": { - "$gte": 6 - } - }, - "limit": 0 } ], "ordered": true, @@ -463,6 +457,33 @@ "database_name": "transaction-tests" } }, + { + "command_started_event": { + "command": { + "delete": "test", + "deletes": [ + { + "q": { + "_id": { + "$gte": 6 + } + }, + "limit": 0 + } + ], + "ordered": true, + "lsid": "session0", + "txnNumber": { + "$numberLong": "1" + }, + "startTransaction": null, + "autocommit": false, + "writeConcern": null + }, + "command_name": "delete", + "database_name": "transaction-tests" + } + }, { "command_started_event": { "command": { diff --git a/test/transactions/causal-consistency.json b/test/transactions/causal-consistency.json index 6374508a0..6fb2892dd 100644 --- a/test/transactions/causal-consistency.json +++ b/test/transactions/causal-consistency.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [ { "_id": 1, diff --git a/test/transactions/commit.json b/test/transactions/commit.json index 00af2b7ea..faa50fb18 100644 --- a/test/transactions/commit.json +++ b/test/transactions/commit.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [], "tests": [ { diff --git a/test/transactions/delete.json b/test/transactions/delete.json index a852c88f5..799da1c0e 100644 --- a/test/transactions/delete.json +++ b/test/transactions/delete.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [ { "_id": 1 diff --git a/test/transactions/errors.json b/test/transactions/errors.json index 7de861d96..a362745f8 100644 --- a/test/transactions/errors.json +++ b/test/transactions/errors.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [], "tests": [ { diff --git a/test/transactions/findOneAndDelete.json b/test/transactions/findOneAndDelete.json index 7064ba992..95160a638 100644 --- a/test/transactions/findOneAndDelete.json +++ b/test/transactions/findOneAndDelete.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [ { "_id": 1 diff --git a/test/transactions/findOneAndReplace.json b/test/transactions/findOneAndReplace.json index edfb572e2..e4193fc35 100644 --- a/test/transactions/findOneAndReplace.json +++ b/test/transactions/findOneAndReplace.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [ { "_id": 1 diff --git a/test/transactions/findOneAndUpdate.json b/test/transactions/findOneAndUpdate.json index cafaca212..434af067c 100644 --- a/test/transactions/findOneAndUpdate.json +++ b/test/transactions/findOneAndUpdate.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [ { "_id": 1 diff --git a/test/transactions/insert.json b/test/transactions/insert.json index aac31bd80..58ddb8292 100644 --- a/test/transactions/insert.json +++ b/test/transactions/insert.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [], "tests": [ { diff --git a/test/transactions/isolation.json b/test/transactions/isolation.json index 279699ff6..2c1217aff 100644 --- a/test/transactions/isolation.json +++ b/test/transactions/isolation.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [], "tests": [ { diff --git a/test/transactions/read-pref.json b/test/transactions/read-pref.json index b1db6bfbf..2931dd66a 100644 --- a/test/transactions/read-pref.json +++ b/test/transactions/read-pref.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [], "tests": [ { diff --git a/test/transactions/reads.json b/test/transactions/reads.json index 1f5431cb2..4fb5ea6b7 100644 --- a/test/transactions/reads.json +++ b/test/transactions/reads.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [ { "_id": 1 diff --git a/test/transactions/retryable-writes.json b/test/transactions/retryable-writes.json index 1066134bd..34926cc21 100644 --- a/test/transactions/retryable-writes.json +++ b/test/transactions/retryable-writes.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [], "tests": [ { diff --git a/test/transactions/transaction-options.json b/test/transactions/transaction-options.json index a50f35fdd..70d4f32b2 100644 --- a/test/transactions/transaction-options.json +++ b/test/transactions/transaction-options.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [], "tests": [ { diff --git a/test/transactions/update.json b/test/transactions/update.json index be929f338..4e58e9d5c 100644 --- a/test/transactions/update.json +++ b/test/transactions/update.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [ { "_id": 1 diff --git a/test/transactions/write-concern.json b/test/transactions/write-concern.json index 7d99e04d3..dc441823f 100644 --- a/test/transactions/write-concern.json +++ b/test/transactions/write-concern.json @@ -1,4 +1,6 @@ { + "database_name": "transaction-tests", + "collection_name": "test", "data": [], "tests": [ {