Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ PyMongo 4.11 brings a number of changes including:
- :attr:`~pymongo.asynchronous.mongo_client.AsyncMongoClient.address` and
:attr:`~pymongo.mongo_client.MongoClient.address` now correctly block when called on unconnected clients
until either connection succeeds or a server selection timeout error is raised.
- Added :func:`repr` support to :class:`pymongo.operations.IndexModel`.
- Added :func:`repr` support to :class:`pymongo.operations.SearchIndexModel`.

Issues Resolved
...............
Expand Down
13 changes: 13 additions & 0 deletions pymongo/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,13 @@ def document(self) -> dict[str, Any]:
"""
return self.__document

def __repr__(self) -> str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ruff is still failing because it wants you to use f-strings. Do you have pre-commit set up?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, forgot my pre-commit install! I think I just need to add a more meaningful test to the SearchIndexModel test class to finish this.

return "{}({}{})".format(
self.__class__.__name__,
self.document["key"],
"".join([f", {key}={value!r}" for key, value in self.document.items() if key != "key"]),
)


class SearchIndexModel:
"""Represents a search index to create."""
Expand Down Expand Up @@ -812,3 +819,9 @@ def __init__(
def document(self) -> Mapping[str, Any]:
"""The document for this index."""
return self.__document

def __repr__(self) -> str:
return "{}({})".format(
self.__class__.__name__,
", ".join([f"{key}={value!r}" for key, value in self.document.items()]),
)
80 changes: 80 additions & 0 deletions test/test_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright 2009-2024 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 operations module."""
from __future__ import annotations

from test import UnitTest, unittest

from pymongo import ASCENDING, DESCENDING
from pymongo.collation import Collation
from pymongo.errors import OperationFailure
from pymongo.operations import IndexModel, SearchIndexModel


class TestOperationsBase(UnitTest):
"""Base class for testing operations module."""

def assertRepr(self, obj):
new_obj = eval(repr(obj))
self.assertEqual(type(new_obj), type(obj))
self.assertEqual(repr(new_obj), repr(obj))


class TestIndexModel(TestOperationsBase):
"""Test IndexModel features."""

def test_repr(self):
# Based on examples in test_collection.py
self.assertRepr(IndexModel("hello"))
self.assertRepr(IndexModel([("hello", DESCENDING), ("world", ASCENDING)]))
self.assertRepr(
IndexModel([("hello", DESCENDING), ("world", ASCENDING)], name="hello_world")
)
# Test all the kwargs
self.assertRepr(IndexModel("name", name="name"))
self.assertRepr(IndexModel("unique", unique=False))
self.assertRepr(IndexModel("background", background=True))
self.assertRepr(IndexModel("sparse", sparse=True))
self.assertRepr(IndexModel("bucketSize", bucketSize=1))
self.assertRepr(IndexModel("min", min=1))
self.assertRepr(IndexModel("max", max=1))
self.assertRepr(IndexModel("expireAfterSeconds", expireAfterSeconds=1))
self.assertRepr(
IndexModel("partialFilterExpression", partialFilterExpression={"hello": "world"})
)
self.assertRepr(IndexModel("collation", collation=Collation(locale="en_US")))
self.assertRepr(IndexModel("wildcardProjection", wildcardProjection={"$**": 1}))
self.assertRepr(IndexModel("hidden", hidden=False))
# Test string literal
self.assertEqual(repr(IndexModel("hello")), "IndexModel({'hello': 1}, name='hello_1')")
self.assertEqual(
repr(IndexModel({"hello": 1, "world": -1})),
"IndexModel({'hello': 1, 'world': -1}, name='hello_1_world_-1')",
)


class TestSearchIndexModel(TestOperationsBase):
"""Test SearchIndexModel features."""

def test_repr(self):
self.assertRepr(SearchIndexModel({"hello": "hello"}, key=1))
self.assertEqual(
repr(SearchIndexModel({"hello": "hello"}, key=1)),
"SearchIndexModel(definition={'hello': 'hello'}, key=1)",
)


if __name__ == "__main__":
unittest.main()
Loading