From e9e97c8cfcf211b293a7103ff23cabae3cb1d960 Mon Sep 17 00:00:00 2001 From: patrizzzz Date: Wed, 11 Mar 2026 23:57:16 +0800 Subject: [PATCH 1/3] added the missing tests (and verified they pass) --- tests/units/compiler/test_compiler_utils.py | 42 ++++++++++++++++++--- tests/units/test_app.py | 29 +++++++++++--- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/tests/units/compiler/test_compiler_utils.py b/tests/units/compiler/test_compiler_utils.py index d94c779af67..383b0eab58f 100644 --- a/tests/units/compiler/test_compiler_utils.py +++ b/tests/units/compiler/test_compiler_utils.py @@ -1,7 +1,39 @@ -def TestState(State): - pass +from __future__ import annotations +import asyncio -def test_compile_state(): - # TODO: Implement test for compile_state function. - pass +import pytest + +from reflex.compiler.utils import compile_state +from reflex.constants.state import FIELD_MARKER +from reflex.state import State +from reflex.vars.base import computed_var + + +class CompileStateState(State): + a: int = 1 + b: int = 2 + + @computed_var + async def async_value(self) -> str: + await asyncio.sleep(0) + return "resolved" + + +def _get_state_values(compiled: dict, state: type[State]) -> dict: + return compiled[state.get_full_name()] + + +def test_compile_state_resolves_async_computed_vars_without_event_loop(): + compiled = compile_state(CompileStateState) + values = _get_state_values(compiled, CompileStateState) + assert values[f"a{FIELD_MARKER}"] == 1 + assert values[f"b{FIELD_MARKER}"] == 2 + assert values[f"async_value{FIELD_MARKER}"] == "resolved" + + +@pytest.mark.asyncio +async def test_compile_state_resolves_async_computed_vars_with_running_event_loop(): + compiled = compile_state(CompileStateState) + values = _get_state_values(compiled, CompileStateState) + assert values[f"async_value{FIELD_MARKER}"] == "resolved" diff --git a/tests/units/test_app.py b/tests/units/test_app.py index 6efd006f1fa..6cea4a809d5 100644 --- a/tests/units/test_app.py +++ b/tests/units/test_app.py @@ -1670,13 +1670,32 @@ def test_call_app(): assert isinstance(api, Starlette) -def test_app_with_optional_endpoints(): +def test_app_with_optional_endpoints(tmp_path: Path): from reflex.components.core.upload import Upload - app = App() - Upload.is_used = True - app._add_optional_endpoints() - # TODO: verify the availability of the endpoints in app.api + from starlette.routing import Mount, Route + + Upload.is_used = False + with chdir(tmp_path): + app = App() + Upload.is_used = True + app._add_optional_endpoints() + + assert app._api is not None + upload_path = str(constants.Endpoint.UPLOAD) + routes = list(app._api.routes) + + assert any( + isinstance(r, Route) + and r.path == upload_path + and "POST" in getattr(r, "methods", set()) + for r in routes + ) + assert any( + isinstance(r, Mount) and r.path == upload_path and r.name == "uploaded_files" + for r in routes + ) + Upload.is_used = False def test_app_state_manager(): From d37dd5cd445dd40893bd978099760803109caa0a Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Thu, 7 May 2026 13:14:21 -0700 Subject: [PATCH 2/3] Apply suggestion from @greptile-apps[bot] Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> --- tests/units/compiler/test_compiler_utils.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/units/compiler/test_compiler_utils.py b/tests/units/compiler/test_compiler_utils.py index 383b0eab58f..e448fbb0399 100644 --- a/tests/units/compiler/test_compiler_utils.py +++ b/tests/units/compiler/test_compiler_utils.py @@ -36,4 +36,6 @@ def test_compile_state_resolves_async_computed_vars_without_event_loop(): async def test_compile_state_resolves_async_computed_vars_with_running_event_loop(): compiled = compile_state(CompileStateState) values = _get_state_values(compiled, CompileStateState) + assert values[f"a{FIELD_MARKER}"] == 1 + assert values[f"b{FIELD_MARKER}"] == 2 assert values[f"async_value{FIELD_MARKER}"] == "resolved" From f34e74180e3f7ddd9ca485d08b7c2547c6462125 Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Thu, 7 May 2026 15:14:51 -0700 Subject: [PATCH 3/3] fixing pre-commit issues --- tests/units/compiler/test_compiler_utils.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/units/compiler/test_compiler_utils.py b/tests/units/compiler/test_compiler_utils.py index e448fbb0399..db7eff1a495 100644 --- a/tests/units/compiler/test_compiler_utils.py +++ b/tests/units/compiler/test_compiler_utils.py @@ -11,11 +11,18 @@ class CompileStateState(State): + """State fixture exercising async computed vars during compile_state.""" + a: int = 1 b: int = 2 @computed_var async def async_value(self) -> str: + """Return a resolved value after yielding to the event loop. + + Returns: + The resolved string value. + """ await asyncio.sleep(0) return "resolved" @@ -34,6 +41,8 @@ def test_compile_state_resolves_async_computed_vars_without_event_loop(): @pytest.mark.asyncio async def test_compile_state_resolves_async_computed_vars_with_running_event_loop(): + assert asyncio.get_running_loop() is not None + await asyncio.sleep(0) compiled = compile_state(CompileStateState) values = _get_state_values(compiled, CompileStateState) assert values[f"a{FIELD_MARKER}"] == 1