ForcePilot/backend/test/unit/channel/startup/test_setup_channel.py

48 lines
1.3 KiB
Python
Raw Normal View History

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