Exemplo n.º 1
0
def test_custom_output():
    async def failure_handler(**kwargs):
        is_success = all(kwargs.values())
        return {
            "status": "success" if is_success else "failure",
            "results": [
                {"condition": condition, "output": value}
                for condition, value in kwargs.items()
            ],
        }

    def sick():
        return False

    def healthy():
        return True

    app = FastAPI()
    app.add_api_route(
        "/health", health([sick, healthy], failure_handler=failure_handler)
    )
    with TestClient(app) as client:
        res = client.get("/health")
        assert res.status_code == 503
        assert res.json() == {
            "status": "failure",
            "results": [
                {"condition": "sick", "output": False},
                {"condition": "healthy", "output": True},
            ],
        }
Exemplo n.º 2
0
def test_single_condition():
    def healthy():
        return True

    app = FastAPI()
    app.add_api_route("/health", health([healthy]))
    with TestClient(app) as client:
        res = client.get("/health")
        assert res.status_code == 200
Exemplo n.º 3
0
def test_coroutine_condition():
    async def async_health():
        return True

    app = FastAPI()
    app.add_api_route("/health", health([async_health]))
    with TestClient(app) as client:
        res = client.get("/health")
        assert res.status_code == 200
Exemplo n.º 4
0
def test_json_response():
    def healthy_dict():
        return {"potato": "yes"}

    app = FastAPI()
    app.add_api_route("/health", health([healthy_dict]))
    with TestClient(app) as client:
        res = client.get("/health")
        assert res.status_code == 200
        assert res.json() == {"potato": "yes"}
Exemplo n.º 5
0
def test_endpoint_with_dependency():
    def healthy():
        return True

    def healthy_with_dependency(condition_banana: bool = Depends(healthy)):
        return condition_banana

    app = FastAPI()
    app.add_api_route("/health", health([healthy_with_dependency]))
    with TestClient(app) as client:
        res = client.get("/health")
        assert res.status_code == 200
Exemplo n.º 6
0
def test_hybrid():
    def healthy_dict():
        return True

    def sick():
        return False

    def also_healthy_dict():
        return {"banana": "yes"}

    app = FastAPI()
    app.add_api_route("/health", health([healthy_dict, also_healthy_dict, sick]))
    with TestClient(app) as client:
        res = client.get("/health")
        assert res.status_code == 503
        assert res.json() == {"banana": "yes"}
Exemplo n.º 7
0
def test_success_handler():
    async def success_handler(**kwargs):
        return kwargs

    def healthy():
        return True

    def another_healthy():
        return True

    app = FastAPI()
    app.add_api_route(
        "/health", health([healthy, another_healthy], success_handler=success_handler)
    )
    with TestClient(app) as client:
        res = client.get("/health")
        assert res.status_code == 200
        assert res.json() == {"healthy": True, "another_healthy": True}
Exemplo n.º 8
0
    home_city_to_work_train = get_home_train(apikey, station_home_city_train,
                                             station_work,
                                             arrival_home_to_work_bus)

    arrival_home_to_work_bus = datetime.strptime(
        home_to_work_bus['arrival'], "%Y-%m-%dT%H:%M:%S%z").strftime("%H:%M")

    departure_home_city_to_work_train = datetime.strptime(
        home_city_to_work_train['departure'],
        "%Y-%m-%dT%H:%M:%S%z").strftime("%H:%M")
    arrival_home_city_to_work_train = datetime.strptime(
        home_city_to_work_train['arrival'],
        "%Y-%m-%dT%H:%M:%S%z").strftime("%H:%M")

    data = {
        'departure_home_to_work_bus_0': departure_home_to_work_bus,
        'arrival_home_to_work_bus_1': arrival_home_to_work_bus,
        'departure_home_city_to_work_train_2':
        departure_home_city_to_work_train,
        'arrival_home_city_to_work_train_3': arrival_home_city_to_work_train
    }
    return JSONResponse(content=data)


def is_api_online():
    return True


app.add_api_route("/health", health([is_api_online]))
Exemplo n.º 9
0
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi_health import health

import server.db.database as db
from server.api.v1.api import api_router as api_v1
from server.core.config import settings

app = FastAPI(
    title=settings.PROJECT_NAME,
    openapi_url=f"{settings.API_V1_PREFIX}/openapi.json"
)

# use static folder
app.mount('/static', StaticFiles(directory='server/static'), name='static')

# route api
app.include_router(api_v1, prefix=settings.API_V1_PREFIX)


# add health check
def is_database_online():
    # test = db.test_db
    # print(test)
    return True

app.add_api_route("/hc", health([is_database_online]))