mirror of
https://github.com/long2ice/fastapi-cache.git
synced 2026-03-24 20:47:54 +00:00
feat: fix tests and add FastAPICache init in tests.
This commit is contained in:
@@ -34,6 +34,16 @@ class FastAPICache:
|
|||||||
cls._key_builder = key_builder
|
cls._key_builder = key_builder
|
||||||
cls._enable = enable
|
cls._enable = enable
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def reset(cls) -> None:
|
||||||
|
cls._init = False
|
||||||
|
cls._backend = None
|
||||||
|
cls._prefix = None
|
||||||
|
cls._expire = None
|
||||||
|
cls._coder = None
|
||||||
|
cls._key_builder = None
|
||||||
|
cls._enable = True
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_backend(cls) -> Backend:
|
def get_backend(cls) -> Backend:
|
||||||
assert cls._backend, "You must call init first!" # nosec: B101
|
assert cls._backend, "You must call init first!" # nosec: B101
|
||||||
@@ -41,7 +51,7 @@ class FastAPICache:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_prefix(cls) -> str:
|
def get_prefix(cls) -> str:
|
||||||
assert cls._prefix, "You must call init first!" # nosec: B101
|
assert cls._prefix is not None, "You must call init first!" # nosec: B101
|
||||||
return cls._prefix
|
return cls._prefix
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
@@ -64,6 +74,6 @@ class FastAPICache:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def clear(cls, namespace: Optional[str] = None, key: Optional[str] = None) -> int:
|
async def clear(cls, namespace: Optional[str] = None, key: Optional[str] = None) -> int:
|
||||||
assert cls._backend and cls._prefix, "You must call init first!" # nosec: B101
|
assert cls._backend and cls._prefix is not None, "You must call init first!" # nosec: B101
|
||||||
namespace = cls._prefix + (":" + namespace if namespace else "")
|
namespace = cls._prefix + (":" + namespace if namespace else "")
|
||||||
return await cls._backend.clear(namespace, key)
|
return await cls._backend.clear(namespace, key)
|
||||||
|
|||||||
@@ -1,13 +1,24 @@
|
|||||||
import time
|
import time
|
||||||
|
from typing import Generator
|
||||||
|
|
||||||
import pendulum
|
import pendulum
|
||||||
|
import pytest
|
||||||
|
|
||||||
from fastapi_cache import FastAPICache
|
from fastapi_cache import FastAPICache
|
||||||
from starlette.testclient import TestClient
|
from starlette.testclient import TestClient
|
||||||
|
|
||||||
from examples.in_memory.main import app
|
from examples.in_memory.main import app
|
||||||
|
from fastapi_cache.backends.inmemory import InMemoryBackend
|
||||||
|
|
||||||
|
|
||||||
def test_datetime():
|
@pytest.fixture(autouse=True)
|
||||||
|
def init_cache() -> Generator:
|
||||||
|
FastAPICache.init(InMemoryBackend())
|
||||||
|
yield
|
||||||
|
FastAPICache.reset()
|
||||||
|
|
||||||
|
|
||||||
|
def test_datetime() -> None:
|
||||||
with TestClient(app) as client:
|
with TestClient(app) as client:
|
||||||
response = client.get("/datetime")
|
response = client.get("/datetime")
|
||||||
now = response.json().get("now")
|
now = response.json().get("now")
|
||||||
@@ -23,7 +34,8 @@ def test_datetime():
|
|||||||
assert now != now_
|
assert now != now_
|
||||||
assert now == pendulum.now().replace(microsecond=0)
|
assert now == pendulum.now().replace(microsecond=0)
|
||||||
|
|
||||||
def test_date():
|
|
||||||
|
def test_date() -> None:
|
||||||
"""Test path function without request or response arguments."""
|
"""Test path function without request or response arguments."""
|
||||||
with TestClient(app) as client:
|
with TestClient(app) as client:
|
||||||
|
|
||||||
@@ -40,7 +52,8 @@ def test_date():
|
|||||||
assert pendulum.parse(response.json()) == pendulum.today()
|
assert pendulum.parse(response.json()) == pendulum.today()
|
||||||
FastAPICache._enable = True
|
FastAPICache._enable = True
|
||||||
|
|
||||||
def test_sync():
|
|
||||||
|
def test_sync() -> None:
|
||||||
"""Ensure that sync function support works."""
|
"""Ensure that sync function support works."""
|
||||||
with TestClient(app) as client:
|
with TestClient(app) as client:
|
||||||
response = client.get("/sync-me")
|
response = client.get("/sync-me")
|
||||||
|
|||||||
Reference in New Issue
Block a user