1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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()
|