From f593fb59e0d7894fd60b4b282a48f2c26b25eb61 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sat, 30 Mar 2024 19:07:30 -0500 Subject: [PATCH 1/6] PYTHON-4300 Forward comment argument in list_search_indexes --- pymongo/collection.py | 1 + test/test_index_management.py | 32 +++++++++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) 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..5d8794ff57 100644 --- a/test/test_index_management.py +++ b/test/test_index_management.py @@ -63,7 +63,9 @@ def test_inputs(self): self.assertIn("arbitraryOption", listener.events[0].command["indexes"][0]) -class TestSearchIndexProse(unittest.TestCase): +class TestSearchIndexIntegration(unittest.TestCase): + db = "test_search_index" + @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 = AllowListEventListener("commandStarted") + 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] @classmethod def tearDownClass(cls): @@ -94,6 +99,27 @@ def wait_for_ready(self, coll, name=_NAME, predicate=None): break time.sleep(5) + 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}} + implicit_search_resp = coll0.create_search_index( + model={"name": _NAME + "-implicit", "definition": search_definition} + ) + + # 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(unittest.TestCase): + db = "test_search_index_prose" + def test_case_1(self): """Driver can successfully create and list search indexes.""" From 8f3acb576dfed3e178640ca4ea4373921081684e Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sat, 30 Mar 2024 19:29:51 -0500 Subject: [PATCH 2/6] fix var name --- test/test_index_management.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_index_management.py b/test/test_index_management.py index 5d8794ff57..d9d1978dc8 100644 --- a/test/test_index_management.py +++ b/test/test_index_management.py @@ -64,7 +64,7 @@ def test_inputs(self): class TestSearchIndexIntegration(unittest.TestCase): - db = "test_search_index" + db_name = "test_search_index" @classmethod def setUpClass(cls) -> None: @@ -79,7 +79,7 @@ def setUpClass(cls) -> None: url, username=username, password=password, event_listeners=listener ) cls.client.drop_database(_NAME) - cls.db = cls.client[cls.db] + cls.db = cls.client[cls.db_name] @classmethod def tearDownClass(cls): @@ -118,7 +118,7 @@ def test_comment_field(self): class TestSearchIndexProse(unittest.TestCase): - db = "test_search_index_prose" + db_name = "test_search_index_prose" def test_case_1(self): """Driver can successfully create and list search indexes.""" From 26f77d8671e5612b7523425c0d3dfc27a2cf8f4e Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sun, 31 Mar 2024 01:19:05 -0500 Subject: [PATCH 3/6] fix listeners --- test/test_index_management.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_index_management.py b/test/test_index_management.py index d9d1978dc8..b91251a63e 100644 --- a/test/test_index_management.py +++ b/test/test_index_management.py @@ -76,7 +76,7 @@ def setUpClass(cls) -> None: password = os.environ["DB_PASSWORD"] cls.listener = listener = AllowListEventListener("commandStarted") cls.client = MongoClient( - url, username=username, password=password, event_listeners=listener + url, username=username, password=password, event_listeners=[listener] ) cls.client.drop_database(_NAME) cls.db = cls.client[cls.db_name] From d4c4f0db53e6d2f7f2bade703f0dd8966250df14 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sun, 31 Mar 2024 01:41:30 -0500 Subject: [PATCH 4/6] fixups --- test/test_index_management.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_index_management.py b/test/test_index_management.py index b91251a63e..8b1cd485c0 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 @@ -74,7 +74,7 @@ def setUpClass(cls) -> None: url = os.environ.get("MONGODB_URI") username = os.environ["DB_USER"] password = os.environ["DB_PASSWORD"] - cls.listener = listener = AllowListEventListener("commandStarted") + cls.listener = listener = EventListener() cls.client = MongoClient( url, username=username, password=password, event_listeners=[listener] ) @@ -117,7 +117,7 @@ def test_comment_field(self): self.assertEqual(event["command"]["comment"], "foo") -class TestSearchIndexProse(unittest.TestCase): +class TestSearchIndexProse(TestSearchIndexIntegration): db_name = "test_search_index_prose" def test_case_1(self): From 8ebae0030f9e0e0d7e901af1338a116f87b3eb44 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Sun, 31 Mar 2024 02:01:20 -0500 Subject: [PATCH 5/6] fix handling of event --- test/test_index_management.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_index_management.py b/test/test_index_management.py index 8b1cd485c0..855c1f1e37 100644 --- a/test/test_index_management.py +++ b/test/test_index_management.py @@ -114,7 +114,7 @@ def test_comment_field(self): 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") + self.assertEqual(event.command["comment"], "foo") class TestSearchIndexProse(TestSearchIndexIntegration): From 1d2110d170be472b0394fcf2e30837bce3f2438c Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 1 Apr 2024 15:09:21 -0500 Subject: [PATCH 6/6] address review --- test/test_index_management.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/test/test_index_management.py b/test/test_index_management.py index 855c1f1e37..c9a69aeccd 100644 --- a/test/test_index_management.py +++ b/test/test_index_management.py @@ -63,8 +63,8 @@ def test_inputs(self): self.assertIn("arbitraryOption", listener.events[0].command["indexes"][0]) -class TestSearchIndexIntegration(unittest.TestCase): - db_name = "test_search_index" +class SearchIndexIntegrationBase(unittest.TestCase): + db_name = "test_search_index_base" @classmethod def setUpClass(cls) -> None: @@ -99,6 +99,10 @@ 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()}"] @@ -106,9 +110,12 @@ def test_comment_field(self): # 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} + 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() @@ -117,7 +124,7 @@ def test_comment_field(self): self.assertEqual(event.command["comment"], "foo") -class TestSearchIndexProse(TestSearchIndexIntegration): +class TestSearchIndexProse(SearchIndexIntegrationBase): db_name = "test_search_index_prose" def test_case_1(self):