-
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
7e8f2c7
Migrate test_comment.py to async
sleepyStick 161547a
check if its coroutine and then await it
sleepyStick 0ecdd31
remove extra await
sleepyStick 2b4797e
jk wasn't extra, need to check, sometimes its need and sometimes its not
sleepyStick 84893dc
forgot to run precommit (again)
sleepyStick 858b805
added missing parens
sleepyStick 4120a15
fix coroutine function check
sleepyStick 715b031
Merge branch 'master' into PYTHON-4804
sleepyStick 9dac71a
accept suggested changes
sleepyStick 69b5ce0
Merge branch 'master' into PYTHON-4804
sleepyStick 9e0f409
remove Empty class and cleanup _test_ops funciton
sleepyStick 89dcfac
cleaned up _test_ops more
sleepyStick 979a5bf
change listener for tests
sleepyStick 2575651
Merge branch 'master' into PYTHON-4804
sleepyStick 91597c2
Merge branch 'master' into PYTHON-4804
sleepyStick d0e9d78
Merge branch 'master' into PYTHON-4804
sleepyStick 5763fcc
Merge branch 'master' into PYTHON-4804
sleepyStick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not _IS_SYNC and isinstance(coll, Empty): | ||
coll.create_index("a") | ||
else: | ||
await coll.create_index("a") | ||
if not _IS_SYNC and iscoroutinefunction(h): | ||
maybe_cursor = await h(*args, **kwargs) | ||
else: | ||
maybe_cursor = h(*args, **kwargs) | ||
self.assertIn( | ||
"comment", | ||
inspect.signature(h).parameters, | ||
msg="Could not find 'comment' in the " | ||
"signature of function %s" % (h.__name__), | ||
) | ||
self.assertEqual( | ||
inspect.signature(h).parameters["comment"].annotation, "Optional[Any]" | ||
) | ||
if isinstance(maybe_cursor, AsyncCommandCursor): | ||
await maybe_cursor.close() | ||
tested = False | ||
# For some reason collection.list_indexes creates two commands and the first | ||
# one doesn't contain 'comment'. | ||
for i in listener.started_events: | ||
if cc == i.command.get("comment", ""): | ||
self.assertEqual(cc, i.command["comment"]) | ||
tested = True | ||
self.assertTrue(tested) | ||
if h not in [coll.aggregate_raw_batches]: | ||
self.assertIn( | ||
":param comment:", | ||
h.__doc__, | ||
) | ||
if h not in already_supported: | ||
self.assertIn( | ||
"Added ``comment`` parameter", | ||
h.__doc__, | ||
) | ||
else: | ||
self.assertNotIn( | ||
"Added ``comment`` parameter", | ||
h.__doc__, | ||
) | ||
|
||
listener.reset() | ||
|
||
@async_client_context.require_version_min(4, 7, -1) | ||
@async_client_context.require_replica_set | ||
async def test_database_helpers(self): | ||
listener = EventListener() | ||
db = (await self.async_rs_or_single_client(event_listeners=[listener])).db | ||
helpers = [ | ||
(db.watch, []), | ||
(db.command, ["hello"]), | ||
(db.list_collections, []), | ||
(db.list_collection_names, []), | ||
(db.drop_collection, ["hello"]), | ||
(db.validate_collection, ["test"]), | ||
(db.dereference, [DBRef("collection", 1)]), | ||
] | ||
already_supported = [db.command, db.list_collections, db.list_collection_names] | ||
await self._test_ops( | ||
helpers, already_supported, listener, db=db, coll=db.get_collection("test") | ||
) | ||
|
||
@async_client_context.require_version_min(4, 7, -1) | ||
@async_client_context.require_replica_set | ||
async def test_client_helpers(self): | ||
listener = EventListener() | ||
cli = await self.async_rs_or_single_client(event_listeners=[listener]) | ||
helpers = [ | ||
(cli.watch, []), | ||
(cli.list_databases, []), | ||
(cli.list_database_names, []), | ||
(cli.drop_database, ["test"]), | ||
] | ||
already_supported = [ | ||
cli.list_databases, | ||
] | ||
await self._test_ops(helpers, already_supported, listener) | ||
|
||
@async_client_context.require_version_min(4, 7, -1) | ||
async def test_collection_helpers(self): | ||
listener = EventListener() | ||
db = (await self.async_rs_or_single_client(event_listeners=[listener]))[self.db.name] | ||
coll = db.get_collection("test") | ||
|
||
helpers = [ | ||
(coll.list_indexes, []), | ||
(coll.drop, []), | ||
(coll.index_information, []), | ||
(coll.options, []), | ||
(coll.aggregate, [[{"$set": {"x": 1}}]]), | ||
(coll.aggregate_raw_batches, [[{"$set": {"x": 1}}]]), | ||
(coll.rename, ["temp_temp_temp"]), | ||
(coll.distinct, ["_id"]), | ||
(coll.find_one_and_delete, [{}]), | ||
(coll.find_one_and_replace, [{}, {}]), | ||
(coll.find_one_and_update, [{}, {"$set": {"a": 1}}]), | ||
(coll.estimated_document_count, []), | ||
(coll.count_documents, [{}]), | ||
(coll.create_indexes, [[IndexModel("a")]]), | ||
(coll.create_index, ["a"]), | ||
(coll.drop_index, [[("a", 1)]]), | ||
(coll.drop_indexes, []), | ||
] | ||
already_supported = [ | ||
coll.estimated_document_count, | ||
coll.count_documents, | ||
coll.create_indexes, | ||
coll.drop_indexes, | ||
coll.options, | ||
coll.find_one_and_replace, | ||
coll.drop_index, | ||
coll.rename, | ||
coll.distinct, | ||
coll.find_one_and_delete, | ||
coll.find_one_and_update, | ||
] | ||
await self._test_ops(helpers, already_supported, listener, coll=coll, db=db) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | ||
|
@@ -56,7 +58,7 @@ def _test_ops( | |
listener.reset() | ||
kwargs = {"comment": cc} | ||
if h == coll.rename: | ||
_ = db.get_collection("temp_temp_temp").drop() | ||
db.get_collection("temp_temp_temp").drop() | ||
destruct_coll = db.get_collection("test_temp") | ||
destruct_coll.insert_one({}) | ||
maybe_cursor = destruct_coll.rename(*args, **kwargs) | ||
|
@@ -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 not _IS_SYNC and isinstance(coll, Empty): | ||
coll.create_index("a") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can we call create_index when coll is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thats a great question...HAHA code is removed now. |
||
else: | ||
coll.create_index("a") | ||
if not _IS_SYNC and 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 = [ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This Empty class looks very suspicious. What's the point of it?
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.
Uhhh honestly not sure. It was added in PYTHON-2682.
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.
cleaned up
_test_ops
and nowEmpty
isn't needed anymore :)