Files
fastapi-cache/tests/test_decorator.py
Martijn Pieters f78a599bbc Decode cache data to the correct endpoint type
Use the return annotation to decode cached data to the correct type.
This follows the same logic FastAPI uses to JSON request bodies.

For the PickleCoder, this is a no-op as pickle already stores type
information in the serialised data.
2023-05-08 16:55:05 +01:00

89 lines
2.8 KiB
Python

import time
from typing import Generator
import pendulum
import pytest
from starlette.testclient import TestClient
from examples.in_memory.main import app
from fastapi_cache import FastAPICache
from fastapi_cache.backends.inmemory import InMemoryBackend
@pytest.fixture(autouse=True)
def init_cache() -> Generator:
FastAPICache.init(InMemoryBackend())
yield
FastAPICache.reset()
def test_datetime() -> None:
with TestClient(app) as client:
response = client.get("/datetime")
now = response.json().get("now")
now_ = pendulum.now().replace(microsecond=0)
assert pendulum.parse(now).replace(microsecond=0) == now_
response = client.get("/datetime")
now = response.json().get("now")
assert pendulum.parse(now).replace(microsecond=0) == now_
time.sleep(3)
response = client.get("/datetime")
now = response.json().get("now")
now = pendulum.parse(now).replace(microsecond=0)
assert now != now_
assert now == pendulum.now().replace(microsecond=0)
def test_date() -> None:
"""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() -> None:
"""Ensure that sync function support works."""
with TestClient(app) as client:
response = client.get("/sync-me")
assert response.json() == 42
def test_cache_response_obj() -> None:
with TestClient(app) as client:
cache_response = client.get("cache_response_obj")
assert cache_response.json() == {"a": 1}
get_cache_response = client.get("cache_response_obj")
assert get_cache_response.json() == {"a": 1}
assert get_cache_response.headers.get("cache-control")
assert get_cache_response.headers.get("etag")
def test_kwargs() -> None:
with TestClient(app) as client:
name = "Jon"
response = client.get("/kwargs", params={"name": name})
assert response.json() == {"name": name}
def test_method() -> None:
with TestClient(app) as client:
response = client.get("/method")
assert response.json() == 17
def test_pydantic_model() -> None:
with TestClient(app) as client:
r1 = client.get("/pydantic_instance").json()
r2 = client.get("/pydantic_instance").json()
assert r1 == r2