|
| 1 | +# Copyright 2009-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 | + |
| 17 | +from test import UnitTest |
| 18 | +from pymongo import ASCENDING, DESCENDING |
| 19 | +from pymongo.operations import IndexModel |
| 20 | +from pymongo.collation import Collation |
| 21 | + |
| 22 | + |
| 23 | +class TestOperations(UnitTest): |
| 24 | + """Test Operations module including methods: |
| 25 | + _Op, _DeleteOp, _UpdateOp, |
| 26 | + DeleteOne, DeleteMany, |
| 27 | + InsertOne, |
| 28 | + ReplaceOne, |
| 29 | + UpdateOne, UpdateMany, |
| 30 | + IndexModel, |
| 31 | + SearchIndexModel""" |
| 32 | + |
| 33 | + def test_index_model_repr(self): |
| 34 | + # Based on examples in test_collection.py |
| 35 | + self.assertRepr(IndexModel("hello")) |
| 36 | + self.assertRepr(IndexModel([("hello", DESCENDING), ("world", ASCENDING)])) |
| 37 | + self.assertRepr( |
| 38 | + IndexModel([("hello", DESCENDING), ("world", ASCENDING)], name="hello_world") |
| 39 | + ) |
| 40 | + |
| 41 | + # Test all the kwargs |
| 42 | + self.assertRepr(IndexModel("name", name="name")) |
| 43 | + self.assertRepr(IndexModel("unique", unique=False)) |
| 44 | + self.assertRepr(IndexModel("background", background=True)) |
| 45 | + self.assertRepr(IndexModel("sparse", sparse=True)) |
| 46 | + self.assertRepr(IndexModel("bucketSize", bucketSize=1)) |
| 47 | + self.assertRepr(IndexModel("min", min=1)) |
| 48 | + self.assertRepr(IndexModel("max", max=1)) |
| 49 | + self.assertRepr(IndexModel("expireAfterSeconds", expireAfterSeconds=1)) |
| 50 | + self.assertRepr( |
| 51 | + IndexModel("partialFilterExpression", partialFilterExpression={"hello": "world"}) |
| 52 | + ) |
| 53 | + self.assertRepr(IndexModel("collation", collation=Collation(locale="en_US"))) |
| 54 | + self.assertRepr(IndexModel("wildcardProjection", wildcardProjection={"$**": 1})) |
| 55 | + self.assertRepr(IndexModel("hidden", hidden=False)) |
| 56 | + |
| 57 | + # Test string literal |
| 58 | + self.assertEqual(repr(IndexModel("hello")), "IndexModel({'hello': 1}, name='hello_1')") |
| 59 | + self.assertEqual( |
| 60 | + repr(IndexModel({"hello": 1, "world": -1})), |
| 61 | + "IndexModel({'hello': 1, 'world': -1}, name='hello_1_world_-1')", |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == "__main__": |
| 66 | + unittest.main() |
0 commit comments