fixed tests
Python Code Quality / python-code-quality (push) Successful in 9s
Python Test / python-test (push) Successful in 12s

This commit is contained in:
Brian Bjarke Jensen
2025-11-04 21:50:58 +01:00
parent 54f658d093
commit 298fb192de
7 changed files with 346 additions and 15 deletions
+29
View File
@@ -0,0 +1,29 @@
"""Integration tests configuration."""
import os
from typing import Iterator
import pytest
from fastapi.testclient import TestClient
@pytest.fixture(scope="session", autouse=True)
def set_test_environment() -> Iterator[None]:
"""Set environment to development for all integration tests."""
original_env = os.environ.get("ENVIRONMENT")
os.environ["ENVIRONMENT"] = "development"
yield
# Cleanup: restore original environment
if original_env is None:
os.environ.pop("ENVIRONMENT", None)
else:
os.environ["ENVIRONMENT"] = original_env
@pytest.fixture(scope="session")
def app_test_client() -> Iterator[TestClient]:
"""Provides a TestClient for the FastAPI app."""
# Import here after environment is set
from baby_monitor.main import app
with TestClient(app) as client:
yield client
+72
View File
@@ -0,0 +1,72 @@
"""Integration tests for FastAPI application."""
import pytest
from fastapi.testclient import TestClient
@pytest.mark.integration
def test_read_root(app_test_client: TestClient) -> None:
"""Test root endpoint returns expected message."""
response = app_test_client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Hello World"}
@pytest.mark.integration
def test_health_check(app_test_client: TestClient) -> None:
"""Test health check endpoint returns healthy status."""
response = app_test_client.get("/health")
assert response.status_code == 200
assert response.json() == {"status": "healthy"}
@pytest.mark.integration
def test_readiness_check(app_test_client: TestClient) -> None:
"""Test readiness check endpoint returns ready status."""
response = app_test_client.get("/ready")
assert response.status_code == 200
assert response.json() == {"status": "ready"}
@pytest.mark.integration
def test_openapi_docs_accessible(app_test_client: TestClient) -> None:
"""Test OpenAPI documentation is accessible."""
response = app_test_client.get("/docs")
assert response.status_code == 200
@pytest.mark.integration
def test_openapi_json_accessible(app_test_client: TestClient) -> None:
"""Test OpenAPI JSON schema is accessible."""
response = app_test_client.get("/openapi.json")
assert response.status_code == 200
assert "openapi" in response.json()
assert "info" in response.json()
@pytest.mark.integration
def test_nonexistent_endpoint_returns_404(app_test_client: TestClient) -> None:
"""Test requesting a non-existent endpoint returns 404."""
response = app_test_client.get("/nonexistent")
assert response.status_code == 404
@pytest.mark.integration
def test_root_endpoint_content_type(app_test_client: TestClient) -> None:
"""Test root endpoint returns JSON content type."""
response = app_test_client.get("/")
assert response.headers["content-type"] == "application/json"
@pytest.mark.integration
def test_health_endpoint_content_type(app_test_client: TestClient) -> None:
"""Test health endpoint returns JSON content type."""
response = app_test_client.get("/health")
assert response.headers["content-type"] == "application/json"
@pytest.mark.integration
def test_ready_endpoint_content_type(app_test_client: TestClient) -> None:
"""Test readiness endpoint returns JSON content type."""
response = app_test_client.get("/ready")
assert response.headers["content-type"] == "application/json"