Skip to content

Commit 2f4a7c8

Browse files
committed
PYTHON-2684 Send Versioned API options with getMore+txn commands (#618)
(cherry picked from commit e221b49)
1 parent 2c676bc commit 2f4a7c8

9 files changed

+62
-241
lines changed

pymongo/message.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def as_command(self, sock_info):
307307
self.name = 'explain'
308308
cmd = SON([('explain', cmd)])
309309
session = self.session
310-
sock_info.add_server_api(cmd, session)
310+
sock_info.add_server_api(cmd)
311311
if session:
312312
session._apply_to(cmd, False, self.read_preference)
313313
# Explain does not support readConcern.
@@ -411,6 +411,7 @@ def as_command(self, sock_info):
411411

412412
if self.session:
413413
self.session._apply_to(cmd, False, self.read_preference)
414+
sock_info.add_server_api(cmd)
414415
sock_info.send_cluster_time(cmd, self.session, self.client)
415416
# Support auto encryption
416417
client = self.client
@@ -893,7 +894,7 @@ def __init__(self, database_name, command, sock_info, operation_id,
893894
self.compress = True if sock_info.compression_context else False
894895
self.op_type = op_type
895896
self.codec = codec
896-
sock_info.add_server_api(command, session)
897+
sock_info.add_server_api(command)
897898

898899
def _batch_command(self, docs):
899900
namespace = self.db_name + '.$cmd'

pymongo/pool.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ def command(self, dbname, spec, slave_ok=False,
686686
raise ConfigurationError(
687687
'Must be connected to MongoDB 3.4+ to use a collation.')
688688

689-
self.add_server_api(spec, session)
689+
self.add_server_api(spec)
690690
if session:
691691
session._apply_to(spec, retryable_write, read_preference)
692692
self.send_cluster_time(spec, session, client)
@@ -876,11 +876,8 @@ def send_cluster_time(self, command, session, client):
876876
if self.max_wire_version >= 6 and client:
877877
client._send_cluster_time(command, session)
878878

879-
def add_server_api(self, command, session):
879+
def add_server_api(self, command):
880880
"""Add server_api parameters."""
881-
if (session and session.in_transaction and
882-
not session._starting_transaction):
883-
return
884881
if self.opts.server_api:
885882
_add_to_command(command, self.opts.server_api)
886883

test/test_versioned_api.py

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ def assertServerApi(self, event):
6868
def assertNoServerApi(self, event):
6969
self.assertNotIn('apiVersion', event.command)
7070

71-
def assertServerApiOnlyInFirstCommand(self, events):
72-
self.assertServerApi(events[0])
73-
for event in events[1:]:
74-
self.assertNoServerApi(event)
71+
def assertServerApiInAllCommands(self, events):
72+
for event in events:
73+
self.assertServerApi(event)
7574

7675
@client_context.require_version_min(4, 7)
7776
def test_command_options(self):
@@ -84,11 +83,7 @@ def test_command_options(self):
8483
self.addCleanup(coll.delete_many, {})
8584
list(coll.find(batch_size=25))
8685
client.admin.command('ping')
87-
for event in listener.results['started']:
88-
if event.command_name == 'getMore':
89-
self.assertNoServerApi(event)
90-
else:
91-
self.assertServerApi(event)
86+
self.assertServerApiInAllCommands(listener.results['started'])
9287

9388
@client_context.require_version_min(4, 7)
9489
@client_context.require_transactions
@@ -106,21 +101,7 @@ def test_command_options_txn(self):
106101
coll.insert_many([{} for _ in range(100)], session=s)
107102
list(coll.find(batch_size=25, session=s))
108103
client.test.command('find', 'test', session=s)
109-
self.assertServerApiOnlyInFirstCommand(listener.results['started'])
110-
111-
listener.reset()
112-
with client.start_session() as s, s.start_transaction():
113-
list(coll.find(batch_size=25, session=s))
114-
coll.insert_many([{} for _ in range(100)], session=s)
115-
client.test.command('find', 'test', session=s)
116-
self.assertServerApiOnlyInFirstCommand(listener.results['started'])
117-
118-
listener.reset()
119-
with client.start_session() as s, s.start_transaction():
120-
client.test.command('find', 'test', session=s)
121-
list(coll.find(batch_size=25, session=s))
122-
coll.insert_many([{} for _ in range(100)], session=s)
123-
self.assertServerApiOnlyInFirstCommand(listener.results['started'])
104+
self.assertServerApiInAllCommands(listener.results['started'])
124105

125106

126107
if __name__ == "__main__":

test/unified_format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ class UnifiedSpecTestMixinV1(IntegrationTest):
524524
Specification of the test suite being currently run is available as
525525
a class attribute ``TEST_SPEC``.
526526
"""
527-
SCHEMA_VERSION = Version.from_string('1.1')
527+
SCHEMA_VERSION = Version.from_string('1.4')
528528

529529
@staticmethod
530530
def should_run_on(run_on_spec):

test/versioned-api/crud-api-version-1-strict.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "CRUD Api Version 1 (strict)",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.4",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.9"
@@ -141,6 +141,11 @@
141141
},
142142
{
143143
"description": "aggregate on database appends declared API version",
144+
"runOnRequirements": [
145+
{
146+
"serverless": "forbid"
147+
}
148+
],
144149
"operations": [
145150
{
146151
"name": "aggregate",
@@ -651,7 +656,7 @@
651656
]
652657
},
653658
{
654-
"description": "find command with declared API version appends to the command, but getMore does not",
659+
"description": "find and getMore append API version",
655660
"operations": [
656661
{
657662
"name": "find",
@@ -712,14 +717,10 @@
712717
"long"
713718
]
714719
},
715-
"apiVersion": {
716-
"$$exists": false
717-
},
718-
"apiStrict": {
719-
"$$exists": false
720-
},
720+
"apiVersion": "1",
721+
"apiStrict": true,
721722
"apiDeprecationErrors": {
722-
"$$exists": false
723+
"$$unsetOrMatches": false
723724
}
724725
}
725726
}

test/versioned-api/crud-api-version-1.json

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "CRUD Api Version 1",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.4",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.9"
@@ -141,6 +141,11 @@
141141
},
142142
{
143143
"description": "aggregate on database appends declared API version",
144+
"runOnRequirements": [
145+
{
146+
"serverless": "forbid"
147+
}
148+
],
144149
"operations": [
145150
{
146151
"name": "aggregate",
@@ -643,7 +648,7 @@
643648
]
644649
},
645650
{
646-
"description": "find command with declared API version appends to the command, but getMore does not",
651+
"description": "find and getMore append API version",
647652
"operations": [
648653
{
649654
"name": "find",
@@ -704,15 +709,11 @@
704709
"long"
705710
]
706711
},
707-
"apiVersion": {
708-
"$$exists": false
709-
},
712+
"apiVersion": "1",
710713
"apiStrict": {
711-
"$$exists": false
714+
"$$unsetOrMatches": false
712715
},
713-
"apiDeprecationErrors": {
714-
"$$exists": false
715-
}
716+
"apiDeprecationErrors": true
716717
}
717718
}
718719
}

test/versioned-api/runcommand-helper-no-api-version-declared.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"description": "RunCommand helper: No API version declared",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.4",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.9",
@@ -29,6 +29,11 @@
2929
"tests": [
3030
{
3131
"description": "runCommand does not inspect or change the command document",
32+
"runOnRequirements": [
33+
{
34+
"serverless": "forbid"
35+
}
36+
],
3237
"operations": [
3338
{
3439
"name": "runCommand",
@@ -72,6 +77,11 @@
7277
},
7378
{
7479
"description": "runCommand does not prevent sending invalid API version declarations",
80+
"runOnRequirements": [
81+
{
82+
"serverless": "forbid"
83+
}
84+
],
7585
"operations": [
7686
{
7787
"name": "runCommand",

test/versioned-api/test-commands-strict-mode.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"description": "Test commands: strict mode",
3-
"schemaVersion": "1.1",
3+
"schemaVersion": "1.4",
44
"runOnRequirements": [
55
{
66
"minServerVersion": "4.9",
77
"serverParameters": {
88
"enableTestCommands": true
9-
}
9+
},
10+
"serverless": "forbid"
1011
}
1112
],
1213
"createEntities": [

0 commit comments

Comments
 (0)