Files
xiaoxia-saas/tests/unit/domain/test_exceptions.py
T
xiaoxia 63d4048354
CI/CD Pipeline / Check if frontend-only change (push) Has been cancelled
CI/CD Pipeline / Validate - Code Quality (push) Has been cancelled
CI/CD Pipeline / Validate - Type Check (mypy) (push) Has been cancelled
CI/CD Pipeline / Validate - Migration (alembic) (push) Has been cancelled
CI/CD Pipeline / Unit Tests (push) Has been cancelled
CI/CD Pipeline / Integration Tests (push) Has been cancelled
CI/CD Pipeline / Frontend Lint (push) Has been cancelled
CI/CD Pipeline / Frontend Unit Tests (push) Has been cancelled
CI/CD Pipeline / PR Build API Image (push) Has been cancelled
CI/CD Pipeline / PR Build Web Image (push) Has been cancelled
CI/CD Pipeline / PR Build Worker Image (push) Has been cancelled
CI/CD Pipeline / Build Staging API Image (push) Has been cancelled
CI/CD Pipeline / Build Staging Web Image (push) Has been cancelled
CI/CD Pipeline / Build Staging Worker Image (push) Has been cancelled
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (push) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (push) Has been cancelled
CI/CD Pipeline / Build Production API Image (push) Has been cancelled
CI/CD Pipeline / Build Production Web Image (push) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (push) Has been cancelled
CI/CD Pipeline / Deploy Production (push) Has been cancelled
CI/CD Pipeline / Production Browser E2E (push) Has been cancelled
CI/CD Pipeline / ACR Image Cleanup (push) Has been cancelled
CI/CD Pipeline / Canary Release to Production (push) Has been cancelled
CI/CD Pipeline / CI Gate (push) Has been cancelled
test(domain): wave196 小模块批量单测 +74 (#1160)
2026-07-30 00:25:01 +08:00

108 lines
3.0 KiB
Python
Executable File

"""domain exceptions 单元测试."""
from __future__ import annotations
import pytest
from domain.exceptions import (
DomainError,
NotFoundError,
QuotaExceededError,
ValidationError,
)
class TestDomainError:
"""DomainError 基类测试."""
def test_is_exception(self):
assert issubclass(DomainError, Exception)
def test_raise_and_catch(self):
with pytest.raises(DomainError):
raise DomainError("something went wrong")
def test_message(self):
err = DomainError("test message")
assert str(err) == "test message"
def test_empty_message(self):
err = DomainError()
assert str(err) == ""
class TestNotFoundError:
"""NotFoundError 测试."""
def test_is_domain_error(self):
assert issubclass(NotFoundError, DomainError)
def test_raise_and_catch_as_domain(self):
with pytest.raises(DomainError):
raise NotFoundError("user not found")
def test_raise_and_catch_specific(self):
with pytest.raises(NotFoundError):
raise NotFoundError("user not found")
def test_message(self):
err = NotFoundError("resource not found")
assert str(err) == "resource not found"
class TestValidationError:
"""ValidationError 测试."""
def test_is_domain_error(self):
assert issubclass(ValidationError, DomainError)
def test_raise_and_catch_as_domain(self):
with pytest.raises(DomainError):
raise ValidationError("invalid input")
def test_raise_and_catch_specific(self):
with pytest.raises(ValidationError):
raise ValidationError("invalid input")
def test_message(self):
err = ValidationError("bad data")
assert str(err) == "bad data"
class TestQuotaExceededError:
"""QuotaExceededError 测试."""
def test_is_domain_error(self):
assert issubclass(QuotaExceededError, DomainError)
def test_constructor_stores_fields(self):
err = QuotaExceededError("storage", limit=100.0, used=150.0)
assert err.dimension == "storage"
assert err.limit == 100.0
assert err.used == 150.0
def test_message_format(self):
err = QuotaExceededError("storage", limit=100.0, used=150.0)
assert "storage" in str(err)
assert "150.0" in str(err)
assert "100.0" in str(err)
assert "Quota exceeded" in str(err)
def test_raise_and_catch_as_domain(self):
with pytest.raises(DomainError):
raise QuotaExceededError("api_calls", limit=1000, used=2000)
def test_raise_and_catch_specific(self):
with pytest.raises(QuotaExceededError):
raise QuotaExceededError("api_calls", limit=1000, used=2000)
def test_int_values(self):
err = QuotaExceededError("count", limit=100, used=150)
assert err.limit == 100
assert err.used == 150
assert "150/100" in str(err)
def test_float_values(self):
err = QuotaExceededError("size", limit=10.5, used=20.3)
assert err.limit == 10.5
assert err.used == 20.3