44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
|
|
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.api.health.httpx.get") as mock_get:
|
||
|
|
mock_get.return_value = MagicMock(status_code=200)
|
||
|
|
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.api.health.httpx.get", side_effect=ConnectionError):
|
||
|
|
client = TestClient(app)
|
||
|
|
response = client.get("/api/health")
|
||
|
|
|
||
|
|
assert response.status_code == 200
|
||
|
|
body = response.json()
|
||
|
|
assert body["metube"] == "error"
|