From 7e8f2c7da9a69f4bf17c5ba28b72d62163c34452 Mon Sep 17 00:00:00 2001 From: Iris Date: Tue, 1 Oct 2024 09:12:17 -0700 Subject: [PATCH 01/11] Migrate test_comment.py to async --- test/asynchronous/test_comment.py | 187 ++++++++++++++++++++++++++++++ test/test_comment.py | 4 +- tools/synchro.py | 1 + 3 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 test/asynchronous/test_comment.py diff --git a/test/asynchronous/test_comment.py b/test/asynchronous/test_comment.py new file mode 100644 index 0000000000..d7ca081c16 --- /dev/null +++ b/test/asynchronous/test_comment.py @@ -0,0 +1,187 @@ +# 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 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: + await coll.create_index("a") + maybe_cursor = await 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() diff --git a/test/test_comment.py b/test/test_comment.py index c0f037ea44..f182386157 100644 --- a/test/test_comment.py +++ b/test/test_comment.py @@ -28,6 +28,8 @@ from pymongo.operations import IndexModel from pymongo.synchronous.command_cursor import CommandCursor +_IS_SYNC = True + class Empty: def __getattr__(self, item): @@ -141,7 +143,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 = [ diff --git a/tools/synchro.py b/tools/synchro.py index 3333b0de2e..b7f750af0e 100644 --- a/tools/synchro.py +++ b/tools/synchro.py @@ -186,6 +186,7 @@ def async_only_test(f: str) -> bool: "test_client_bulk_write.py", "test_client_context.py", "test_collection.py", + "test_comment.py", "test_cursor.py", "test_database.py", "test_encryption.py", From 161547ad11581872dce9af3acb26626ae94df2b4 Mon Sep 17 00:00:00 2001 From: Iris Date: Tue, 1 Oct 2024 09:37:23 -0700 Subject: [PATCH 02/11] check if its coroutine and then await it --- test/asynchronous/test_comment.py | 7 +++++-- test/test_comment.py | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/test/asynchronous/test_comment.py b/test/asynchronous/test_comment.py index d7ca081c16..89d016b073 100644 --- a/test/asynchronous/test_comment.py +++ b/test/asynchronous/test_comment.py @@ -20,7 +20,7 @@ import sys sys.path[0:0] = [""] - +from asyncio import iscoroutinefunction from test.asynchronous import AsyncIntegrationTest, async_client_context, unittest from test.utils import EventListener @@ -69,7 +69,10 @@ async def _test_ops( maybe_cursor = await db.validate_collection(*args, **kwargs) else: await coll.create_index("a") - maybe_cursor = await h(*args, **kwargs) + if iscoroutinefunction(h): + maybe_cursor = await h(*args, **kwargs) + else: + maybe_cursor = h(*args, **kwargs) self.assertIn( "comment", inspect.signature(h).parameters, diff --git a/test/test_comment.py b/test/test_comment.py index f182386157..890697c5d7 100644 --- a/test/test_comment.py +++ b/test/test_comment.py @@ -20,7 +20,7 @@ import sys sys.path[0:0] = [""] - +from asyncio import iscoroutinefunction from test import IntegrationTest, client_context, unittest from test.utils import EventListener @@ -69,7 +69,10 @@ def _test_ops( maybe_cursor = db.validate_collection(*args, **kwargs) else: coll.create_index("a") - maybe_cursor = h(*args, **kwargs) + if iscoroutinefunction(h): + maybe_cursor = h(*args, **kwargs) + else: + maybe_cursor = h(*args, **kwargs) self.assertIn( "comment", inspect.signature(h).parameters, From 0ecdd31cd86f7bb83c7cd702f049dbbbf21b4931 Mon Sep 17 00:00:00 2001 From: Iris Date: Tue, 1 Oct 2024 10:06:33 -0700 Subject: [PATCH 03/11] remove extra await --- test/asynchronous/test_comment.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/asynchronous/test_comment.py b/test/asynchronous/test_comment.py index 89d016b073..5465c04079 100644 --- a/test/asynchronous/test_comment.py +++ b/test/asynchronous/test_comment.py @@ -68,7 +68,7 @@ async def _test_ops( await coll.insert_one({}) maybe_cursor = await db.validate_collection(*args, **kwargs) else: - await coll.create_index("a") + coll.create_index("a") if iscoroutinefunction(h): maybe_cursor = await h(*args, **kwargs) else: From 2b4797e00257fa960ca905c59bbc4f71843078dd Mon Sep 17 00:00:00 2001 From: Iris Date: Tue, 1 Oct 2024 10:19:46 -0700 Subject: [PATCH 04/11] jk wasn't extra, need to check, sometimes its need and sometimes its not --- test/asynchronous/test_comment.py | 5 +++-- test/test_comment.py | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/test/asynchronous/test_comment.py b/test/asynchronous/test_comment.py index 5465c04079..1e006ef3d1 100644 --- a/test/asynchronous/test_comment.py +++ b/test/asynchronous/test_comment.py @@ -68,10 +68,11 @@ async def _test_ops( await coll.insert_one({}) maybe_cursor = await db.validate_collection(*args, **kwargs) else: - coll.create_index("a") - if iscoroutinefunction(h): + if iscoroutinefunction(coll.create_index): + await coll.create_index("a") maybe_cursor = await h(*args, **kwargs) else: + coll.create_index("a") maybe_cursor = h(*args, **kwargs) self.assertIn( "comment", diff --git a/test/test_comment.py b/test/test_comment.py index 890697c5d7..bcadcc2d92 100644 --- a/test/test_comment.py +++ b/test/test_comment.py @@ -68,7 +68,10 @@ def _test_ops( coll.insert_one({}) maybe_cursor = db.validate_collection(*args, **kwargs) else: - coll.create_index("a") + if iscoroutinefunction(coll.create_index): + coll.create_index("a") + else: + coll.create_index("a") if iscoroutinefunction(h): maybe_cursor = h(*args, **kwargs) else: From 84893dc328e381b9ece08441a8a2198d56055ca6 Mon Sep 17 00:00:00 2001 From: Iris Date: Tue, 1 Oct 2024 10:22:08 -0700 Subject: [PATCH 05/11] forgot to run precommit (again) --- test/test_comment.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/test_comment.py b/test/test_comment.py index bcadcc2d92..4410b890c5 100644 --- a/test/test_comment.py +++ b/test/test_comment.py @@ -70,11 +70,9 @@ def _test_ops( else: if iscoroutinefunction(coll.create_index): coll.create_index("a") - else: - coll.create_index("a") - if iscoroutinefunction(h): maybe_cursor = h(*args, **kwargs) else: + coll.create_index("a") maybe_cursor = h(*args, **kwargs) self.assertIn( "comment", From 858b8050df182a0ece666517df0adab1ebc029fd Mon Sep 17 00:00:00 2001 From: Iris Date: Tue, 1 Oct 2024 10:43:12 -0700 Subject: [PATCH 06/11] added missing parens --- test/asynchronous/test_comment.py | 2 +- test/test_comment.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/asynchronous/test_comment.py b/test/asynchronous/test_comment.py index 1e006ef3d1..d95aa0edd0 100644 --- a/test/asynchronous/test_comment.py +++ b/test/asynchronous/test_comment.py @@ -115,7 +115,7 @@ async def _test_ops( @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 + db = (await self.async_rs_or_single_client(event_listeners=[listener])).db helpers = [ (db.watch, []), (db.command, ["hello"]), diff --git a/test/test_comment.py b/test/test_comment.py index 4410b890c5..8149071dcc 100644 --- a/test/test_comment.py +++ b/test/test_comment.py @@ -115,7 +115,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"]), From 4120a1523f034e8684781edf072d242c0c9b9db0 Mon Sep 17 00:00:00 2001 From: Iris Date: Tue, 1 Oct 2024 11:08:52 -0700 Subject: [PATCH 07/11] fix coroutine function check --- test/asynchronous/test_comment.py | 4 +++- test/test_comment.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/test/asynchronous/test_comment.py b/test/asynchronous/test_comment.py index d95aa0edd0..674e04c753 100644 --- a/test/asynchronous/test_comment.py +++ b/test/asynchronous/test_comment.py @@ -70,9 +70,11 @@ async def _test_ops( else: if iscoroutinefunction(coll.create_index): await coll.create_index("a") - maybe_cursor = await h(*args, **kwargs) else: coll.create_index("a") + if iscoroutinefunction(h): + maybe_cursor = await h(*args, **kwargs) + else: maybe_cursor = h(*args, **kwargs) self.assertIn( "comment", diff --git a/test/test_comment.py b/test/test_comment.py index 8149071dcc..4698bd7745 100644 --- a/test/test_comment.py +++ b/test/test_comment.py @@ -70,9 +70,11 @@ def _test_ops( else: if iscoroutinefunction(coll.create_index): coll.create_index("a") - maybe_cursor = h(*args, **kwargs) else: coll.create_index("a") + if iscoroutinefunction(h): + maybe_cursor = h(*args, **kwargs) + else: maybe_cursor = h(*args, **kwargs) self.assertIn( "comment", From 9dac71ae72c7813ca375555ba130ac972a6405ab Mon Sep 17 00:00:00 2001 From: Iris Date: Mon, 7 Oct 2024 10:03:07 -0700 Subject: [PATCH 08/11] accept suggested changes --- test/asynchronous/test_comment.py | 10 +++++----- test/test_comment.py | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/asynchronous/test_comment.py b/test/asynchronous/test_comment.py index 674e04c753..6b2dfbb793 100644 --- a/test/asynchronous/test_comment.py +++ b/test/asynchronous/test_comment.py @@ -58,7 +58,7 @@ async def _test_ops( listener.reset() kwargs = {"comment": cc} if h == coll.rename: - _ = await db.get_collection("temp_temp_temp").drop() + 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) @@ -68,11 +68,11 @@ async def _test_ops( await coll.insert_one({}) maybe_cursor = await db.validate_collection(*args, **kwargs) else: - if iscoroutinefunction(coll.create_index): - await coll.create_index("a") - else: + if not _IS_SYNC and isinstance(coll, Empty): coll.create_index("a") - if iscoroutinefunction(h): + 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) diff --git a/test/test_comment.py b/test/test_comment.py index 4698bd7745..434c67f018 100644 --- a/test/test_comment.py +++ b/test/test_comment.py @@ -58,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) @@ -68,11 +68,11 @@ def _test_ops( coll.insert_one({}) maybe_cursor = db.validate_collection(*args, **kwargs) else: - if iscoroutinefunction(coll.create_index): + if not _IS_SYNC and isinstance(coll, Empty): coll.create_index("a") else: coll.create_index("a") - if iscoroutinefunction(h): + if not _IS_SYNC and iscoroutinefunction(h): maybe_cursor = h(*args, **kwargs) else: maybe_cursor = h(*args, **kwargs) From 9e0f40969bb0fedef971b9c2b05dbac89764f3df Mon Sep 17 00:00:00 2001 From: Iris Date: Thu, 10 Oct 2024 16:29:37 -0700 Subject: [PATCH 09/11] remove Empty class and cleanup _test_ops funciton --- test/asynchronous/test_comment.py | 44 +++++-------------------------- test/test_comment.py | 42 +++++------------------------ 2 files changed, 14 insertions(+), 72 deletions(-) diff --git a/test/asynchronous/test_comment.py b/test/asynchronous/test_comment.py index 6b2dfbb793..a19a8bdc99 100644 --- a/test/asynchronous/test_comment.py +++ b/test/asynchronous/test_comment.py @@ -31,25 +31,12 @@ _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__ @@ -57,25 +44,10 @@ async def _test_ops( 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) + try: + maybe_cursor = await h(*args, **kwargs) + except Exception: + maybe_cursor = None self.assertIn( "comment", inspect.signature(h).parameters, @@ -95,7 +67,7 @@ async def _test_ops( self.assertEqual(cc, i.command["comment"]) tested = True self.assertTrue(tested) - if h not in [coll.aggregate_raw_batches]: + if h.__name__ != "aggregate_raw_batches": self.assertIn( ":param comment:", h.__doc__, @@ -128,9 +100,7 @@ async def test_database_helpers(self): (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") - ) + await self._test_ops(helpers, already_supported, listener) @async_client_context.require_version_min(4, 7, -1) @async_client_context.require_replica_set @@ -186,7 +156,7 @@ async def test_collection_helpers(self): coll.find_one_and_delete, coll.find_one_and_update, ] - await self._test_ops(helpers, already_supported, listener, coll=coll, db=db) + await self._test_ops(helpers, already_supported, listener) if __name__ == "__main__": diff --git a/test/test_comment.py b/test/test_comment.py index 434c67f018..315afeeead 100644 --- a/test/test_comment.py +++ b/test/test_comment.py @@ -31,25 +31,12 @@ _IS_SYNC = True -class Empty: - def __getattr__(self, item): - try: - self.__dict__[item] - except KeyError: - return self.empty - - def empty(self, *args, **kwargs): - return Empty() - - class TestComment(IntegrationTest): 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__ @@ -57,25 +44,10 @@ def _test_ops( for cc in [c, {"key": c}, ["any", 1]]: listener.reset() kwargs = {"comment": cc} - if h == coll.rename: - 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) - destruct_coll.drop() - elif h == db.validate_collection: - coll = db.get_collection("test") - coll.insert_one({}) - maybe_cursor = db.validate_collection(*args, **kwargs) - else: - if not _IS_SYNC and isinstance(coll, Empty): - coll.create_index("a") - else: - coll.create_index("a") - if not _IS_SYNC and iscoroutinefunction(h): - maybe_cursor = h(*args, **kwargs) - else: - maybe_cursor = h(*args, **kwargs) + try: + maybe_cursor = h(*args, **kwargs) + except Exception: + maybe_cursor = None self.assertIn( "comment", inspect.signature(h).parameters, @@ -95,7 +67,7 @@ def _test_ops( self.assertEqual(cc, i.command["comment"]) tested = True self.assertTrue(tested) - if h not in [coll.aggregate_raw_batches]: + if h.__name__ != "aggregate_raw_batches": self.assertIn( ":param comment:", h.__doc__, @@ -128,7 +100,7 @@ def test_database_helpers(self): (db.dereference, [DBRef("collection", 1)]), ] already_supported = [db.command, db.list_collections, db.list_collection_names] - self._test_ops(helpers, already_supported, listener, db=db, coll=db.get_collection("test")) + self._test_ops(helpers, already_supported, listener) @client_context.require_version_min(4, 7, -1) @client_context.require_replica_set @@ -184,7 +156,7 @@ def test_collection_helpers(self): coll.find_one_and_delete, coll.find_one_and_update, ] - self._test_ops(helpers, already_supported, listener, coll=coll, db=db) + self._test_ops(helpers, already_supported, listener) if __name__ == "__main__": From 89dcfac3f75078ceda8664e2c7c0b014dde47def Mon Sep 17 00:00:00 2001 From: Iris Date: Thu, 10 Oct 2024 16:38:38 -0700 Subject: [PATCH 10/11] cleaned up _test_ops more --- test/asynchronous/test_comment.py | 12 ++++-------- test/test_comment.py | 12 ++++-------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/test/asynchronous/test_comment.py b/test/asynchronous/test_comment.py index a19a8bdc99..8c4cbcfbfc 100644 --- a/test/asynchronous/test_comment.py +++ b/test/asynchronous/test_comment.py @@ -59,14 +59,10 @@ async def _test_ops( ) 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) + + cmd = listener.started_events[0] + self.assertEqual(cc, cmd.command.get("comment"), msg=cmd) + if h.__name__ != "aggregate_raw_batches": self.assertIn( ":param comment:", diff --git a/test/test_comment.py b/test/test_comment.py index 315afeeead..ca974ae431 100644 --- a/test/test_comment.py +++ b/test/test_comment.py @@ -59,14 +59,10 @@ def _test_ops( ) if isinstance(maybe_cursor, CommandCursor): 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) + + cmd = listener.started_events[0] + self.assertEqual(cc, cmd.command.get("comment"), msg=cmd) + if h.__name__ != "aggregate_raw_batches": self.assertIn( ":param comment:", From 979a5bfef62ae93fcc20e81dfa65aac762b75974 Mon Sep 17 00:00:00 2001 From: Iris Date: Thu, 10 Oct 2024 17:04:20 -0700 Subject: [PATCH 11/11] change listener for tests --- test/asynchronous/test_comment.py | 8 ++++---- test/test_comment.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/asynchronous/test_comment.py b/test/asynchronous/test_comment.py index 8c4cbcfbfc..be3626a8b8 100644 --- a/test/asynchronous/test_comment.py +++ b/test/asynchronous/test_comment.py @@ -22,7 +22,7 @@ sys.path[0:0] = [""] from asyncio import iscoroutinefunction from test.asynchronous import AsyncIntegrationTest, async_client_context, unittest -from test.utils import EventListener +from test.utils import OvertCommandListener from bson.dbref import DBRef from pymongo.asynchronous.command_cursor import AsyncCommandCursor @@ -84,7 +84,7 @@ async def _test_ops( @async_client_context.require_version_min(4, 7, -1) @async_client_context.require_replica_set async def test_database_helpers(self): - listener = EventListener() + listener = OvertCommandListener() db = (await self.async_rs_or_single_client(event_listeners=[listener])).db helpers = [ (db.watch, []), @@ -101,7 +101,7 @@ async def test_database_helpers(self): @async_client_context.require_version_min(4, 7, -1) @async_client_context.require_replica_set async def test_client_helpers(self): - listener = EventListener() + listener = OvertCommandListener() cli = await self.async_rs_or_single_client(event_listeners=[listener]) helpers = [ (cli.watch, []), @@ -116,7 +116,7 @@ async def test_client_helpers(self): @async_client_context.require_version_min(4, 7, -1) async def test_collection_helpers(self): - listener = EventListener() + listener = OvertCommandListener() db = (await self.async_rs_or_single_client(event_listeners=[listener]))[self.db.name] coll = db.get_collection("test") diff --git a/test/test_comment.py b/test/test_comment.py index ca974ae431..9f9bf98640 100644 --- a/test/test_comment.py +++ b/test/test_comment.py @@ -22,7 +22,7 @@ sys.path[0:0] = [""] from asyncio import iscoroutinefunction from test import IntegrationTest, client_context, unittest -from test.utils import EventListener +from test.utils import OvertCommandListener from bson.dbref import DBRef from pymongo.operations import IndexModel @@ -84,7 +84,7 @@ def _test_ops( @client_context.require_version_min(4, 7, -1) @client_context.require_replica_set def test_database_helpers(self): - listener = EventListener() + listener = OvertCommandListener() db = (self.rs_or_single_client(event_listeners=[listener])).db helpers = [ (db.watch, []), @@ -101,7 +101,7 @@ def test_database_helpers(self): @client_context.require_version_min(4, 7, -1) @client_context.require_replica_set def test_client_helpers(self): - listener = EventListener() + listener = OvertCommandListener() cli = self.rs_or_single_client(event_listeners=[listener]) helpers = [ (cli.watch, []), @@ -116,7 +116,7 @@ def test_client_helpers(self): @client_context.require_version_min(4, 7, -1) def test_collection_helpers(self): - listener = EventListener() + listener = OvertCommandListener() db = (self.rs_or_single_client(event_listeners=[listener]))[self.db.name] coll = db.get_collection("test")