added the missing tests (and verified they pass)#6170
added the missing tests (and verified they pass)#6170patrizzzz wants to merge 1 commit intoreflex-dev:mainfrom
Conversation
Greptile SummaryThis PR replaces two placeholder Key observations:
Confidence Score: 4/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant T1 as test_no_loop
participant T2 as test_running_loop
participant T3 as test_optional_endpoints
participant CS as compile_state
participant RD as _resolve_delta
participant App as App
participant UE as _add_optional_endpoints
T1->>CS: compile_state(CompileStateState)
CS->>CS: get_running_loop raises RuntimeError
CS->>RD: asyncio.run(_resolve_delta)
RD-->>CS: resolved dict with async_value=resolved
CS-->>T1: sorted compiled dict
T2->>CS: compile_state(CompileStateState)
CS->>CS: get_running_loop returns loop
CS->>RD: ThreadPoolExecutor submits asyncio.run(_resolve_delta)
RD-->>CS: resolved dict with async_value=resolved
CS-->>T2: sorted compiled dict
T3->>App: App() inside chdir(tmp_path)
App-->>T3: app with _api set
T3->>UE: Upload.is_used=True then _add_optional_endpoints
UE->>UE: add_route POST /_upload
UE->>UE: mount /_upload as uploaded_files
UE-->>T3: routes registered
T3->>T3: assert Route and Mount present
Last reviewed commit: e9e97c8 |
| ) | ||
| Upload.is_used = False |
There was a problem hiding this comment.
Fragile cleanup – Upload.is_used not reset on test failure
Upload.is_used = False is placed after the with chdir(tmp_path): block without a try/finally guard. If either assert any(...) call on lines 1688–1696 raises an AssertionError, the cleanup line never executes and Upload.is_used is left as True for subsequent tests. Wrap the assertions in try/finally or, preferably, use the existing monkeypatch fixture for test-scoped teardown:
| ) | |
| Upload.is_used = False | |
| finally: | |
| Upload.is_used = False |
Or, restructure the test to use the monkeypatch fixture:
def test_app_with_optional_endpoints(tmp_path: Path, monkeypatch):
from reflex.components.core.upload import Upload
from starlette.routing import Mount, Route
monkeypatch.setattr(Upload, "is_used", False)
with chdir(tmp_path):
app = App()
monkeypatch.setattr(Upload, "is_used", True)
app._add_optional_endpoints()
...Using monkeypatch guarantees restoration to the original value regardless of how the test exits.
There was a problem hiding this comment.
i suggest setting Upload.is_used with monkeypatch inside a fixture that this test depends on, that way the clean up and test logic are cleanly separated
There was a problem hiding this comment.
Good call – that's cleaner. Here's how it would look:
| ) | |
| Upload.is_used = False | |
| @pytest.fixture | |
| def upload_component(monkeypatch): | |
| from reflex.components.core.upload import Upload | |
| monkeypatch.setattr(Upload, "is_used", False) | |
| yield Upload | |
| def test_app_with_optional_endpoints(tmp_path: Path, upload_component): | |
| from starlette.routing import Mount, Route | |
| with chdir(tmp_path): | |
| app = App() | |
| upload_component.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 | |
| ) |
The fixture ensures Upload.is_used is False initially and handles cleanup automatically, while the test logic stays focused on the actual behavior being tested.
There was a problem hiding this comment.
Actually, I can't provide this as an inline suggestion because:
- The fixture needs to be placed before the test function
- The comment is only on lines 1697-1698 (the function signature)
- Inline suggestions only modify the exact lines the comment is attached to
Here's the pattern you should use:
@pytest.fixture
def upload_enabled(monkeypatch):
"""Fixture that enables Upload and cleans up afterward."""
from reflex.components.core.upload import Upload
monkeypatch.setattr(Upload, "is_used", True)
yield
monkeypatch.setattr(Upload, "is_used", False)
def test_app_with_optional_endpoints(tmp_path: Path, upload_enabled):
from reflex.components.core.upload import Upload
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
)Place the upload_enabled fixture above the test function. The fixture handles both setup and teardown automatically, keeping the test logic clean.
| @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" |
There was a problem hiding this comment.
Missing assertions for regular state vars in async-loop test
The synchronous counterpart (test_compile_state_resolves_async_computed_vars_without_event_loop) asserts that both regular state vars (a, b) and the async computed var are resolved correctly. This test only checks async_value, leaving the regular-var assertions untested for the running-event-loop code path. Consider adding the same assertions for completeness and consistency:
| @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" | |
| @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"a{FIELD_MARKER}"] == 1 | |
| assert values[f"b{FIELD_MARKER}"] == 2 | |
| assert values[f"async_value{FIELD_MARKER}"] == "resolved" |
masenf
left a comment
There was a problem hiding this comment.
thanks for adding the test cases
| ) | ||
| Upload.is_used = False |
There was a problem hiding this comment.
i suggest setting Upload.is_used with monkeypatch inside a fixture that this test depends on, that way the clean up and test logic are cleanly separated
All Submissions:
Type of change
Please delete options that are not relevant.
New Feature Submission:
Changes To Core Features:
After these steps, you're ready to open a pull request.