|
| 1 | +"""Tests for exception classes.""" |
| 2 | + |
| 3 | +from brainus_ai.exceptions import ( |
| 4 | + APIError, |
| 5 | + AuthenticationError, |
| 6 | + BrainusError, |
| 7 | + QuotaExceededError, |
| 8 | + RateLimitError, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +class TestBrainusError: |
| 13 | + def test_is_exception(self) -> None: |
| 14 | + assert issubclass(BrainusError, Exception) |
| 15 | + |
| 16 | + def test_message_attribute(self) -> None: |
| 17 | + e = BrainusError("something broke") |
| 18 | + assert e.message == "something broke" |
| 19 | + |
| 20 | + def test_str_representation(self) -> None: |
| 21 | + e = BrainusError("something broke") |
| 22 | + assert "something broke" in str(e) |
| 23 | + |
| 24 | + |
| 25 | +class TestAuthenticationError: |
| 26 | + def test_is_brainus_error(self) -> None: |
| 27 | + assert issubclass(AuthenticationError, BrainusError) |
| 28 | + |
| 29 | + def test_message(self) -> None: |
| 30 | + e = AuthenticationError("bad key") |
| 31 | + assert e.message == "bad key" |
| 32 | + |
| 33 | + |
| 34 | +class TestRateLimitError: |
| 35 | + def test_is_brainus_error(self) -> None: |
| 36 | + assert issubclass(RateLimitError, BrainusError) |
| 37 | + |
| 38 | + def test_retry_after_none_by_default(self) -> None: |
| 39 | + e = RateLimitError() |
| 40 | + assert e.retry_after is None |
| 41 | + |
| 42 | + def test_retry_after_set(self) -> None: |
| 43 | + e = RateLimitError("limit hit", retry_after=60) |
| 44 | + assert e.retry_after == 60 |
| 45 | + |
| 46 | + def test_default_message(self) -> None: |
| 47 | + e = RateLimitError() |
| 48 | + assert e.message == "Rate limit exceeded" |
| 49 | + |
| 50 | + def test_custom_message(self) -> None: |
| 51 | + e = RateLimitError("slow down") |
| 52 | + assert e.message == "slow down" |
| 53 | + |
| 54 | + |
| 55 | +class TestQuotaExceededError: |
| 56 | + def test_is_brainus_error(self) -> None: |
| 57 | + assert issubclass(QuotaExceededError, BrainusError) |
| 58 | + |
| 59 | + def test_message(self) -> None: |
| 60 | + e = QuotaExceededError("quota gone") |
| 61 | + assert e.message == "quota gone" |
| 62 | + |
| 63 | + |
| 64 | +class TestAPIError: |
| 65 | + def test_is_brainus_error(self) -> None: |
| 66 | + assert issubclass(APIError, BrainusError) |
| 67 | + |
| 68 | + def test_status_code_none_by_default(self) -> None: |
| 69 | + e = APIError("error") |
| 70 | + assert e.status_code is None |
| 71 | + |
| 72 | + def test_status_code_set(self) -> None: |
| 73 | + e = APIError("server error", status_code=500) |
| 74 | + assert e.status_code == 500 |
| 75 | + |
| 76 | + def test_message(self) -> None: |
| 77 | + e = APIError("bad gateway", status_code=502) |
| 78 | + assert e.message == "bad gateway" |
| 79 | + |
| 80 | + |
| 81 | +class TestInstanceofChecks: |
| 82 | + def test_all_subclass_of_brainus_error(self) -> None: |
| 83 | + for cls in [AuthenticationError, RateLimitError, QuotaExceededError, APIError]: |
| 84 | + assert issubclass(cls, BrainusError) |
| 85 | + |
| 86 | + def test_all_subclass_of_exception(self) -> None: |
| 87 | + for cls in [BrainusError, AuthenticationError, RateLimitError, QuotaExceededError, APIError]: |
| 88 | + assert issubclass(cls, Exception) |
| 89 | + |
| 90 | + def test_can_catch_with_base_class(self) -> None: |
| 91 | + try: |
| 92 | + raise AuthenticationError("bad key") |
| 93 | + except BrainusError as e: |
| 94 | + assert e.message == "bad key" |
| 95 | + else: |
| 96 | + assert False, "Should have caught exception" |
0 commit comments