Skip to content

Commit

Permalink
🧪 test: add check db unhealthy
Browse files Browse the repository at this point in the history
  • Loading branch information
ezeparziale committed Dec 13, 2024
1 parent 9fcb437 commit 1a2888b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
26 changes: 26 additions & 0 deletions app/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from unittest.mock import MagicMock

import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session, sessionmaker

from app.core.config import settings
Expand Down Expand Up @@ -90,3 +93,26 @@ def create_user_model(post: dict):
session.commit()
post = session.query(Post).all()
return post


@pytest.fixture
def mock_db_error():
mock_session = MagicMock()
mock_session.execute.side_effect = SQLAlchemyError("Simulated database error")
return mock_session


@pytest.fixture
def client_with_db_error(mock_db_error):
def override_get_db():
try:
yield mock_db_error
finally:
mock_db_error.close()

app.dependency_overrides[get_db] = override_get_db

try:
yield TestClient(app)
finally:
app.dependency_overrides.pop(get_db)
14 changes: 14 additions & 0 deletions app/tests/test_health.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ def validate(data):
print(data)

assert res.status_code == 200


def test_api_health_db_error(client_with_db_error: TestClient):
res = client_with_db_error.get("/health")

def validate(data):
return APIStatus(**data)

data = res.json()
validate(data)
print(data)

assert res.status_code == 200
assert data["db_status"] == "Unhealthy"

0 comments on commit 1a2888b

Please sign in to comment.