完成渠道模块的单元测试目录搭建,新增多个领域模型、端口、中间件、事件、服务以及基础设施层的单元测试文件,同时补充了conftest.py的环境变量配置,完善测试基础环境。
48 lines
1.3 KiB
Python
48 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
from fastapi import FastAPI
|
|
|
|
from yuxi.channel.container import setup_channel
|
|
|
|
|
|
class TestSetupChannel:
|
|
def test_setup_channel_with_fastapi(self):
|
|
app = FastAPI()
|
|
container = MagicMock()
|
|
setup_channel(app, container)
|
|
assert hasattr(app.state, "channel")
|
|
assert app.state.channel is container
|
|
|
|
def test_setup_channel_with_non_fastapi(self):
|
|
app = MagicMock()
|
|
container = MagicMock()
|
|
setup_channel(app, container)
|
|
assert not hasattr(app.state, "channel")
|
|
|
|
def test_get_channel(self):
|
|
from yuxi.channel.container import get_channel
|
|
from fastapi import Request
|
|
|
|
app = FastAPI()
|
|
container = MagicMock()
|
|
app.state.channel = container
|
|
|
|
request = MagicMock(spec=Request)
|
|
request.app = app
|
|
result = get_channel(request)
|
|
assert result is container
|
|
|
|
def test_get_channel_not_initialized(self):
|
|
from yuxi.channel.container import get_channel
|
|
from fastapi import Request, HTTPException
|
|
|
|
app = FastAPI()
|
|
request = MagicMock(spec=Request)
|
|
request.app = app
|
|
with pytest.raises(HTTPException) as exc_info:
|
|
get_channel(request)
|
|
assert exc_info.value.status_code == 503
|