Skip to content

Commit f344eb7

Browse files
authored
PYTHON-5109 - Convert test.test_versioned_api to async (#2129)
1 parent ac8fa2d commit f344eb7

File tree

4 files changed

+173
-43
lines changed

4 files changed

+173
-43
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Copyright 2020-present MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from __future__ import annotations
15+
16+
import os
17+
import sys
18+
from pathlib import Path
19+
from test.asynchronous.unified_format import generate_test_classes
20+
21+
sys.path[0:0] = [""]
22+
23+
from test.asynchronous import AsyncIntegrationTest, async_client_context, unittest
24+
from test.utils import OvertCommandListener
25+
26+
from pymongo.server_api import ServerApi
27+
28+
_IS_SYNC = False
29+
30+
# Location of JSON test specifications.
31+
if _IS_SYNC:
32+
TEST_PATH = os.path.join(Path(__file__).resolve().parent, "versioned-api")
33+
else:
34+
TEST_PATH = os.path.join(Path(__file__).resolve().parent.parent, "versioned-api")
35+
36+
37+
# Generate unified tests.
38+
globals().update(generate_test_classes(TEST_PATH, module=__name__))
39+
40+
41+
class TestServerApiIntegration(AsyncIntegrationTest):
42+
RUN_ON_LOAD_BALANCER = True
43+
RUN_ON_SERVERLESS = True
44+
45+
def assertServerApi(self, event):
46+
self.assertIn("apiVersion", event.command)
47+
self.assertEqual(event.command["apiVersion"], "1")
48+
49+
def assertServerApiInAllCommands(self, events):
50+
for event in events:
51+
self.assertServerApi(event)
52+
53+
@async_client_context.require_version_min(4, 7)
54+
async def test_command_options(self):
55+
listener = OvertCommandListener()
56+
client = await self.async_rs_or_single_client(
57+
server_api=ServerApi("1"), event_listeners=[listener]
58+
)
59+
coll = client.test.test
60+
await coll.insert_many([{} for _ in range(100)])
61+
self.addAsyncCleanup(coll.delete_many, {})
62+
await coll.find(batch_size=25).to_list()
63+
await client.admin.command("ping")
64+
self.assertServerApiInAllCommands(listener.started_events)
65+
66+
@async_client_context.require_version_min(4, 7)
67+
@async_client_context.require_transactions
68+
async def test_command_options_txn(self):
69+
listener = OvertCommandListener()
70+
client = await self.async_rs_or_single_client(
71+
server_api=ServerApi("1"), event_listeners=[listener]
72+
)
73+
coll = client.test.test
74+
await coll.insert_many([{} for _ in range(100)])
75+
self.addAsyncCleanup(coll.delete_many, {})
76+
77+
listener.reset()
78+
async with client.start_session() as s, await s.start_transaction():
79+
await coll.insert_many([{} for _ in range(100)], session=s)
80+
await coll.find(batch_size=25, session=s).to_list()
81+
await client.test.command("find", "test", session=s)
82+
self.assertServerApiInAllCommands(listener.started_events)
83+
84+
85+
if __name__ == "__main__":
86+
unittest.main()

test/test_versioned_api.py

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,18 @@
1313
# limitations under the License.
1414
from __future__ import annotations
1515

16-
import os
1716
import sys
17+
from test import UnitTest
1818

1919
sys.path[0:0] = [""]
2020

21-
from test import IntegrationTest, client_context, unittest
22-
from test.unified_format import generate_test_classes
23-
from test.utils import OvertCommandListener
21+
from test import unittest
2422

23+
from pymongo.mongo_client import MongoClient
2524
from pymongo.server_api import ServerApi, ServerApiVersion
26-
from pymongo.synchronous.mongo_client import MongoClient
2725

28-
TEST_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "versioned-api")
29-
30-
# Generate unified tests.
31-
globals().update(generate_test_classes(TEST_PATH, module=__name__))
32-
33-
34-
class TestServerApi(IntegrationTest):
35-
RUN_ON_LOAD_BALANCER = True
36-
RUN_ON_SERVERLESS = True
3726

27+
class TestServerApi(UnitTest):
3828
def test_server_api_defaults(self):
3929
api = ServerApi(ServerApiVersion.V1)
4030
self.assertEqual(api.version, "1")
@@ -74,35 +64,6 @@ def assertServerApiInAllCommands(self, events):
7464
for event in events:
7565
self.assertServerApi(event)
7666

