From 44e47304ffc4ce0c9a1f19bf87ee8a0035056e1d Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 1 Apr 2024 16:41:55 -0500 Subject: [PATCH] PYTHON-4300 Forward comment argument in list_search_indexes (#1569) --- pymongo/collection.py | 1 + test/test_index_management.py | 41 +++++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/pymongo/collection.py b/pymongo/collection.py index 2b771f4f6..ceba72aff 100644 --- a/pymongo/collection.py +++ b/pymongo/collection.py @@ -2372,6 +2372,7 @@ class Collection(common.BaseObject, Generic[_DocumentType]): pipeline, kwargs, explicit_session=session is not None, + comment=comment, user_fields={"cursor": {"firstBatch": 1}}, ) diff --git a/test/test_index_management.py b/test/test_index_management.py index 25541f980..c9a69aecc 100644 --- a/test/test_index_management.py +++ b/test/test_index_management.py @@ -25,7 +25,7 @@ sys.path[0:0] = [""] from test import IntegrationTest, unittest from test.unified_format import generate_test_classes -from test.utils import AllowListEventListener +from test.utils import AllowListEventListener, EventListener from pymongo import MongoClient from pymongo.errors import OperationFailure @@ -63,7 +63,9 @@ class TestCreateSearchIndex(IntegrationTest): self.assertIn("arbitraryOption", listener.events[0].command["indexes"][0]) -class TestSearchIndexProse(unittest.TestCase): +class SearchIndexIntegrationBase(unittest.TestCase): + db_name = "test_search_index_base" + @classmethod def setUpClass(cls) -> None: super().setUpClass() @@ -72,9 +74,12 @@ class TestSearchIndexProse(unittest.TestCase): url = os.environ.get("MONGODB_URI") username = os.environ["DB_USER"] password = os.environ["DB_PASSWORD"] - cls.client = MongoClient(url, username=username, password=password) + cls.listener = listener = EventListener() + cls.client = MongoClient( + url, username=username, password=password, event_listeners=[listener] + ) cls.client.drop_database(_NAME) - cls.db = cls.client.test_search_index_prose + cls.db = cls.client[cls.db_name] @classmethod def tearDownClass(cls): @@ -94,6 +99,34 @@ class TestSearchIndexProse(unittest.TestCase): break time.sleep(5) + +class TestSearchIndexIntegration(SearchIndexIntegrationBase): + db_name = "test_search_index" + + def test_comment_field(self): + # Create a collection with the "create" command using a randomly generated name (referred to as ``coll0``). + coll0 = self.db[f"col{uuid.uuid4()}"] + coll0.insert_one({}) + + # Create a new search index on ``coll0`` that implicitly passes its type. + search_definition = {"mappings": {"dynamic": False}} + self.listener.reset() + implicit_search_resp = coll0.create_search_index( + model={"name": _NAME + "-implicit", "definition": search_definition}, comment="foo" + ) + event = self.listener.events[0] + self.assertEqual(event.command["comment"], "foo") + + # Get the index definition. + self.listener.reset() + coll0.list_search_indexes(name=implicit_search_resp, comment="foo").next() + event = self.listener.events[0] + self.assertEqual(event.command["comment"], "foo") + + +class TestSearchIndexProse(SearchIndexIntegrationBase): + db_name = "test_search_index_prose" + def test_case_1(self): """Driver can successfully create and list search indexes."""