diff --git a/examples/in_memory/main.py b/examples/in_memory/main.py index a7d6345..1df6bc2 100644 --- a/examples/in_memory/main.py +++ b/examples/in_memory/main.py @@ -33,7 +33,7 @@ async def clear(): @app.get("/date") @cache(namespace="test", expire=10) -async def get_data(): +async def get_date(): return pendulum.today() @@ -42,6 +42,13 @@ async def get_data(): async def get_datetime(request: Request, response: Response): return {"now": pendulum.now()} +@app.get("/sync-me") +@cache(namespace="test") +def sync_me(): + # as per the fastapi docs, this sync function is wrapped in a thread, + # thereby converted to async. fastapi-cache does the same. + return 42 + @app.on_event("startup") async def startup(): diff --git a/tests/test_decorator.py b/tests/test_decorator.py index 0ec4060..29db05d 100644 --- a/tests/test_decorator.py +++ b/tests/test_decorator.py @@ -1,6 +1,7 @@ import time import pendulum +from fastapi_cache import FastAPICache from starlette.testclient import TestClient from examples.in_memory.main import app @@ -21,3 +22,26 @@ def test_datetime(): now = pendulum.parse(now).replace(microsecond=0) assert now != now_ assert now == pendulum.now().replace(microsecond=0) + +def test_date(): + """Test path function without request or response arguments.""" + with TestClient(app) as client: + + response = client.get("/date") + assert pendulum.parse(response.json()) == pendulum.today() + + # do it again to test cache + response = client.get("/date") + assert pendulum.parse(response.json()) == pendulum.today() + + # now test with cache disabled, as that's a separate code path + FastAPICache._enable = False + response = client.get("/date") + assert pendulum.parse(response.json()) == pendulum.today() + FastAPICache._enable = True + +def test_sync(): + """Ensure that sync function support works.""" + with TestClient(app) as client: + response = client.get("/sync-me") + assert response.json() == 42