PYTHON-1508 Remove autoStartTransaction and resync tests

Also use the test file's database name and collection name.
This commit is contained in:
Shane Harvey 2018-05-22 12:09:11 -07:00
parent 335cb97a34
commit cac0d5548f
21 changed files with 84 additions and 796 deletions

View File

@ -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

View File

@ -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)

View File

@ -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')

View File

@ -1,4 +1,6 @@
{
"database_name": "transaction-tests",
"collection_name": "test",
"data": [],
"tests": [
{

View File

@ -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
}
]
}
}
}
]
}

View File

@ -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": {

View File

@ -1,4 +1,6 @@
{
"database_name": "transaction-tests",
"collection_name": "test",
"data": [
{
"_id": 1,

View File

@ -1,4 +1,6 @@
{
"database_name": "transaction-tests",
"collection_name": "test",
"data": [],
"tests": [
{

View File

@ -1,4 +1,6 @@
{
"database_name": "transaction-tests",
"collection_name": "test",
"data": [
{
"_id": 1

View File

@ -1,4 +1,6 @@
{
"database_name": "transaction-tests",
"collection_name": "test",
"data": [],
"tests": [
{

View File

@ -1,4 +1,6 @@
{
"database_name": "transaction-tests",
"collection_name": "test",
"data": [
{
"_id": 1

View File

@ -1,4 +1,6 @@
{
"database_name": "transaction-tests",
"collection_name": "test",
"data": [
{
"_id": 1

View File

@ -1,4 +1,6 @@
{
"database_name": "transaction-tests",
"collection_name": "test",
"data": [
{
"_id": 1

View File

@ -1,4 +1,6 @@
{
"database_name": "transaction-tests",
"collection_name": "test",
"data": [],
"tests": [
{

View File

@ -1,4 +1,6 @@
{
"database_name": "transaction-tests",
"collection_name": "test",
"data": [],
"tests": [
{

View File

@ -1,4 +1,6 @@
{
"database_name": "transaction-tests",
"collection_name": "test",
"data": [],
"tests": [
{

View File

@ -1,4 +1,6 @@
{
"database_name": "transaction-tests",
"collection_name": "test",
"data": [
{
"_id": 1

View File

@ -1,4 +1,6 @@
{
"database_name": "transaction-tests",
"collection_name": "test",
"data": [],
"tests": [
{

View File

@ -1,4 +1,6 @@
{
"database_name": "transaction-tests",
"collection_name": "test",
"data": [],
"tests": [
{

View File

@ -1,4 +1,6 @@
{
"database_name": "transaction-tests",
"collection_name": "test",
"data": [
{
"_id": 1

View File

@ -1,4 +1,6 @@
{
"database_name": "transaction-tests",
"collection_name": "test",
"data": [],
"tests": [
{