PYTHON-4834 Add __repr__ to IndexModel, SearchIndexModel (#1909)

This commit is contained in:
Jeffrey A. Clark 2024-10-15 18:45:49 -04:00 committed by GitHub
parent fa263dc87d
commit 8034baec90
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 95 additions and 0 deletions

View File

@ -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
...............

View File

@ -773,6 +773,13 @@ class IndexModel:
"""
return self.__document
def __repr__(self) -> str:
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."""
@ -812,3 +819,9 @@ class SearchIndexModel:
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
test/test_operations.py Normal file
View File

@ -0,0 +1,80 @@
# Copyright 2024-present 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()