Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 48 additions & 5 deletions tests/units/compiler/test_compiler_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,50 @@
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):
"""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"


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():
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
assert values[f"b{FIELD_MARKER}"] == 2
assert values[f"async_value{FIELD_MARKER}"] == "resolved"
Comment thread
masenf marked this conversation as resolved.
37 changes: 32 additions & 5 deletions tests/units/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2388,13 +2388,40 @@ def test_call_app():
assert isinstance(api, Starlette)


def test_app_with_optional_endpoints():
@pytest.fixture
def upload_enabled(monkeypatch):
"""Fixture that enables Upload and cleans up afterward."""
from reflex_components_core.core.upload import Upload

app = App()
Upload.is_used = True
app._add_optional_endpoints()
# TODO: verify the availability of the endpoints in app.api
monkeypatch.setattr(Upload, "is_used", True)
yield
monkeypatch.setattr(Upload, "is_used", False)


@pytest.mark.usefixtures("upload_enabled")
def test_app_with_optional_endpoints(tmp_path: Path):
from starlette.routing import Mount, Route

with chdir(tmp_path):
app = App()
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
)


def test_app_state_manager():
Expand Down
Loading