Skip to content

Commit f33b21f

Browse files
committed
Move Health endpoint model
1 parent 53cbe9c commit f33b21f

3 files changed

Lines changed: 29 additions & 8 deletions

File tree

hackspaceapi/main.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from fastapi.middleware.cors import CORSMiddleware
66
from fastapi.responses import RedirectResponse
77
from prometheus_fastapi_instrumentator import Instrumentator
8-
from pydantic import BaseModel, Field
98

109
from hackspaceapi import VERSION
10+
from hackspaceapi.models import HealthResponseModel
1111

1212
from .events import events
1313
from .spaceapi import spaceapi
@@ -34,11 +34,6 @@
3434
Instrumentator().instrument(app).expose(app, include_in_schema=False)
3535

3636

37-
class HealthResponse(BaseModel):
38-
health: str = Field(description="State of the API", examples=["ok", "error"])
39-
version: str = Field(description="Version of the API", examples=[VERSION])
40-
41-
4237
@app.get("/", include_in_schema=False)
4338
def root():
4439
return RedirectResponse("/docs")
@@ -49,5 +44,5 @@ def root():
4944
description="Healthcheck endpoint to ensure the API is running correctly",
5045
tags=["Health"],
5146
)
52-
def health() -> HealthResponse:
53-
return HealthResponse(health="ok", version=VERSION)
47+
def health() -> HealthResponseModel:
48+
return HealthResponseModel(health="ok", version=VERSION)

hackspaceapi/models/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from pydantic import BaseModel, Field
2+
3+
from hackspaceapi import VERSION
4+
5+
6+
class HealthResponseModel(BaseModel):
7+
health: str = Field(description="State of the API", examples=["ok", "error"])
8+
version: str = Field(description="Version of the API", examples=[VERSION])

tests/test_health.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from hackspaceapi import VERSION
2+
from tests.utils import FastAPIVCRTestCase, client
3+
4+
5+
class HealthTestCase(FastAPIVCRTestCase):
6+
"""
7+
Test the health endpoint
8+
"""
9+
10+
def test_health_endpoint(self):
11+
response = client.get("/health")
12+
assert response.status_code == 200
13+
assert response.headers["Content-Type"] == "application/json"
14+
15+
data = response.json()
16+
17+
assert data["health"] == "ok"
18+
assert data["version"] == VERSION

0 commit comments

Comments
 (0)