|
| 1 | +# Copyright 2022-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 | + |
| 15 | +"""Test the keyword argument 'comment' in various helpers.""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import inspect |
| 20 | +import sys |
| 21 | + |
| 22 | +sys.path[0:0] = [""] |
| 23 | +from asyncio import iscoroutinefunction |
| 24 | +from test.asynchronous import AsyncIntegrationTest, async_client_context, unittest |
| 25 | +from test.utils import OvertCommandListener |
| 26 | + |
| 27 | +from bson.dbref import DBRef |
| 28 | +from pymongo.asynchronous.command_cursor import AsyncCommandCursor |
| 29 | +from pymongo.operations import IndexModel |
| 30 | + |
| 31 | +_IS_SYNC = False |
| 32 | + |
| 33 | + |
| 34 | +class AsyncTestComment(AsyncIntegrationTest): |
| 35 | + async def _test_ops( |
| 36 | + self, |
| 37 | + helpers, |
| 38 | + already_supported, |
| 39 | + listener, |
| 40 | + ): |
| 41 | + for h, args in helpers: |
| 42 | + c = "testing comment with " + h.__name__ |
| 43 | + with self.subTest("collection-" + h.__name__ + "-comment"): |
| 44 | + for cc in [c, {"key": c}, ["any", 1]]: |
| 45 | + listener.reset() |
| 46 | + kwargs = {"comment": cc} |
| 47 | + try: |
| 48 | + maybe_cursor = await h(*args, **kwargs) |
| 49 | + except Exception: |
| 50 | + maybe_cursor = None |
| 51 | + self.assertIn( |
| 52 | + "comment", |
| 53 | + inspect.signature(h).parameters, |
| 54 | + msg="Could not find 'comment' in the " |
| 55 | + "signature of function %s" % (h.__name__), |
| 56 | + ) |
| 57 | + self.assertEqual( |
| 58 | + inspect.signature(h).parameters["comment"].annotation, "Optional[Any]" |
| 59 | + ) |
| 60 | + if isinstance(maybe_cursor, AsyncCommandCursor): |
| 61 | + await maybe_cursor.close() |
| 62 | + |
| 63 | + cmd = listener.started_events[0] |
| 64 | + self.assertEqual(cc, cmd.command.get("comment"), msg=cmd) |
| 65 | + |
| 66 | + if h.__name__ != "aggregate_raw_batches": |
| 67 | + self.assertIn( |
| 68 | + ":param comment:", |
| 69 | + h.__doc__, |
| 70 | + ) |
| 71 | + if h not in already_supported: |
| 72 | + self.assertIn( |
| 73 | + "Added ``comment`` parameter", |
| 74 | + h.__doc__, |
| 75 | + ) |
| 76 | + else: |
| 77 | + self.assertNotIn( |
| 78 | + "Added ``comment`` parameter", |
| 79 | + h.__doc__, |
| 80 | + ) |
| 81 | + |
| 82 | + listener.reset() |
| 83 | + |
| 84 | + @async_client_context.require_version_min(4, 7, -1) |
| 85 | + @async_client_context.require_replica_set |
| 86 | + async def test_database_helpers(self): |
| 87 | + listener = OvertCommandListener() |
| 88 | + db = (await self.async_rs_or_single_client(event_listeners=[listener])).db |
| 89 | + helpers = [ |
| 90 | + (db.watch, []), |
| 91 | + (db.command, ["hello"]), |
| 92 | + (db.list_collections, []), |
| 93 | + (db.list_collection_names, []), |
| 94 | + (db.drop_collection, ["hello"]), |
| 95 | + (db.validate_collection, ["test"]), |
| 96 | + (db.dereference, [DBRef("collection", 1)]), |
| 97 | + ] |
| 98 | + already_supported = [db.command, db.list_collections, db.list_collection_names] |
| 99 | + await self._test_ops(helpers, already_supported, listener) |
| 100 | + |
| 101 | + @async_client_context.require_version_min(4, 7, -1) |
| 102 | + @async_client_context.require_replica_set |
| 103 | + async def test_client_helpers(self): |
| 104 | + listener = OvertCommandListener() |
| 105 | + cli = await self.async_rs_or_single_client(event_listeners=[listener]) |
| 106 | + helpers = [ |
| 107 | + (cli.watch, []), |
| 108 | + (cli.list_databases, []), |
| 109 | + (cli.list_database_names, []), |
| 110 | + (cli.drop_database, ["test"]), |
| 111 | + ] |
| 112 | + already_supported = [ |
| 113 | + cli.list_databases, |
| 114 | + ] |
| 115 | + await self._test_ops(helpers, already_supported, listener) |
| 116 | + |
| 117 | + @async_client_context.require_version_min(4, 7, -1) |
| 118 | + async def test_collection_helpers(self): |
| 119 | + listener = OvertCommandListener() |
| 120 | + db = (await self.async_rs_or_single_client(event_listeners=[listener]))[self.db.name] |
| 121 | + coll = db.get_collection("test") |
| 122 | + |
| 123 | + helpers = [ |
| 124 | + (coll.list_indexes, []), |
| 125 | + (coll.drop, []), |
| 126 | + (coll.index_information, []), |
| 127 | + (coll.options, []), |
| 128 | + (coll.aggregate, [[{"$set": {"x": 1}}]]), |
| 129 | + (coll.aggregate_raw_batches, [[{"$set": {"x": 1}}]]), |
| 130 | + (coll.rename, ["temp_temp_temp"]), |
| 131 | + (coll.distinct, ["_id"]), |
| 132 | + (coll.find_one_and_delete, [{}]), |
| 133 | + (coll.find_one_and_replace, [{}, {}]), |
| 134 | + (coll.find_one_and_update, [{}, {"$set": {"a": 1}}]), |
| 135 | + (coll.estimated_document_count, []), |
| 136 | + (coll.count_documents, [{}]), |
| 137 | + (coll.create_indexes, [[IndexModel("a")]]), |
| 138 | + (coll.create_index, ["a"]), |
| 139 | + (coll.drop_index, [[("a", 1)]]), |
| 140 | + (coll.drop_indexes, []), |
| 141 | + ] |
| 142 | + already_supported = [ |
| 143 | + coll.estimated_document_count, |
| 144 | + coll.count_documents, |
| 145 | + coll.create_indexes, |
| 146 | + coll.drop_indexes, |
| 147 | + coll.options, |
| 148 | + coll.find_one_and_replace, |
| 149 | + coll.drop_index, |
| 150 | + coll.rename, |
| 151 | + coll.distinct, |
| 152 | + coll.find_one_and_delete, |
| 153 | + coll.find_one_and_update, |
| 154 | + ] |
| 155 | + await self._test_ops(helpers, already_supported, listener) |
| 156 | + |
| 157 | + |
| 158 | +if __name__ == "__main__": |
| 159 | + unittest.main() |
0 commit comments