-
Notifications
You must be signed in to change notification settings - Fork 1.1k
PYTHON-4804 Migrate test_comment.py to async #1887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
7e8f2c7
161547a
0ecdd31
2b4797e
84893dc
858b805
4120a15
715b031
9dac71a
69b5ce0
9e0f409
89dcfac
979a5bf
2575651
91597c2
d0e9d78
5763fcc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,193 @@ | ||||||||||||||||||
# Copyright 2022-present MongoDB, Inc. | ||||||||||||||||||
# | ||||||||||||||||||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||
# you may not use this file except in compliance with the License. | ||||||||||||||||||
# You may obtain a copy of the License at | ||||||||||||||||||
# | ||||||||||||||||||
# http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||
# | ||||||||||||||||||
# Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||
# See the License for the specific language governing permissions and | ||||||||||||||||||
# limitations under the License. | ||||||||||||||||||
|
||||||||||||||||||
"""Test the keyword argument 'comment' in various helpers.""" | ||||||||||||||||||
|
||||||||||||||||||
from __future__ import annotations | ||||||||||||||||||
|
||||||||||||||||||
import inspect | ||||||||||||||||||
import sys | ||||||||||||||||||
|
||||||||||||||||||
sys.path[0:0] = [""] | ||||||||||||||||||
from asyncio import iscoroutinefunction | ||||||||||||||||||
from test.asynchronous import AsyncIntegrationTest, async_client_context, unittest | ||||||||||||||||||
from test.utils import EventListener | ||||||||||||||||||
|
||||||||||||||||||
from bson.dbref import DBRef | ||||||||||||||||||
from pymongo.asynchronous.command_cursor import AsyncCommandCursor | ||||||||||||||||||
from pymongo.operations import IndexModel | ||||||||||||||||||
|
||||||||||||||||||
_IS_SYNC = False | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class Empty: | ||||||||||||||||||
def __getattr__(self, item): | ||||||||||||||||||
try: | ||||||||||||||||||
self.__dict__[item] | ||||||||||||||||||
except KeyError: | ||||||||||||||||||
return self.empty | ||||||||||||||||||
|
||||||||||||||||||
def empty(self, *args, **kwargs): | ||||||||||||||||||
return Empty() | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
class AsyncTestComment(AsyncIntegrationTest): | ||||||||||||||||||
async def _test_ops( | ||||||||||||||||||
self, | ||||||||||||||||||
helpers, | ||||||||||||||||||
already_supported, | ||||||||||||||||||
listener, | ||||||||||||||||||
db=Empty(), # noqa: B008 | ||||||||||||||||||
coll=Empty(), # noqa: B008 | ||||||||||||||||||
): | ||||||||||||||||||
for h, args in helpers: | ||||||||||||||||||
c = "testing comment with " + h.__name__ | ||||||||||||||||||
with self.subTest("collection-" + h.__name__ + "-comment"): | ||||||||||||||||||
for cc in [c, {"key": c}, ["any", 1]]: | ||||||||||||||||||
listener.reset() | ||||||||||||||||||
kwargs = {"comment": cc} | ||||||||||||||||||
if h == coll.rename: | ||||||||||||||||||
_ = await db.get_collection("temp_temp_temp").drop() | ||||||||||||||||||
destruct_coll = db.get_collection("test_temp") | ||||||||||||||||||
await destruct_coll.insert_one({}) | ||||||||||||||||||
maybe_cursor = await destruct_coll.rename(*args, **kwargs) | ||||||||||||||||||
await destruct_coll.drop() | ||||||||||||||||||
elif h == db.validate_collection: | ||||||||||||||||||
coll = db.get_collection("test") | ||||||||||||||||||
await coll.insert_one({}) | ||||||||||||||||||
maybe_cursor = await db.validate_collection(*args, **kwargs) | ||||||||||||||||||
else: | ||||||||||||||||||
if iscoroutinefunction(coll.create_index): | ||||||||||||||||||
await coll.create_index("a") | ||||||||||||||||||
else: | ||||||||||||||||||
coll.create_index("a") | ||||||||||||||||||
|
if iscoroutinefunction(coll.create_index): | |
await coll.create_index("a") | |
else: | |
coll.create_index("a") | |
if not _IS_SYNC and isinstance(coll, Empty): | |
coll.create_index("a") | |
else: | |
await coll.create_index("a") |
For clarity so unfamiliar readers can understand why coll.create_index
could be synchronous.
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if iscoroutinefunction(h): | |
maybe_cursor = await h(*args, **kwargs) | |
else: | |
maybe_cursor = h(*args, **kwargs) | |
if not _IS_SYNC and iscoroutinefunction(h): | |
maybe_cursor = await h(*args, **kwargs) | |
else: | |
maybe_cursor = h(*args, **kwargs) |
Same here.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,14 +20,16 @@ | |
import sys | ||
|
||
sys.path[0:0] = [""] | ||
|
||
from asyncio import iscoroutinefunction | ||
from test import IntegrationTest, client_context, unittest | ||
from test.utils import EventListener | ||
|
||
from bson.dbref import DBRef | ||
from pymongo.operations import IndexModel | ||
from pymongo.synchronous.command_cursor import CommandCursor | ||
|
||
_IS_SYNC = True | ||
|
||
|
||
class Empty: | ||
|
||
def __getattr__(self, item): | ||
|
@@ -66,8 +68,14 @@ def _test_ops( | |
coll.insert_one({}) | ||
maybe_cursor = db.validate_collection(*args, **kwargs) | ||
else: | ||
coll.create_index("a") | ||
maybe_cursor = h(*args, **kwargs) | ||
if iscoroutinefunction(coll.create_index): | ||
coll.create_index("a") | ||
|
||
else: | ||
coll.create_index("a") | ||
if iscoroutinefunction(h): | ||
maybe_cursor = h(*args, **kwargs) | ||
else: | ||
maybe_cursor = h(*args, **kwargs) | ||
self.assertIn( | ||
"comment", | ||
inspect.signature(h).parameters, | ||
|
@@ -109,7 +117,7 @@ def _test_ops( | |
@client_context.require_replica_set | ||
def test_database_helpers(self): | ||
listener = EventListener() | ||
db = self.rs_or_single_client(event_listeners=[listener]).db | ||
db = (self.rs_or_single_client(event_listeners=[listener])).db | ||
helpers = [ | ||
(db.watch, []), | ||
(db.command, ["hello"]), | ||
|
@@ -141,7 +149,7 @@ def test_client_helpers(self): | |
@client_context.require_version_min(4, 7, -1) | ||
def test_collection_helpers(self): | ||
listener = EventListener() | ||
db = self.rs_or_single_client(event_listeners=[listener])[self.db.name] | ||
db = (self.rs_or_single_client(event_listeners=[listener]))[self.db.name] | ||
coll = db.get_collection("test") | ||
|
||
helpers = [ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this underscore needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably not! just removed it :)