2022-09-10 20:06:37 +08:00
|
|
|
import time
|
2022-10-22 21:12:04 +04:00
|
|
|
from typing import Generator
|
2022-09-10 20:06:37 +08:00
|
|
|
|
|
|
|
|
import pendulum
|
2022-10-22 21:12:04 +04:00
|
|
|
import pytest
|
2022-09-10 20:06:37 +08:00
|
|
|
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
|
2022-10-22 21:12:04 +04:00
|
|
|
from fastapi_cache.backends.inmemory import InMemoryBackend
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
|
def init_cache() -> Generator:
|
|
|
|
|
FastAPICache.init(InMemoryBackend())
|
|
|
|
|
yield
|
|
|
|
|
FastAPICache.reset()
|
2022-09-10 20:06:37 +08:00
|
|
|
|
|
|
|
|
|
2022-10-22 21:12:04 +04:00
|
|
|
def test_datetime() -> None:
|
2022-09-10 20:06:37 +08:00
|
|
|
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)
|
2022-10-14 21:59:33 +02:00
|
|
|
|
2022-10-22 21:12:04 +04: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 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
|
|
|
|
|
|
2022-10-22 21:12:04 +04: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})
|
2023-02-15 10:35:41 +09:00
|
|
|
assert response.json() == {"name": name}
|