diff --git a/pymongo/collection.py b/pymongo/collection.py index 2b771f4f60..ceba72aff3 100644 --- a/pymongo/collection.py +++ b/pymongo/collection.py @@ -2372,6 +2372,7 @@ def list_search_indexes( 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 25541f980c..c9a69aeccd 100644 --- a/test/test_index_management.py +++ b/test/test_index_management.py @@ -25,7 +25,7 @@ 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 @@ def test_inputs(self): 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 @@ def setUpClass(cls) -> None: 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 @@ def wait_for_ready(self, coll, name=_NAME, predicate=None): 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."""