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_manager: StateManager, substate_token: BaseStateToken
)
| 4664 | |
| 4665 | @pytest.mark.asyncio |
| 4666 | async def test_get_var_value( |
| 4667 | state_manager: StateManager, substate_token: BaseStateToken |
| 4668 | ): |
| 4669 | """Test that get_var_value works correctly. |
| 4670 | |
| 4671 | Args: |
| 4672 | state_manager: The state manager to use. |
| 4673 | substate_token: Token for the substate used by state_manager. |
| 4674 | """ |
| 4675 | state = await state_manager.get_state(substate_token) |
| 4676 | |
| 4677 | # State Var from same state |
| 4678 | assert await state.get_var_value(TestState.num1) == 0 |
| 4679 | state.num1 = 42 |
| 4680 | assert await state.get_var_value(TestState.num1) == 42 |
| 4681 | |
| 4682 | # State Var from another state |
| 4683 | child_state = await state.get_state(ChildState) |
| 4684 | assert await state.get_var_value(ChildState.count) == 23 |
| 4685 | child_state.count = 66 |
| 4686 | assert await state.get_var_value(ChildState.count) == 66 |
| 4687 | |
| 4688 | # LiteralVar with known value |
| 4689 | assert await state.get_var_value(rx.Var.create([1, 2, 3])) == [1, 2, 3] |
| 4690 | |
| 4691 | # Generic Var with no state |
| 4692 | with pytest.raises(UnretrievableVarValueError): |
| 4693 | await state.get_var_value(rx.Var("undefined")) |
| 4694 | |
| 4695 | # ObjectVar |
| 4696 | assert await state.get_var_value(TestState.mapping) == { |
| 4697 | "a": [1, 2, 3], |
| 4698 | "b": [4, 5, 6], |
| 4699 | } |
| 4700 | |
| 4701 | # Regression for https://github.com/reflex-dev/reflex/issues/6629: a Var |
| 4702 | # operation / derived var (arithmetic, indexed or item access) must not |
| 4703 | # silently return the value of its first constituent field. Such vars have |
| 4704 | # no retrievable value, so raise instead of returning a plausible-but-wrong one. |
| 4705 | with pytest.raises(UnretrievableVarValueError): |
| 4706 | await state.get_var_value(TestState.num1 + TestState.num2) |
| 4707 | with pytest.raises(UnretrievableVarValueError): |
| 4708 | # array[0] is a Var operation at runtime, though statically typed as the element. |
| 4709 | await state.get_var_value(TestState.array[0]) # pyright: ignore[reportArgumentType] |
| 4710 | with pytest.raises(UnretrievableVarValueError): |
| 4711 | await state.get_var_value(TestState.mapping["a"]) |
| 4712 | |
| 4713 | # Computed vars are derived but state-bound, so they remain resolvable. |
| 4714 | assert await state.get_var_value(TestState.sum) == pytest.approx(42 + 3.15) |
| 4715 | |
| 4716 | |
| 4717 | @pytest.mark.asyncio |
nothing calls this directly
no test coverage detected