57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
|
|
"""Tests for the FastAPI application."""
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
from fastapi.testclient import TestClient
|
||
|
|
|
||
|
|
from app.main import app
|
||
|
|
|
||
|
|
client = TestClient(app)
|
||
|
|
|
||
|
|
|
||
|
|
class TestHealth:
|
||
|
|
def test_health_endpoint_returns_200(self):
|
||
|
|
response = client.get("/api/health")
|
||
|
|
assert response.status_code == 200
|
||
|
|
data = response.json()
|
||
|
|
assert data["status"] == "healthy"
|
||
|
|
assert data["service"] == "micro-api"
|
||
|
|
assert data["version"] == "0.1.0"
|
||
|
|
assert "timestamp" in data
|
||
|
|
|
||
|
|
def test_root_returns_service_info(self):
|
||
|
|
response = client.get("/")
|
||
|
|
assert response.status_code == 200
|
||
|
|
data = response.json()
|
||
|
|
assert data["service"] == "micro-api"
|
||
|
|
assert data["status"] == "running"
|
||
|
|
|
||
|
|
|
||
|
|
class TestDatabase:
|
||
|
|
def test_init_db_creates_tables(self):
|
||
|
|
from app.database import init_db
|
||
|
|
|
||
|
|
# Should not raise
|
||
|
|
init_db()
|
||
|
|
|
||
|
|
|
||
|
|
class TestModels:
|
||
|
|
def test_item_create_validation(self):
|
||
|
|
from app.models import ItemCreate
|
||
|
|
|
||
|
|
item = ItemCreate(name="test-item")
|
||
|
|
assert item.name == "test-item"
|
||
|
|
|
||
|
|
def test_item_create_requires_name(self):
|
||
|
|
from app.models import ItemCreate
|
||
|
|
|
||
|
|
with pytest.raises(Exception):
|
||
|
|
ItemCreate()
|
||
|
|
|
||
|
|
def test_item_response_serialization(self):
|
||
|
|
from app.models import ItemResponse
|
||
|
|
|
||
|
|
item = ItemResponse(id=1, name="test")
|
||
|
|
data = item.model_dump()
|
||
|
|
assert data["id"] == 1
|
||
|
|
assert data["name"] == "test"
|