-
Notifications
You must be signed in to change notification settings - Fork 1.7k
added the missing tests (and verified they pass) #6170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1697
to
+1698
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fragile cleanup –
Suggested change
Or, restructure the test to use the 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i suggest setting
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call – that's cleaner. Here's how it would look:
Suggested change
The fixture ensures
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I can't provide this as an inline suggestion because:
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 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def test_app_state_manager(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 checksasync_value, leaving the regular-var assertions untested for the running-event-loop code path. Consider adding the same assertions for completeness and consistency: