Skip to content

Commit

Permalink
test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cofin committed Dec 21, 2024
1 parent bef56c9 commit 6b2968b
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions src/py/tests/test_inertia/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import Any, Dict

import pytest
from anyio.from_thread import start_blocking_portal
from litestar import Request, get
from litestar.exceptions import NotAuthorizedException
from litestar.middleware.session.server_side import ServerSideSessionConfig
Expand All @@ -21,6 +20,8 @@
from litestar_vite.inertia import InertiaHeaders, InertiaPlugin
from litestar_vite.inertia.response import (
DeferredProp,
InertiaBack,
InertiaExternalRedirect,
StaticProp,
is_lazy_prop,
is_or_contains_lazy_prop,
Expand Down Expand Up @@ -238,8 +239,8 @@ async def test_inertia_external_redirect(
template_config: TemplateConfig, # pyright: ignore[reportUnknownParameterType,reportMissingTypeArgument]
) -> None:
@get("/external", component="External")
async def handler(request: Request[Any, Any, Any]) -> Dict[str, Any]:
return {"thing": "value"}
async def handler(request: Request[Any, Any, Any]) -> InertiaExternalRedirect:
return InertiaExternalRedirect(request, "/external")

with create_test_client(
route_handlers=[handler],
Expand All @@ -263,8 +264,8 @@ async def test_inertia_back(
template_config: TemplateConfig, # pyright: ignore[reportUnknownParameterType,reportMissingTypeArgument]
) -> None:
@get("/back", component="Back")
async def handler(request: Request[Any, Any, Any]) -> Dict[str, Any]:
return {"thing": "value"}
async def handler(request: Request[Any, Any, Any]) -> InertiaBack:
return InertiaBack(request)

with create_test_client(
route_handlers=[handler],
Expand All @@ -284,28 +285,27 @@ async def handler(request: Request[Any, Any, Any]) -> Dict[str, Any]:

async def test_deferred_prop_render() -> None:
# Test rendering a callable
with start_blocking_portal() as _:

def simulated_expensive_sync_function() -> str:
sleep(0.5)
return "callable_result"
def simulated_expensive_sync_function() -> str:
sleep(0.5)
return "callable_result"

async def simulated_expensive_async_function() -> str:
await asyncio.sleep(0.5)
return "async_result"
async def simulated_expensive_async_function() -> str:
await asyncio.sleep(0.5)
return "async_result"

test_prop_1 = lazy("test_prop_1", simulated_expensive_sync_function)
assert test_prop_1.render() == "callable_result"
test_prop_1 = lazy("test_prop_1", simulated_expensive_sync_function)
assert test_prop_1.render() == "callable_result"

test_prop_2 = lazy("test_prop_2", simulated_expensive_async_function)
assert test_prop_2.render() == "async_result"
test_prop_2 = lazy("test_prop_2", simulated_expensive_async_function)
assert test_prop_2.render() == "async_result"

# Test rendering an async callable
async def async_callable_func() -> str:
return "async_result"
# Test rendering an async callable
async def async_callable_func() -> str:
return "async_result"

prop_async_callable = DeferredProp(key="async_callable", value=async_callable_func)
assert prop_async_callable.render() == "async_result"
prop_async_callable = DeferredProp(key="async_callable", value=async_callable_func)
assert prop_async_callable.render() == "async_result"


async def test_static_prop_render() -> None:
Expand Down Expand Up @@ -351,7 +351,7 @@ async def simulated_expensive_async_function() -> str:

async def test_should_render() -> None:
prop = lazy("test", "value")
assert should_render(prop) is True
assert should_render(prop) is False
assert should_render(prop, partial_data={"test"}) is True
assert should_render(prop, partial_data={"other"}) is False
assert should_render("string") is True
Expand Down Expand Up @@ -414,7 +414,7 @@ async def test_filter_deferred_props() -> None:
}


async def test_lazy_helper() -> None:
def test_lazy_helper() -> None:
prop = lazy("test", "value")
assert isinstance(prop, StaticProp)
assert prop.key == "test"
Expand Down Expand Up @@ -456,9 +456,8 @@ async def handler(request: Request[Any, Any, Any]) -> Dict[str, Any]:
response = client.get("/", headers={InertiaHeaders.ENABLED.value: "true"})
assert response.json()["props"]["content"] == {
"static": "value",
"deferred": "deferred_value",
"nested": {"deferred": "nested_deferred_value"},
"list": ["list_deferred_value"],
"nested": {},
"list": [],
}

# Partial data, only specified deferred props should be rendered
Expand Down

0 comments on commit 6b2968b

Please sign in to comment.