|
| 1 | +# pylint: disable=protected-access |
| 2 | +# pylint: disable=redefined-outer-name |
| 3 | +# pylint: disable=unused-argument |
| 4 | +# pylint: disable=unused-variable |
| 5 | +# pylint: disable=no-member |
| 6 | + |
| 7 | + |
| 8 | +from datetime import datetime |
| 9 | +from typing import Any |
| 10 | + |
| 11 | +import pytest |
| 12 | +from common_library.errors_classes import OsparcErrorMixin |
| 13 | + |
| 14 | + |
| 15 | +def test_get_full_class_name(): |
| 16 | + class A(OsparcErrorMixin): |
| 17 | + ... |
| 18 | + |
| 19 | + class B1(A): |
| 20 | + ... |
| 21 | + |
| 22 | + class B2(A): |
| 23 | + ... |
| 24 | + |
| 25 | + class C(B2): |
| 26 | + ... |
| 27 | + |
| 28 | + class B12(B1, ValueError): |
| 29 | + ... |
| 30 | + |
| 31 | + assert B1._get_full_class_name() == "A.B1" |
| 32 | + assert C._get_full_class_name() == "A.B2.C" |
| 33 | + assert A._get_full_class_name() == "A" |
| 34 | + |
| 35 | + # diamond inheritance (not usual but supported) |
| 36 | + assert B12._get_full_class_name() == "ValueError.A.B1.B12" |
| 37 | + |
| 38 | + |
| 39 | +def test_error_codes_and_msg_template(): |
| 40 | + class MyBaseError(OsparcErrorMixin, Exception): |
| 41 | + def __init__(self, **ctx: Any) -> None: |
| 42 | + super().__init__(**ctx) # Do not forget this for base exceptions! |
| 43 | + |
| 44 | + class MyValueError(MyBaseError, ValueError): |
| 45 | + msg_template = "Wrong value {value}" |
| 46 | + |
| 47 | + error = MyValueError(value=42) |
| 48 | + |
| 49 | + assert error.code == "ValueError.MyBaseError.MyValueError" |
| 50 | + assert f"{error}" == "Wrong value 42" |
| 51 | + |
| 52 | + class MyTypeError(MyBaseError, TypeError): |
| 53 | + code = "i_want_this" |
| 54 | + msg_template = "Wrong type {type}" |
| 55 | + |
| 56 | + error = MyTypeError(type="int") |
| 57 | + |
| 58 | + assert error.code == "i_want_this" |
| 59 | + assert f"{error}" == "Wrong type int" |
| 60 | + |
| 61 | + |
| 62 | +def test_error_msg_template_override(): |
| 63 | + class MyError(OsparcErrorMixin, Exception): |
| 64 | + msg_template = "Wrong value {value}" |
| 65 | + |
| 66 | + error_override_msg = MyError(msg_template="I want this message") |
| 67 | + assert str(error_override_msg) == "I want this message" |
| 68 | + |
| 69 | + error = MyError(value=42) |
| 70 | + assert hasattr(error, "value") |
| 71 | + assert str(error) == f"Wrong value {error.value}" |
| 72 | + |
| 73 | + |
| 74 | +def test_error_msg_template_nicer_override(): |
| 75 | + class MyError(OsparcErrorMixin, Exception): |
| 76 | + msg_template = "Wrong value {value}" |
| 77 | + |
| 78 | + def __init__(self, msg=None, **ctx: Any) -> None: |
| 79 | + super().__init__(**ctx) |
| 80 | + # positional argument msg (if defined) overrides the msg_template |
| 81 | + if msg: |
| 82 | + self.msg_template = msg |
| 83 | + |
| 84 | + error_override_msg = MyError("I want this message") |
| 85 | + assert str(error_override_msg) == "I want this message" |
| 86 | + |
| 87 | + error = MyError(value=42) |
| 88 | + assert hasattr(error, "value") |
| 89 | + assert str(error) == f"Wrong value {error.value}" |
| 90 | + |
| 91 | + |
| 92 | +def test_error_with_constructor(): |
| 93 | + class MyError(OsparcErrorMixin, ValueError): |
| 94 | + msg_template = "Wrong value {value}" |
| 95 | + |
| 96 | + # handy e.g. autocompletion |
| 97 | + def __init__(self, *, my_value: int = 42, **extra): |
| 98 | + super().__init__(**extra) |
| 99 | + self.value = my_value |
| 100 | + |
| 101 | + error = MyError(my_value=33, something_else="yes") |
| 102 | + assert error.value == 33 |
| 103 | + assert str(error) == "Wrong value 33" |
| 104 | + assert not hasattr(error, "my_value") |
| 105 | + |
| 106 | + # the autocompletion does not see this |
| 107 | + assert error.something_else == "yes" |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.parametrize( |
| 111 | + "str_format,ctx,expected", |
| 112 | + [ |
| 113 | + pytest.param("{value:10}", {"value": "Python"}, "Python ", id="left-align"), |
| 114 | + pytest.param( |
| 115 | + "{value:>10}", {"value": "Python"}, " Python", id="right-align" |
| 116 | + ), |
| 117 | + pytest.param( |
| 118 | + "{value:^10}", {"value": "Python"}, " Python ", id="center-align" |
| 119 | + ), |
| 120 | + pytest.param("{v:.2f}", {"v": 3.1415926}, "3.14", id="decimals"), |
| 121 | + pytest.param( |
| 122 | + "{dt:%Y-%m-%d %H:%M}", |
| 123 | + {"dt": datetime(2020, 5, 17, 18, 45)}, |
| 124 | + "2020-05-17 18:45", |
| 125 | + id="datetime", |
| 126 | + ), |
| 127 | + ], |
| 128 | +) |
| 129 | +def test_msg_template_with_different_formats( |
| 130 | + str_format: str, ctx: dict[str, Any], expected: str |
| 131 | +): |
| 132 | + class MyError(OsparcErrorMixin, ValueError): |
| 133 | + msg_template = str_format |
| 134 | + |
| 135 | + error = MyError(**ctx) |
| 136 | + assert str(error) == expected |
| 137 | + |
| 138 | + |
| 139 | +def test_missing_keys_in_msg_template_does_not_raise(): |
| 140 | + class MyError(OsparcErrorMixin, ValueError): |
| 141 | + msg_template = "{value} and {missing}" |
| 142 | + |
| 143 | + assert str(MyError(value=42)) == "42 and 'missing=?'" |
0 commit comments