Files
KiloStar/tests/unit/test_database_exception.py
T
zhaoxi 99520c69d7 feat(system):优化后端
1.新增后端测试
2.增加了后端的加密
3.增加了i18n(国际化)
2026-05-31 15:39:34 +00:00

73 lines
1.7 KiB
Python

"""``database_exception`` 装饰器:透传异常并通过 logger 上报。"""
import pytest
from pydantic import BaseModel, ValidationError
from sqlalchemy.exc import IntegrityError, OperationalError
from kilostar.core.postgres_database.database_exception import database_exception
from kilostar.utils.error import UserNotExistError
async def test_normal_path_returns_value():
@database_exception
async def ok() -> int:
return 42
assert await ok() == 42
async def test_passes_args_and_kwargs():
@database_exception
async def add(a, b, *, c) -> int:
return a + b + c
assert await add(1, 2, c=3) == 6
async def test_validation_error_propagates():
class Model(BaseModel):
x: int
@database_exception
async def boom() -> None:
Model(x="not-int") # type: ignore[arg-type]
with pytest.raises(ValidationError):
await boom()
async def test_integrity_error_propagates():
@database_exception
async def boom() -> None:
raise IntegrityError("stmt", {}, Exception("dup"))
with pytest.raises(IntegrityError):
await boom()
async def test_operational_error_propagates():
@database_exception
async def boom() -> None:
raise OperationalError("stmt", {}, Exception("conn"))
with pytest.raises(OperationalError):
await boom()
async def test_user_not_exist_error_propagates():
@database_exception
async def boom() -> None:
raise UserNotExistError("missing")
with pytest.raises(UserNotExistError):
await boom()
async def test_unexpected_error_propagates():
@database_exception
async def boom() -> None:
raise RuntimeError("unexpected")
with pytest.raises(RuntimeError):
await boom()