ForcePilot/backend/test/unit/channel/test_exceptions.py
Kris bab30f2715
Some checks failed
Deploy VitePress site to Pages / build (push) Has been cancelled
Ruff Format Check / Ruff Format & Lint (push) Has been cancelled
Deploy VitePress site to Pages / Deploy (push) Has been cancelled
feat:0715
2026-07-15 12:30:58 +08:00

92 lines
3.3 KiB
Python

from __future__ import annotations
import pytest
from yuxi.channel.exceptions import (
ChannelConfigurationError,
ChannelError,
ChannelErrorClassification,
ChannelNotSupportedError,
ChannelPermanentError,
ChannelRateLimitedError,
ChannelRetryableError,
ChannelTransportReconnectRequested,
ChannelValidationError,
)
class TestChannelErrorClassification:
def test_members(self):
assert ChannelErrorClassification.RETRYABLE == "retryable"
assert ChannelErrorClassification.PERMANENT == "permanent"
assert ChannelErrorClassification.RATE_LIMITED == "rate_limited"
class TestChannelErrorHierarchy:
def test_channel_error_is_base_exception(self):
exc = ChannelError("base error")
assert isinstance(exc, Exception)
assert str(exc) == "base error"
def test_channel_permanent_error_inherits_channel_error(self):
exc = ChannelPermanentError("permanent")
assert isinstance(exc, ChannelError)
def test_channel_validation_error_inherits_permanent_error(self):
exc = ChannelValidationError("validation failed")
assert isinstance(exc, ChannelPermanentError)
assert isinstance(exc, ChannelError)
def test_channel_retryable_error_inherits_channel_error(self):
exc = ChannelRetryableError("retryable")
assert isinstance(exc, ChannelError)
def test_channel_rate_limited_error_inherits_retryable_error(self):
exc = ChannelRateLimitedError("rate limited")
assert isinstance(exc, ChannelRetryableError)
assert isinstance(exc, ChannelError)
assert exc.retry_after is None
def test_channel_rate_limited_error_with_retry_after(self):
exc = ChannelRateLimitedError("rate limited", retry_after=120)
assert exc.retry_after == 120
def test_channel_configuration_error_inherits_permanent_error(self):
exc = ChannelConfigurationError("bad config")
assert isinstance(exc, ChannelPermanentError)
def test_channel_not_supported_error_inherits_permanent_error(self):
exc = ChannelNotSupportedError("unsupported")
assert isinstance(exc, ChannelPermanentError)
def test_channel_transport_reconnect_requested_inherits_channel_error(self):
exc = ChannelTransportReconnectRequested("reconnect")
assert isinstance(exc, ChannelError)
class TestExceptionCatchability:
def test_base_catches_all(self):
exceptions = [
ChannelPermanentError("x"),
ChannelValidationError("x"),
ChannelRetryableError("x"),
ChannelRateLimitedError("x"),
ChannelConfigurationError("x"),
ChannelNotSupportedError("x"),
ChannelTransportReconnectRequested("x"),
]
for exc in exceptions:
with pytest.raises(ChannelError):
raise exc
def test_permanent_catches_validation_and_configuration(self):
with pytest.raises(ChannelPermanentError):
raise ChannelValidationError("x")
with pytest.raises(ChannelPermanentError):
raise ChannelConfigurationError("x")
with pytest.raises(ChannelPermanentError):
raise ChannelNotSupportedError("x")
def test_retryable_catches_rate_limited(self):
with pytest.raises(ChannelRetryableError):
raise ChannelRateLimitedError("x")