|
| 1 | +# Copyright 2024-present MongoDB, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Test the operations module.""" |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +from test import UnitTest, unittest |
| 19 | + |
| 20 | +from pymongo import ASCENDING, DESCENDING |
| 21 | +from pymongo.collation import Collation |
| 22 | +from pymongo.errors import OperationFailure |
| 23 | +from pymongo.operations import IndexModel, SearchIndexModel |
| 24 | + |
| 25 | + |
| 26 | +class TestOperationsBase(UnitTest): |
| 27 | + """Base class for testing operations module.""" |
| 28 | + |
| 29 | + def assertRepr(self, obj): |
| 30 | + new_obj = eval(repr(obj)) |
| 31 | + self.assertEqual(type(new_obj), type(obj)) |
| 32 | + self.assertEqual(repr(new_obj), repr(obj)) |
| 33 | + |
| 34 | + |
| 35 | +class TestIndexModel(TestOperationsBase): |
| 36 | + """Test IndexModel features.""" |
| 37 | + |
| 38 | + def test_repr(self): |
| 39 | + # Based on examples in test_collection.py |
| 40 | + self.assertRepr(IndexModel("hello")) |
| 41 | + self.assertRepr(IndexModel([("hello", DESCENDING), ("world", ASCENDING)])) |
| 42 | + self.assertRepr( |
| 43 | + IndexModel([("hello", DESCENDING), ("world", ASCENDING)], name="hello_world") |
| 44 | + ) |
| 45 | + # Test all the kwargs |
| 46 | + self.assertRepr(IndexModel("name", name="name")) |
| 47 | + self.assertRepr(IndexModel("unique", unique=False)) |
| 48 | + self.assertRepr(IndexModel("background", background=True)) |
| 49 | + self.assertRepr(IndexModel("sparse", sparse=True)) |
| 50 | + self.assertRepr(IndexModel("bucketSize", bucketSize=1)) |
| 51 | + self.assertRepr(IndexModel("min", min=1)) |
| 52 | + self.assertRepr(IndexModel("max", max=1)) |
| 53 | + self.assertRepr(IndexModel("expireAfterSeconds", expireAfterSeconds=1)) |
| 54 | + self.assertRepr( |
| 55 | + IndexModel("partialFilterExpression", partialFilterExpression={"hello": "world"}) |
| 56 | + ) |
| 57 | + self.assertRepr(IndexModel("collation", collation=Collation(locale="en_US"))) |
| 58 | + self.assertRepr(IndexModel("wildcardProjection", wildcardProjection={"$**": 1})) |
| 59 | + self.assertRepr(IndexModel("hidden", hidden=False)) |
| 60 | + # Test string literal |
| 61 | + self.assertEqual(repr(IndexModel("hello")), "IndexModel({'hello': 1}, name='hello_1')") |
| 62 | + self.assertEqual( |
| 63 | + repr(IndexModel({"hello": 1, "world": -1})), |
| 64 | + "IndexModel({'hello': 1, 'world': -1}, name='hello_1_world_-1')", |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +class TestSearchIndexModel(TestOperationsBase): |
| 69 | + """Test SearchIndexModel features.""" |
| 70 | + |
| 71 | + def test_repr(self): |
| 72 | + self.assertRepr(SearchIndexModel({"hello": "hello"}, key=1)) |
| 73 | + self.assertEqual( |
| 74 | + repr(SearchIndexModel({"hello": "hello"}, key=1)), |
| 75 | + "SearchIndexModel(definition={'hello': 'hello'}, key=1)", |
| 76 | + ) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + unittest.main() |
0 commit comments