diff --git a/pymongo/message.py b/pymongo/message.py index d08636ff1..5a07d5d07 100644 --- a/pymongo/message.py +++ b/pymongo/message.py @@ -305,7 +305,7 @@ class _Query(object): self.name = 'explain' cmd = SON([('explain', cmd)]) session = self.session - sock_info.add_server_api(cmd, session) + sock_info.add_server_api(cmd) if session: session._apply_to(cmd, False, self.read_preference) # Explain does not support readConcern. @@ -409,6 +409,7 @@ class _GetMore(object): if self.session: self.session._apply_to(cmd, False, self.read_preference) + sock_info.add_server_api(cmd) sock_info.send_cluster_time(cmd, self.session, self.client) # Support auto encryption client = self.client @@ -891,7 +892,7 @@ class _BulkWriteContext(object): self.compress = True if sock_info.compression_context else False self.op_type = op_type self.codec = codec - sock_info.add_server_api(command, session) + sock_info.add_server_api(command) def _batch_command(self, docs): namespace = self.db_name + '.$cmd' diff --git a/pymongo/pool.py b/pymongo/pool.py index 7690bc8b4..a0dda5d9f 100644 --- a/pymongo/pool.py +++ b/pymongo/pool.py @@ -668,7 +668,7 @@ class SocketInfo(object): raise ConfigurationError( 'Must be connected to MongoDB 3.4+ to use a collation.') - self.add_server_api(spec, session) + self.add_server_api(spec) if session: session._apply_to(spec, retryable_write, read_preference) self.send_cluster_time(spec, session, client) @@ -846,11 +846,8 @@ class SocketInfo(object): if self.max_wire_version >= 6 and client: client._send_cluster_time(command, session) - def add_server_api(self, command, session): + def add_server_api(self, command): """Add server_api parameters.""" - if (session and session.in_transaction and - not session._starting_transaction): - return if self.opts.server_api: _add_to_command(command, self.opts.server_api) diff --git a/test/test_versioned_api.py b/test/test_versioned_api.py index fc7c919af..27a1dc7fe 100644 --- a/test/test_versioned_api.py +++ b/test/test_versioned_api.py @@ -68,10 +68,9 @@ class TestServerApi(IntegrationTest): def assertNoServerApi(self, event): self.assertNotIn('apiVersion', event.command) - def assertServerApiOnlyInFirstCommand(self, events): - self.assertServerApi(events[0]) - for event in events[1:]: - self.assertNoServerApi(event) + def assertServerApiInAllCommands(self, events): + for event in events: + self.assertServerApi(event) @client_context.require_version_min(4, 7) def test_command_options(self): @@ -84,11 +83,7 @@ class TestServerApi(IntegrationTest): self.addCleanup(coll.delete_many, {}) list(coll.find(batch_size=25)) client.admin.command('ping') - for event in listener.results['started']: - if event.command_name == 'getMore': - self.assertNoServerApi(event) - else: - self.assertServerApi(event) + self.assertServerApiInAllCommands(listener.results['started']) @client_context.require_version_min(4, 7) @client_context.require_transactions @@ -106,21 +101,7 @@ class TestServerApi(IntegrationTest): coll.insert_many([{} for _ in range(100)], session=s) list(coll.find(batch_size=25, session=s)) client.test.command('find', 'test', session=s) - self.assertServerApiOnlyInFirstCommand(listener.results['started']) - - listener.reset() - with client.start_session() as s, s.start_transaction(): - list(coll.find(batch_size=25, session=s)) - coll.insert_many([{} for _ in range(100)], session=s) - client.test.command('find', 'test', session=s) - self.assertServerApiOnlyInFirstCommand(listener.results['started']) - - listener.reset() - with client.start_session() as s, s.start_transaction(): - client.test.command('find', 'test', session=s) - list(coll.find(batch_size=25, session=s)) - coll.insert_many([{} for _ in range(100)], session=s) - self.assertServerApiOnlyInFirstCommand(listener.results['started']) + self.assertServerApiInAllCommands(listener.results['started']) if __name__ == "__main__": diff --git a/test/unified_format.py b/test/unified_format.py index 3a5e5782c..0aea11b7a 100644 --- a/test/unified_format.py +++ b/test/unified_format.py @@ -520,7 +520,7 @@ class UnifiedSpecTestMixinV1(IntegrationTest): Specification of the test suite being currently run is available as a class attribute ``TEST_SPEC``. """ - SCHEMA_VERSION = Version.from_string('1.1') + SCHEMA_VERSION = Version.from_string('1.4') @staticmethod def should_run_on(run_on_spec): diff --git a/test/versioned-api/crud-api-version-1-strict.json b/test/versioned-api/crud-api-version-1-strict.json index 2705b505a..29a0ec4e3 100644 --- a/test/versioned-api/crud-api-version-1-strict.json +++ b/test/versioned-api/crud-api-version-1-strict.json @@ -1,6 +1,6 @@ { "description": "CRUD Api Version 1 (strict)", - "schemaVersion": "1.1", + "schemaVersion": "1.4", "runOnRequirements": [ { "minServerVersion": "4.9" @@ -141,6 +141,11 @@ }, { "description": "aggregate on database appends declared API version", + "runOnRequirements": [ + { + "serverless": "forbid" + } + ], "operations": [ { "name": "aggregate", @@ -651,7 +656,7 @@ ] }, { - "description": "find command with declared API version appends to the command, but getMore does not", + "description": "find and getMore append API version", "operations": [ { "name": "find", @@ -712,14 +717,10 @@ "long" ] }, - "apiVersion": { - "$$exists": false - }, - "apiStrict": { - "$$exists": false - }, + "apiVersion": "1", + "apiStrict": true, "apiDeprecationErrors": { - "$$exists": false + "$$unsetOrMatches": false } } } diff --git a/test/versioned-api/crud-api-version-1.json b/test/versioned-api/crud-api-version-1.json index 917185837..1f135eea1 100644 --- a/test/versioned-api/crud-api-version-1.json +++ b/test/versioned-api/crud-api-version-1.json @@ -1,6 +1,6 @@ { "description": "CRUD Api Version 1", - "schemaVersion": "1.1", + "schemaVersion": "1.4", "runOnRequirements": [ { "minServerVersion": "4.9" @@ -141,6 +141,11 @@ }, { "description": "aggregate on database appends declared API version", + "runOnRequirements": [ + { + "serverless": "forbid" + } + ], "operations": [ { "name": "aggregate", @@ -643,7 +648,7 @@ ] }, { - "description": "find command with declared API version appends to the command, but getMore does not", + "description": "find and getMore append API version", "operations": [ { "name": "find", @@ -704,15 +709,11 @@ "long" ] }, - "apiVersion": { - "$$exists": false - }, + "apiVersion": "1", "apiStrict": { - "$$exists": false + "$$unsetOrMatches": false }, - "apiDeprecationErrors": { - "$$exists": false - } + "apiDeprecationErrors": true } } } diff --git a/test/versioned-api/runcommand-helper-no-api-version-declared.json b/test/versioned-api/runcommand-helper-no-api-version-declared.json index e901887e4..17e0126d1 100644 --- a/test/versioned-api/runcommand-helper-no-api-version-declared.json +++ b/test/versioned-api/runcommand-helper-no-api-version-declared.json @@ -1,6 +1,6 @@ { "description": "RunCommand helper: No API version declared", - "schemaVersion": "1.1", + "schemaVersion": "1.4", "runOnRequirements": [ { "minServerVersion": "4.9", @@ -29,6 +29,11 @@ "tests": [ { "description": "runCommand does not inspect or change the command document", + "runOnRequirements": [ + { + "serverless": "forbid" + } + ], "operations": [ { "name": "runCommand", @@ -72,6 +77,11 @@ }, { "description": "runCommand does not prevent sending invalid API version declarations", + "runOnRequirements": [ + { + "serverless": "forbid" + } + ], "operations": [ { "name": "runCommand", diff --git a/test/versioned-api/test-commands-strict-mode.json b/test/versioned-api/test-commands-strict-mode.json index 1705ba7bf..9c4ebea78 100644 --- a/test/versioned-api/test-commands-strict-mode.json +++ b/test/versioned-api/test-commands-strict-mode.json @@ -1,12 +1,13 @@ { "description": "Test commands: strict mode", - "schemaVersion": "1.1", + "schemaVersion": "1.4", "runOnRequirements": [ { "minServerVersion": "4.9", "serverParameters": { "enableTestCommands": true - } + }, + "serverless": "forbid" } ], "createEntities": [ diff --git a/test/versioned-api/transaction-handling.json b/test/versioned-api/transaction-handling.json index a740405d3..5c627bb35 100644 --- a/test/versioned-api/transaction-handling.json +++ b/test/versioned-api/transaction-handling.json @@ -6,7 +6,8 @@ "minServerVersion": "4.9", "topologies": [ "replicaset", - "sharded-replicaset" + "sharded-replicaset", + "load-balanced" ] } ], @@ -53,17 +54,6 @@ "apiDeprecationErrors": { "$$unsetOrMatches": false } - }, - { - "apiVersion": { - "$$exists": false - }, - "apiStrict": { - "$$exists": false - }, - "apiDeprecationErrors": { - "$$exists": false - } } ] }, @@ -97,12 +87,13 @@ ], "tests": [ { - "description": "Only the first command in a transaction declares an API version", + "description": "All commands in a transaction declare an API version", "runOnRequirements": [ { "topologies": [ "replicaset", - "sharded-replicaset" + "sharded-replicaset", + "load-balanced" ] } ], @@ -193,119 +184,6 @@ "lsid": { "$$sessionLsid": "session" }, - "apiVersion": { - "$$exists": false - }, - "apiStrict": { - "$$exists": false - }, - "apiDeprecationErrors": { - "$$exists": false - } - } - } - }, - { - "commandStartedEvent": { - "command": { - "commitTransaction": 1, - "lsid": { - "$$sessionLsid": "session" - }, - "apiVersion": { - "$$exists": false - }, - "apiStrict": { - "$$exists": false - }, - "apiDeprecationErrors": { - "$$exists": false - } - } - } - } - ] - } - ] - }, - { - "description": "Committing a transaction twice does not append server API options", - "runOnRequirements": [ - { - "topologies": [ - "replicaset", - "sharded-replicaset" - ] - } - ], - "operations": [ - { - "name": "startTransaction", - "object": "session" - }, - { - "name": "insertOne", - "object": "collection", - "arguments": { - "session": "session", - "document": { - "_id": 6, - "x": 66 - } - }, - "expectResult": { - "$$unsetOrMatches": { - "insertedId": { - "$$unsetOrMatches": 6 - } - } - } - }, - { - "name": "insertOne", - "object": "collection", - "arguments": { - "session": "session", - "document": { - "_id": 7, - "x": 77 - } - }, - "expectResult": { - "$$unsetOrMatches": { - "insertedId": { - "$$unsetOrMatches": 7 - } - } - } - }, - { - "name": "commitTransaction", - "object": "session" - }, - { - "name": "commitTransaction", - "object": "session" - } - ], - "expectEvents": [ - { - "client": "client", - "events": [ - { - "commandStartedEvent": { - "command": { - "insert": "test", - "documents": [ - { - "_id": 6, - "x": 66 - } - ], - "lsid": { - "$$sessionLsid": "session" - }, - "startTransaction": true, "apiVersion": "1", "apiStrict": { "$$unsetOrMatches": false @@ -316,31 +194,6 @@ } } }, - { - "commandStartedEvent": { - "command": { - "insert": "test", - "documents": [ - { - "_id": 7, - "x": 77 - } - ], - "lsid": { - "$$sessionLsid": "session" - }, - "apiVersion": { - "$$exists": false - }, - "apiStrict": { - "$$exists": false - }, - "apiDeprecationErrors": { - "$$exists": false - } - } - } - }, { "commandStartedEvent": { "command": { @@ -348,33 +201,12 @@ "lsid": { "$$sessionLsid": "session" }, - "apiVersion": { - "$$exists": false - }, + "apiVersion": "1", "apiStrict": { - "$$exists": false + "$$unsetOrMatches": false }, "apiDeprecationErrors": { - "$$exists": false - } - } - } - }, - { - "commandStartedEvent": { - "command": { - "commitTransaction": 1, - "lsid": { - "$$sessionLsid": "session" - }, - "apiVersion": { - "$$exists": false - }, - "apiStrict": { - "$$exists": false - }, - "apiDeprecationErrors": { - "$$exists": false + "$$unsetOrMatches": false } } } @@ -384,12 +216,13 @@ ] }, { - "description": "abortTransaction does not include an API version", + "description": "abortTransaction includes an API version", "runOnRequirements": [ { "topologies": [ "replicaset", - "sharded-replicaset" + "sharded-replicaset", + "load-balanced" ] } ], @@ -480,14 +313,12 @@ "lsid": { "$$sessionLsid": "session" }, - "apiVersion": { - "$$exists": false - }, + "apiVersion": "1", "apiStrict": { - "$$exists": false + "$$unsetOrMatches": false }, "apiDeprecationErrors": { - "$$exists": false + "$$unsetOrMatches": false } } } @@ -499,14 +330,12 @@ "lsid": { "$$sessionLsid": "session" }, - "apiVersion": { - "$$exists": false - }, + "apiVersion": "1", "apiStrict": { - "$$exists": false + "$$unsetOrMatches": false }, "apiDeprecationErrors": { - "$$exists": false + "$$unsetOrMatches": false } } }