|
@@ -60,6 +60,7 @@ from reflex.utils.exceptions import (
|
|
|
ReflexRuntimeError,
|
|
|
SetUndefinedStateVarError,
|
|
|
StateSerializationError,
|
|
|
+ UnretrievableVarValueError,
|
|
|
)
|
|
|
from reflex.utils.format import json_dumps
|
|
|
from reflex.vars.base import Var, computed_var
|
|
@@ -115,7 +116,7 @@ class TestState(BaseState):
|
|
|
# Set this class as not test one
|
|
|
__test__ = False
|
|
|
|
|
|
- num1: int
|
|
|
+ num1: rx.Field[int]
|
|
|
num2: float = 3.14
|
|
|
key: str
|
|
|
map_key: str = "a"
|
|
@@ -163,7 +164,7 @@ class ChildState(TestState):
|
|
|
"""A child state fixture."""
|
|
|
|
|
|
value: str
|
|
|
- count: int = 23
|
|
|
+ count: rx.Field[int] = rx.field(23)
|
|
|
|
|
|
def change_both(self, value: str, count: int):
|
|
|
"""Change both the value and count.
|
|
@@ -1663,7 +1664,7 @@ async def state_manager(request) -> AsyncGenerator[StateManager, None]:
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
-def substate_token(state_manager, token):
|
|
|
+def substate_token(state_manager, token) -> str:
|
|
|
"""A token + substate name for looking up in state manager.
|
|
|
|
|
|
Args:
|
|
@@ -3785,3 +3786,32 @@ async def test_upcast_event_handler_arg(handler, payload):
|
|
|
state = UpcastState()
|
|
|
async for update in state._process_event(handler, state, payload):
|
|
|
assert update.delta == {UpcastState.get_full_name(): {"passed": True}}
|
|
|
+
|
|
|
+
|
|
|
+@pytest.mark.asyncio
|
|
|
+async def test_get_var_value(state_manager: StateManager, substate_token: str):
|
|
|
+ """Test that get_var_value works correctly.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ state_manager: The state manager to use.
|
|
|
+ substate_token: Token for the substate used by state_manager.
|
|
|
+ """
|
|
|
+ state = await state_manager.get_state(substate_token)
|
|
|
+
|
|
|
+ # State Var from same state
|
|
|
+ assert await state.get_var_value(TestState.num1) == 0
|
|
|
+ state.num1 = 42
|
|
|
+ assert await state.get_var_value(TestState.num1) == 42
|
|
|
+
|
|
|
+ # State Var from another state
|
|
|
+ child_state = await state.get_state(ChildState)
|
|
|
+ assert await state.get_var_value(ChildState.count) == 23
|
|
|
+ child_state.count = 66
|
|
|
+ assert await state.get_var_value(ChildState.count) == 66
|
|
|
+
|
|
|
+ # LiteralVar with known value
|
|
|
+ assert await state.get_var_value(rx.Var.create([1, 2, 3])) == [1, 2, 3]
|
|
|
+
|
|
|
+ # Generic Var with no state
|
|
|
+ with pytest.raises(UnretrievableVarValueError):
|
|
|
+ await state.get_var_value(rx.Var("undefined"))
|