myYouTube/tests/test_health.py

43 lines
1 KiB
Python
Raw Normal View History

from unittest.mock import MagicMock, patch
import pytest
from fastapi.testclient import TestClient
from app.db import get_db
from app.main import app
def _fake_db():
session = MagicMock()
session.execute.return_value = None
yield session
@pytest.fixture(autouse=True)
def override_get_db():
app.dependency_overrides[get_db] = _fake_db
yield
del app.dependency_overrides[get_db]
def test_health_ok():
with patch("app.services.metube_client.MeTubeClient.health", return_value=True):
client = TestClient(app)
response = client.get("/api/health")
assert response.status_code == 200
body = response.json()
assert body["status"] == "ok"
assert body["database"] == "ok"
assert body["metube"] == "ok"
def test_health_metube_unreachable():
with patch("app.services.metube_client.MeTubeClient.health", return_value=False):
client = TestClient(app)
response = client.get("/api/health")
assert response.status_code == 200
body = response.json()
assert body["metube"] == "error"