From e9e97c8cfcf211b293a7103ff23cabae3cb1d960 Mon Sep 17 00:00:00 2001 From: patrizzzz Date: Wed, 11 Mar 2026 23:57:16 +0800 Subject: [PATCH] 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():