Files
fastapi-cache/tests/test_decorator.py

143 lines
5.0 KiB
Python
Raw Normal View History

import time
from http import HTTPStatus
from typing import Any, Generator
import pendulum
import pytest
from starlette.testclient import TestClient
from examples.in_memory.main import app
2022-11-07 16:39:17 +08:00
from fastapi_cache import FastAPICache
from fastapi_cache.backends.inmemory import InMemoryBackend
@pytest.fixture(autouse=True)
def _init_cache() -> Generator[Any, Any, None]: # pyright: ignore[reportUnusedFunction]
FastAPICache.init(InMemoryBackend())
yield
FastAPICache.reset()
def test_datetime() -> None:
with TestClient(app) as client:
response = client.get("/datetime")
assert response.headers.get("X-FastAPI-Cache") == "MISS"
now = response.json().get("now")
# now_ = pendulum.now()
# assert pendulum.parse(now) == now_
now_ = pendulum.parse(now)
response = client.get("/datetime")
assert response.headers.get("X-FastAPI-Cache") == "HIT"
now = response.json().get("now")
2024-07-24 22:24:30 +08:00
assert pendulum.parse(now) == now_
time.sleep(3)
response = client.get("/datetime")
# now = response.json().get("now")
assert response.headers.get("X-FastAPI-Cache") == "MISS"
now = response.json().get("now")
2024-07-24 22:24:30 +08:00
now = pendulum.parse(now)
assert now != now_
# assert now == pendulum.now()
2022-10-14 21:59:33 +02:00
def test_date() -> None:
2022-10-14 21:59:33 +02:00
"""Test path function without request or response arguments."""
with TestClient(app) as client:
response = client.get("/date")
assert response.headers.get("X-FastAPI-Cache") == "MISS"
2024-07-24 22:24:30 +08:00
assert pendulum.parse(response.json()) == pendulum.today()
2022-10-14 21:59:33 +02:00
# do it again to test cache
response = client.get("/date")
assert response.headers.get("X-FastAPI-Cache") == "HIT"
2024-07-24 22:24:30 +08:00
assert pendulum.parse(response.json()) == pendulum.today()
2022-10-14 21:59:33 +02:00
# now test with cache disabled, as that's a separate code path
2023-05-09 17:33:07 +01:00
FastAPICache._enable = False # pyright: ignore[reportPrivateUsage]
2022-10-14 21:59:33 +02:00
response = client.get("/date")
assert "X-FastAPI-Cache" not in response.headers
2024-07-24 22:24:30 +08:00
assert pendulum.parse(response.json()) == pendulum.today()
FastAPICache._enable = True # pyright: ignore[reportPrivateUsage]
2022-10-14 21:59:33 +02:00
def test_sync() -> None:
2022-10-14 21:59:33 +02:00
"""Ensure that sync function support works."""
with TestClient(app) as client:
response = client.get("/sync-me")
assert response.json() == 42
2022-11-04 17:34:20 +08:00
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")
2023-02-15 10:35:41 +09:00
2023-02-15 10:45:19 +08:00
2023-02-15 10:35:41 +09:00
def test_kwargs() -> None:
with TestClient(app) as client:
name = "Jon"
2023-02-15 10:45:19 +08:00
response = client.get("/kwargs", params={"name": name})
assert "X-FastAPI-Cache" not in response.headers
2023-02-15 10:35:41 +09:00
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")
assert r1.headers.get("X-FastAPI-Cache") == "MISS"
r2 = client.get("/pydantic_instance")
assert r2.headers.get("X-FastAPI-Cache") == "HIT"
assert r1.json() == r2.json()
def test_non_get() -> None:
with TestClient(app) as client:
2024-05-09 14:51:58 +08:00
response = client.put("/cached_put")
assert response.status_code == HTTPStatus.METHOD_NOT_ALLOWED
assert "X-FastAPI-Cache" not in response.headers
assert response.json() != {"value": 1}
2024-05-09 14:51:58 +08:00
response = client.put("/cached_put")
assert response.status_code == HTTPStatus.METHOD_NOT_ALLOWED
assert "X-FastAPI-Cache" not in response.headers
assert response.json() != {"value": 2}
def test_alternate_injected_namespace() -> None:
with TestClient(app) as client:
response = client.get("/namespaced_injection")
assert response.headers.get("X-FastAPI-Cache") == "MISS"
assert response.json() == {"__fastapi_cache_request": 42, "__fastapi_cache_response": 17}
def test_cache_control() -> None:
with TestClient(app) as client:
2024-05-09 14:51:58 +08:00
response = client.get("/cached_put")
assert response.json() == {"value": 1}
# HIT
2024-05-09 14:51:58 +08:00
response = client.get("/cached_put")
assert response.json() == {"value": 1}
# no-cache
2024-05-09 14:51:58 +08:00
response = client.get("/cached_put", headers={"Cache-Control": "no-cache"})
assert response.json() == {"value": 2}
2024-05-09 14:51:58 +08:00
response = client.get("/cached_put")
assert response.json() == {"value": 2}
# no-store
2024-05-09 14:51:58 +08:00
response = client.get("/cached_put", headers={"Cache-Control": "no-store"})
assert response.json() == {"value": 3}
2024-05-09 14:51:58 +08:00
response = client.get("/cached_put")
assert response.json() == {"value": 2}