Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pymongo/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2372,6 +2372,7 @@ def list_search_indexes(
pipeline,
kwargs,
explicit_session=session is not None,
comment=comment,
user_fields={"cursor": {"firstBatch": 1}},
)

Expand Down
41 changes: 37 additions & 4 deletions test/test_index_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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):
Expand All @@ -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."""

Expand Down