File size: 2,099 Bytes
25834e9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | """Unit tests for AppError hierarchy."""
import pytest
from app.errors import (
AuthenticationError,
ConflictError,
ForbiddenError,
NotFoundError,
RateLimitError,
ValidationError,
)
class TestAppErrorSubclasses:
def test_validation_error(self):
e = ValidationError("bad input")
assert e.status_code == 400
assert e.error_code == "validation_error"
assert e.message == "bad input"
def test_authentication_error(self):
e = AuthenticationError("not authenticated")
assert e.status_code == 401
assert e.error_code == "authentication_error"
def test_forbidden_error(self):
e = ForbiddenError("not allowed")
assert e.status_code == 403
assert e.error_code == "forbidden"
def test_not_found_error(self):
e = NotFoundError("resource missing")
assert e.status_code == 404
assert e.error_code == "not_found"
def test_conflict_error(self):
e = ConflictError("already exists")
assert e.status_code == 409
assert e.error_code == "conflict"
def test_rate_limit_error(self):
e = RateLimitError("slow down")
assert e.status_code == 429
assert e.error_code == "rate_limit_exceeded"
class TestAppErrorToDict:
def test_basic(self):
e = NotFoundError("url not found")
assert e.to_dict() == {"error": "url not found", "code": "not_found"}
@pytest.mark.parametrize(
"kwargs, key, value",
[
({"field": "alias"}, "field", "alias"),
({"details": {"min": 1, "max": 10}}, "details", {"min": 1, "max": 10}),
],
ids=["with_field", "with_details"],
)
def test_optional_key_present(self, kwargs, key, value):
e = ValidationError("invalid", **kwargs)
assert e.to_dict()[key] == value
def test_no_optional_keys_when_absent(self):
d = NotFoundError("missing").to_dict()
assert "field" not in d
assert "details" not in d
|