MCPcopy
hub / github.com/reflex-dev/reflex / test_get_state

Function test_get_state

tests/units/test_state.py:3489–3600  ·  view source on GitHub ↗

Test that a get_state populates the top level state and delta calculation is correct. Args: token: A token. attached_mock_event_context: An event context with a state manager that has a TestState instance corresponding to the token.

(token: str, attached_mock_event_context: EventContext)

Source from the content-addressed store, hash-verified

3487
3488@pytest.mark.asyncio
3489async def test_get_state(token: str, attached_mock_event_context: EventContext):
3490 """Test that a get_state populates the top level state and delta calculation is correct.
3491
3492 Args:
3493 token: A token.
3494 attached_mock_event_context: An event context with a state manager that has a TestState instance corresponding to the token.
3495 """
3496 state_manager = attached_mock_event_context.state_manager
3497
3498 # Get instance of ChildState2.
3499 test_state = await state_manager.get_state(
3500 BaseStateToken(ident=token, cls=ChildState2)
3501 )
3502 assert isinstance(test_state, TestState)
3503 if isinstance(state_manager, (StateManagerMemory, StateManagerDisk)):
3504 # All substates are available
3505 assert tuple(sorted(test_state.substates)) == (
3506 ChildState.get_name(),
3507 ChildState2.get_name(),
3508 ChildState3.get_name(),
3509 )
3510 else:
3511 # Sibling states are only populated if they have computed vars
3512 assert tuple(sorted(test_state.substates)) == (
3513 ChildState2.get_name(),
3514 ChildState3.get_name(),
3515 )
3516
3517 # Because ChildState3 has a computed var, it is always dirty, and always populated.
3518 grandchild_state3 = test_state.substates[ChildState3.get_name()].substates[
3519 GrandchildState3.get_name()
3520 ]
3521 assert isinstance(grandchild_state3, GrandchildState3)
3522 assert grandchild_state3.computed == ""
3523
3524 # Get the child_state2 directly.
3525 child_state2_direct = test_state.get_substate([ChildState2.get_name()])
3526 child_state2_get_state = await test_state.get_state(ChildState2)
3527 # These should be the same object.
3528 assert child_state2_direct is child_state2_get_state
3529
3530 # Get arbitrary GrandchildState.
3531 grandchild_state = await child_state2_get_state.get_state(GrandchildState)
3532 assert isinstance(grandchild_state, GrandchildState)
3533
3534 # Now the original root should have all substates populated.
3535 assert tuple(sorted(test_state.substates)) == (
3536 ChildState.get_name(),
3537 ChildState2.get_name(),
3538 ChildState3.get_name(),
3539 )
3540
3541 # ChildState should be retrievable
3542 child_state_direct = test_state.get_substate([ChildState.get_name()])
3543 child_state_get_state = await test_state.get_state(ChildState)
3544 # These should be the same object.
3545 assert child_state_direct is child_state_get_state
3546

Callers

nothing calls this directly

Calls 7

BaseStateTokenClass · 0.90
get_nameMethod · 0.80
get_deltaMethod · 0.80
get_full_nameMethod · 0.80
get_stateMethod · 0.45
get_substateMethod · 0.45
_cleanMethod · 0.45

Tested by

no test coverage detected