-
-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
address issue-333
- Loading branch information
Showing
12 changed files
with
253 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,5 +122,6 @@ repos: | |
starlette, | ||
types-PyYAML, | ||
types-freezegun, | ||
types-redis, | ||
types-requests, | ||
] |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import random | ||
from typing import TYPE_CHECKING | ||
from uuid import uuid4 | ||
|
||
if TYPE_CHECKING: | ||
from starlite import Response | ||
|
||
|
||
async def slow_handler() -> dict: | ||
output = {} | ||
count = 0 | ||
while count < 1000: | ||
output[str(count)] = random.random() | ||
count += 1 | ||
return output | ||
|
||
|
||
def after_request_handler(response: "Response") -> "Response": | ||
response.headers["unique-identifier"] = str(uuid4()) | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import Any | ||
|
||
from starlite import CacheConfig, get | ||
from starlite.cache import SimpleCacheBackend | ||
from starlite.testing import create_test_client | ||
|
||
from . import after_request_handler, slow_handler | ||
|
||
|
||
def test_async_handling() -> None: | ||
class AsyncCacheBackend(SimpleCacheBackend): | ||
async def set(self, key: str, value: Any, expiration: int) -> Any: # type: ignore | ||
super().set(key=key, value=value, expiration=expiration) | ||
|
||
async def get(self, key: str) -> Any: | ||
return super().get(key=key) | ||
|
||
cache_config = CacheConfig(backend=AsyncCacheBackend()) | ||
|
||
with create_test_client( | ||
route_handlers=[get("/cached-async", cache=True)(slow_handler)], | ||
after_request=after_request_handler, | ||
cache_config=cache_config, | ||
) as client: | ||
first_response = client.get("/cached-async") | ||
first_response_identifier = first_response.headers["unique-identifier"] | ||
assert first_response_identifier | ||
second_response = client.get("/cached-async") | ||
assert second_response.headers["unique-identifier"] == first_response_identifier | ||
assert first_response.json() == second_response.json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from typing import Any | ||
|
||
import pytest | ||
import redis | ||
from pydantic import ValidationError | ||
|
||
from starlite import CacheConfig, Starlite | ||
from starlite.cache import CacheBackendProtocol | ||
|
||
|
||
def test_config_validation_scenario() -> None: | ||
class ProtocolBaseBackend(CacheBackendProtocol): | ||
def get(self, key: str) -> None: | ||
... | ||
|
||
def set(self, key: str, value: Any, expiration: int) -> None: | ||
... | ||
|
||
CacheConfig(backend=ProtocolBaseBackend) # type: ignore[arg-type] | ||
|
||
class NoneProtocolBasedBackend: | ||
def get(self, key: str) -> None: | ||
... | ||
|
||
def set(self, key: str, value: Any, expiration: int) -> None: | ||
... | ||
|
||
with pytest.raises(ValidationError): | ||
CacheConfig(backend=NoneProtocolBasedBackend) # type: ignore[arg-type] | ||
|
||
|
||
def test_config_validation_deep_copy() -> None: | ||
""" | ||
test fix for issue-333: https://github.com/starlite-api/starlite/issues/333 | ||
""" | ||
|
||
cache_config = CacheConfig(backend=redis.from_url("redis://localhost:6379/1")) | ||
Starlite(route_handlers=[], cache_config=cache_config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from time import sleep | ||
|
||
from starlite.cache import SimpleCacheBackend | ||
|
||
|
||
def test_simple_cache_backend() -> None: | ||
backend = SimpleCacheBackend() | ||
backend.set("test", "1", 0.1) # type: ignore | ||
assert backend.get("test") | ||
sleep(0.2) | ||
assert not backend.get("test") |
Binary file not shown.