|
| 1 | +# Copyright 2020-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 | +import sys |
| 16 | +import traceback |
| 17 | + |
| 18 | +sys.path[0:0] = [""] |
| 19 | + |
| 20 | +from pymongo.errors import (NotMasterError, |
| 21 | + OperationFailure) |
| 22 | +from test import (PyMongoTestCase, |
| 23 | + unittest) |
| 24 | + |
| 25 | + |
| 26 | +class TestErrors(PyMongoTestCase): |
| 27 | + def test_not_master_error(self): |
| 28 | + exc = NotMasterError("not master test", {"errmsg": "error"}) |
| 29 | + self.assertIn("full error", str(exc)) |
| 30 | + try: |
| 31 | + raise exc |
| 32 | + except NotMasterError: |
| 33 | + self.assertIn("full error", traceback.format_exc()) |
| 34 | + |
| 35 | + def test_operation_failure(self): |
| 36 | + exc = OperationFailure("operation failure test", 10, |
| 37 | + {"errmsg": "error"}) |
| 38 | + self.assertIn("full error", str(exc)) |
| 39 | + try: |
| 40 | + raise exc |
| 41 | + except OperationFailure: |
| 42 | + self.assertIn("full error", traceback.format_exc()) |
| 43 | + |
| 44 | + def _test_unicode_strs(self, exc): |
| 45 | + if sys.version_info[0] == 2: |
| 46 | + self.assertEqual("unicode \xf0\x9f\x90\x8d, full error: {" |
| 47 | + "'errmsg': u'unicode \\U0001f40d'}", str(exc)) |
| 48 | + elif 'PyPy' in sys.version: |
| 49 | + # PyPy displays unicode in repr differently. |
| 50 | + self.assertEqual("unicode \U0001f40d, full error: {" |
| 51 | + "'errmsg': 'unicode \\U0001f40d'}", str(exc)) |
| 52 | + else: |
| 53 | + self.assertEqual("unicode \U0001f40d, full error: {" |
| 54 | + "'errmsg': 'unicode \U0001f40d'}", str(exc)) |
| 55 | + try: |
| 56 | + raise exc |
| 57 | + except Exception: |
| 58 | + self.assertIn("full error", traceback.format_exc()) |
| 59 | + |
| 60 | + def test_unicode_strs_operation_failure(self): |
| 61 | + exc = OperationFailure(u'unicode \U0001f40d', 10, |
| 62 | + {"errmsg": u'unicode \U0001f40d'}) |
| 63 | + self._test_unicode_strs(exc) |
| 64 | + |
| 65 | + def test_unicode_strs_not_master_error(self): |
| 66 | + exc = NotMasterError(u'unicode \U0001f40d', |
| 67 | + {"errmsg": u'unicode \U0001f40d'}) |
| 68 | + self._test_unicode_strs(exc) |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + unittest.main() |
0 commit comments