|
| 1 | +import pytest |
| 2 | + |
| 3 | +from taskiq.error import Error |
| 4 | +from taskiq.exceptions import SecurityError, TaskiqResultTimeoutError |
| 5 | + |
| 6 | + |
| 7 | +class SimpleError(Error): |
| 8 | + __template__ = "simple" |
| 9 | + |
| 10 | + |
| 11 | +class ValueTemplateError(Error): |
| 12 | + __template__ = "value={value}" |
| 13 | + value: int |
| 14 | + |
| 15 | + |
| 16 | +class DefaultValueTemplateError(Error): |
| 17 | + __template__ = "value={value}" |
| 18 | + value: int = 10 |
| 19 | + |
| 20 | + |
| 21 | +class BaseError(Error): |
| 22 | + __template__ = "base={base}, child={child}" |
| 23 | + base: int = 1 |
| 24 | + |
| 25 | + |
| 26 | +class ChildError(BaseError): |
| 27 | + child: str |
| 28 | + |
| 29 | + |
| 30 | +class MissingAnnotationError(Error): |
| 31 | + __template__ = "value={value}" |
| 32 | + |
| 33 | + |
| 34 | +class IndexedTemplateError(Error): |
| 35 | + __template__ = "{payload[key]}" |
| 36 | + payload: dict[str, str] |
| 37 | + |
| 38 | + |
| 39 | +def test_simple_error_message_and_repr() -> None: |
| 40 | + error = SimpleError() |
| 41 | + assert str(error) == "simple" |
| 42 | + assert error.args == ("simple",) |
| 43 | + assert repr(error).endswith(".SimpleError()") |
| 44 | + |
| 45 | + |
| 46 | +def test_template_with_required_value() -> None: |
| 47 | + error = ValueTemplateError(value=3) |
| 48 | + assert str(error) == "value=3" |
| 49 | + assert repr(error).endswith(".ValueTemplateError(value=3)") |
| 50 | + |
| 51 | + |
| 52 | +def test_missing_argument_raises_type_error() -> None: |
| 53 | + with pytest.raises(TypeError, match="Missing arguments: 'value'"): |
| 54 | + ValueTemplateError() |
| 55 | + |
| 56 | + |
| 57 | +def test_undeclared_argument_raises_type_error() -> None: |
| 58 | + with pytest.raises(TypeError, match="Undeclared arguments: 'extra'"): |
| 59 | + ValueTemplateError(value=1, extra=2) |
| 60 | + |
| 61 | + |
| 62 | +def test_default_value_is_used_without_kwargs() -> None: |
| 63 | + error = DefaultValueTemplateError() |
| 64 | + assert str(error) == "value=10" |
| 65 | + assert repr(error).endswith(".DefaultValueTemplateError(value=10)") |
| 66 | + |
| 67 | + |
| 68 | +def test_annotations_are_collected_from_inheritance() -> None: |
| 69 | + error = ChildError(child="ok") |
| 70 | + assert str(error) == "base=1, child=ok" |
| 71 | + assert repr(error).endswith(".ChildError(base=1, child='ok')") |
| 72 | + |
| 73 | + |
| 74 | +def test_template_fields_must_be_annotated() -> None: |
| 75 | + with pytest.raises(ValueError, match="Fields must be annotated: 'value'"): |
| 76 | + MissingAnnotationError() |
| 77 | + |
| 78 | + |
| 79 | +def test_indexed_template_field_does_not_require_extra_annotation() -> None: |
| 80 | + error = IndexedTemplateError(payload={"key": "value"}) |
| 81 | + assert str(error) == "value" |
| 82 | + |
| 83 | + |
| 84 | +def test_taskiq_exceptions_use_error_base_correctly() -> None: |
| 85 | + timeout_error = TaskiqResultTimeoutError(timeout=1.5) |
| 86 | + security_error = SecurityError(description="boom") |
| 87 | + assert str(timeout_error) == "Waiting for task results has timed out, timeout=1.5" |
| 88 | + assert str(security_error) == "Security exception occurred: boom" |
0 commit comments