77-
@client_context.require_version_min(4, 7)
78-
def test_command_options(self):
79-
listener = OvertCommandListener()
80-
client = self.rs_or_single_client(server_api=ServerApi("1"), event_listeners=[listener])
81-
self.addCleanup(client.close)
82-
coll = client.test.test
83-
coll.insert_many([{} for _ in range(100)])
84-
self.addCleanup(coll.delete_many, {})
85-
list(coll.find(batch_size=25))
86-
client.admin.command("ping")
87-
self.assertServerApiInAllCommands(listener.started_events)
88-
89-
@client_context.require_version_min(4, 7)
90-
@client_context.require_transactions
91-
def test_command_options_txn(self):
92-
listener = OvertCommandListener()
93-
client = self.rs_or_single_client(server_api=ServerApi("1"), event_listeners=[listener])
94-
self.addCleanup(client.close)
95-
coll = client.test.test
96-
coll.insert_many([{} for _ in range(100)])
97-
self.addCleanup(coll.delete_many, {})
98-
99-
listener.reset()
100-
with client.start_session() as s, s.start_transaction():
101-
coll.insert_many([{} for _ in range(100)], session=s)
102-
list(coll.find(batch_size=25, session=s))
103-
client.test.command("find", "test", session=s)
104-
self.assertServerApiInAllCommands(listener.started_events)
105-
10667

10768
if __name__ == "__main__":
10869
unittest.main()
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Copyright 2020-present MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from __future__ import annotations
15+
16+
import os
17+
import sys
18+
from pathlib import Path
19+
from test.unified_format import generate_test_classes
20+
21+
sys.path[0:0] = [""]
22+
23+
from test import IntegrationTest, client_context, unittest
24+
from test.utils import OvertCommandListener
25+
26+
from pymongo.server_api import ServerApi
27+
28+
_IS_SYNC = True
29+
30+
# Location of JSON test specifications.
31+
if _IS_SYNC:
32+
TEST_PATH = os.path.join(Path(__file__).resolve().parent, "versioned-api")
33+
else:
34+
TEST_PATH = os.path.join(Path(__file__).resolve().parent.parent, "versioned-api")
35+
36+
37+
# Generate unified tests.
38+
globals().update(generate_test_classes(TEST_PATH, module=__name__))
39+
40+
41+
class TestServerApiIntegration(IntegrationTest):
42+
RUN_ON_LOAD_BALANCER = True
43+
RUN_ON_SERVERLESS = True
44+
45+
def assertServerApi(self, event):
46+
self.assertIn("apiVersion", event.command)
47+
self.assertEqual(event.command["apiVersion"], "1")
48+
49+
def assertServerApiInAllCommands(self, events):
50+
for event in events:
51+
self.assertServerApi(event)
52+
53+
@client_context.require_version_min(4, 7)
54+
def test_command_options(self):
55+
listener = OvertCommandListener()
56+
client = self.rs_or_single_client(server_api=ServerApi("1"), event_listeners=[listener])
57+
coll = client.test.test
58+
coll.insert_many([{} for _ in range(100)])
59+
self.addCleanup(coll.delete_many, {})
60+
coll.find(batch_size=25).to_list()
61+
client.admin.command("ping")
62+
self.assertServerApiInAllCommands(listener.started_events)
63+
64+
@client_context.require_version_min(4, 7)
65+
@client_context.require_transactions
66+
def test_command_options_txn(self):
67+
listener = OvertCommandListener()
68+
client = self.rs_or_single_client(server_api=ServerApi("1"), event_listeners=[listener])
69+
coll = client.test.test
70+
coll.insert_many([{} for _ in range(100)])
71+
self.addCleanup(coll.delete_many, {})
72+
73+
listener.reset()
74+
with client.start_session() as s, s.start_transaction():
75+
coll.insert_many([{} for _ in range(100)], session=s)
76+
coll.find(batch_size=25, session=s).to_list()
77+
client.test.command("find", "test", session=s)
78+
self.assertServerApiInAllCommands(listener.started_events)
79+
80+
81+
if __name__ == "__main__":
82+
unittest.main()

tools/synchro.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ def async_only_test(f: str) -> bool:
239239
"test_transactions.py",
240240
"test_transactions_unified.py",
241241
"test_unified_format.py",
242+
"test_versioned_api_integration.py",
242243
"unified_format.py",
243244
]
244245

0 commit comments

Comments
 (0)