test: регрессионные тесты lifespan — старт при недоступных S3/NATS и закрытие созданных ресурсов
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
12ebb515c2
commit
1c56d39b09
@ -93,3 +93,67 @@ async def test_healthcheck_nats_follows_connection_state(
|
||||
|
||||
def test_router_prefix():
|
||||
assert router.prefix == "/api/v1"
|
||||
|
||||
|
||||
class _FakeNats:
|
||||
def __init__(self) -> None:
|
||||
self.closed = False
|
||||
|
||||
async def close(self) -> None:
|
||||
self.closed = True
|
||||
|
||||
|
||||
class _FakeS3Client:
|
||||
def __init__(self) -> None:
|
||||
self.session_closed = False
|
||||
|
||||
async def close_session(self) -> None:
|
||||
self.session_closed = True
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def _no_smb(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr(app_module.smbclient, "register_session", lambda *args, **kwargs: None)
|
||||
monkeypatch.setattr(app_module.smbclient, "reset_connection_cache", lambda: None)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("_no_smb")
|
||||
async def test_lifespan_starts_when_s3_and_nats_unavailable(monkeypatch: pytest.MonkeyPatch):
|
||||
async def unavailable() -> None:
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(app_module, "_connect_s3", unavailable)
|
||||
monkeypatch.setattr(app_module, "_connect_nats", unavailable)
|
||||
reached_yield = False
|
||||
|
||||
async with app_module.lifespan(app_module.app):
|
||||
reached_yield = True
|
||||
assert app_module.app.state.storage is None
|
||||
assert app_module.app.state.nats is None
|
||||
|
||||
assert reached_yield
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("_no_smb")
|
||||
async def test_lifespan_closes_created_resources(monkeypatch: pytest.MonkeyPatch):
|
||||
fake_nats = _FakeNats()
|
||||
s3_client = _FakeS3Client()
|
||||
storage = SimpleNamespace(client=s3_client)
|
||||
|
||||
async def connect_s3() -> SimpleNamespace:
|
||||
return storage
|
||||
|
||||
async def connect_nats() -> _FakeNats:
|
||||
return fake_nats
|
||||
|
||||
monkeypatch.setattr(app_module, "_connect_s3", connect_s3)
|
||||
monkeypatch.setattr(app_module, "_connect_nats", connect_nats)
|
||||
|
||||
async with app_module.lifespan(app_module.app):
|
||||
assert app_module.app.state.storage is storage
|
||||
assert app_module.app.state.nats is fake_nats
|
||||
assert not fake_nats.closed
|
||||
assert not s3_client.session_closed
|
||||
|
||||
assert fake_nats.closed
|
||||
assert s3_client.session_closed
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user