|
@@ -217,7 +217,7 @@ def child_state(test_state) -> ChildState:
|
|
Returns:
|
|
Returns:
|
|
A test child state.
|
|
A test child state.
|
|
"""
|
|
"""
|
|
- child_state = test_state.get_substate(["child_state"])
|
|
|
|
|
|
+ child_state = test_state.get_substate([ChildState.get_name()])
|
|
assert child_state is not None
|
|
assert child_state is not None
|
|
return child_state
|
|
return child_state
|
|
|
|
|
|
@@ -232,7 +232,7 @@ def child_state2(test_state) -> ChildState2:
|
|
Returns:
|
|
Returns:
|
|
A second test child state.
|
|
A second test child state.
|
|
"""
|
|
"""
|
|
- child_state2 = test_state.get_substate(["child_state2"])
|
|
|
|
|
|
+ child_state2 = test_state.get_substate([ChildState2.get_name()])
|
|
assert child_state2 is not None
|
|
assert child_state2 is not None
|
|
return child_state2
|
|
return child_state2
|
|
|
|
|
|
@@ -247,7 +247,7 @@ def grandchild_state(child_state) -> GrandchildState:
|
|
Returns:
|
|
Returns:
|
|
A test state.
|
|
A test state.
|
|
"""
|
|
"""
|
|
- grandchild_state = child_state.get_substate(["grandchild_state"])
|
|
|
|
|
|
+ grandchild_state = child_state.get_substate([GrandchildState.get_name()])
|
|
assert grandchild_state is not None
|
|
assert grandchild_state is not None
|
|
return grandchild_state
|
|
return grandchild_state
|
|
|
|
|
|
@@ -357,20 +357,20 @@ def test_computed_vars(test_state):
|
|
assert test_state.upper == "HELLO WORLD"
|
|
assert test_state.upper == "HELLO WORLD"
|
|
|
|
|
|
|
|
|
|
-def test_dict(test_state):
|
|
|
|
|
|
+def test_dict(test_state: TestState):
|
|
"""Test that the dict representation of a state is correct.
|
|
"""Test that the dict representation of a state is correct.
|
|
|
|
|
|
Args:
|
|
Args:
|
|
test_state: A state.
|
|
test_state: A state.
|
|
"""
|
|
"""
|
|
substates = {
|
|
substates = {
|
|
- "test_state",
|
|
|
|
- "test_state.child_state",
|
|
|
|
- "test_state.child_state.grandchild_state",
|
|
|
|
- "test_state.child_state2",
|
|
|
|
- "test_state.child_state2.grandchild_state2",
|
|
|
|
- "test_state.child_state3",
|
|
|
|
- "test_state.child_state3.grandchild_state3",
|
|
|
|
|
|
+ test_state.get_full_name(),
|
|
|
|
+ ChildState.get_full_name(),
|
|
|
|
+ GrandchildState.get_full_name(),
|
|
|
|
+ ChildState2.get_full_name(),
|
|
|
|
+ GrandchildState2.get_full_name(),
|
|
|
|
+ ChildState3.get_full_name(),
|
|
|
|
+ GrandchildState3.get_full_name(),
|
|
}
|
|
}
|
|
test_state_dict = test_state.dict()
|
|
test_state_dict = test_state.dict()
|
|
assert set(test_state_dict) == substates
|
|
assert set(test_state_dict) == substates
|
|
@@ -394,22 +394,30 @@ def test_default_setters(test_state):
|
|
def test_class_indexing_with_vars():
|
|
def test_class_indexing_with_vars():
|
|
"""Test that we can index into a state var with another var."""
|
|
"""Test that we can index into a state var with another var."""
|
|
prop = TestState.array[TestState.num1]
|
|
prop = TestState.array[TestState.num1]
|
|
- assert str(prop) == "{test_state.array.at(test_state.num1)}"
|
|
|
|
|
|
+ assert (
|
|
|
|
+ str(prop) == f"{{{TestState.get_name()}.array.at({TestState.get_name()}.num1)}}"
|
|
|
|
+ )
|
|
|
|
|
|
prop = TestState.mapping["a"][TestState.num1]
|
|
prop = TestState.mapping["a"][TestState.num1]
|
|
- assert str(prop) == '{test_state.mapping["a"].at(test_state.num1)}'
|
|
|
|
|
|
+ assert (
|
|
|
|
+ str(prop)
|
|
|
|
+ == f'{{{TestState.get_name()}.mapping["a"].at({TestState.get_name()}.num1)}}'
|
|
|
|
+ )
|
|
|
|
|
|
prop = TestState.mapping[TestState.map_key]
|
|
prop = TestState.mapping[TestState.map_key]
|
|
- assert str(prop) == "{test_state.mapping[test_state.map_key]}"
|
|
|
|
|
|
+ assert (
|
|
|
|
+ str(prop)
|
|
|
|
+ == f"{{{TestState.get_name()}.mapping[{TestState.get_name()}.map_key]}}"
|
|
|
|
+ )
|
|
|
|
|
|
|
|
|
|
def test_class_attributes():
|
|
def test_class_attributes():
|
|
"""Test that we can get class attributes."""
|
|
"""Test that we can get class attributes."""
|
|
prop = TestState.obj.prop1
|
|
prop = TestState.obj.prop1
|
|
- assert str(prop) == "{test_state.obj.prop1}"
|
|
|
|
|
|
+ assert str(prop) == f"{{{TestState.get_name()}.obj.prop1}}"
|
|
|
|
|
|
prop = TestState.complex[1].prop1
|
|
prop = TestState.complex[1].prop1
|
|
- assert str(prop) == "{test_state.complex[1].prop1}"
|
|
|
|
|
|
+ assert str(prop) == f"{{{TestState.get_name()}.complex[1].prop1}}"
|
|
|
|
|
|
|
|
|
|
def test_get_parent_state():
|
|
def test_get_parent_state():
|
|
@@ -431,27 +439,40 @@ def test_get_substates():
|
|
|
|
|
|
def test_get_name():
|
|
def test_get_name():
|
|
"""Test getting the name of a state."""
|
|
"""Test getting the name of a state."""
|
|
- assert TestState.get_name() == "test_state"
|
|
|
|
- assert ChildState.get_name() == "child_state"
|
|
|
|
- assert ChildState2.get_name() == "child_state2"
|
|
|
|
- assert GrandchildState.get_name() == "grandchild_state"
|
|
|
|
|
|
+ assert TestState.get_name() == "tests___test_state____test_state"
|
|
|
|
+ assert ChildState.get_name() == "tests___test_state____child_state"
|
|
|
|
+ assert ChildState2.get_name() == "tests___test_state____child_state2"
|
|
|
|
+ assert GrandchildState.get_name() == "tests___test_state____grandchild_state"
|
|
|
|
|
|
|
|
|
|
def test_get_full_name():
|
|
def test_get_full_name():
|
|
"""Test getting the full name."""
|
|
"""Test getting the full name."""
|
|
- assert TestState.get_full_name() == "test_state"
|
|
|
|
- assert ChildState.get_full_name() == "test_state.child_state"
|
|
|
|
- assert ChildState2.get_full_name() == "test_state.child_state2"
|
|
|
|
- assert GrandchildState.get_full_name() == "test_state.child_state.grandchild_state"
|
|
|
|
|
|
+ assert TestState.get_full_name() == "tests___test_state____test_state"
|
|
|
|
+ assert (
|
|
|
|
+ ChildState.get_full_name()
|
|
|
|
+ == "tests___test_state____test_state.tests___test_state____child_state"
|
|
|
|
+ )
|
|
|
|
+ assert (
|
|
|
|
+ ChildState2.get_full_name()
|
|
|
|
+ == "tests___test_state____test_state.tests___test_state____child_state2"
|
|
|
|
+ )
|
|
|
|
+ assert (
|
|
|
|
+ GrandchildState.get_full_name()
|
|
|
|
+ == "tests___test_state____test_state.tests___test_state____child_state.tests___test_state____grandchild_state"
|
|
|
|
+ )
|
|
|
|
|
|
|
|
|
|
def test_get_class_substate():
|
|
def test_get_class_substate():
|
|
"""Test getting the substate of a class."""
|
|
"""Test getting the substate of a class."""
|
|
- assert TestState.get_class_substate(("child_state",)) == ChildState
|
|
|
|
- assert TestState.get_class_substate(("child_state2",)) == ChildState2
|
|
|
|
- assert ChildState.get_class_substate(("grandchild_state",)) == GrandchildState
|
|
|
|
|
|
+ assert TestState.get_class_substate((ChildState.get_name(),)) == ChildState
|
|
|
|
+ assert TestState.get_class_substate((ChildState2.get_name(),)) == ChildState2
|
|
|
|
+ assert (
|
|
|
|
+ ChildState.get_class_substate((GrandchildState.get_name(),)) == GrandchildState
|
|
|
|
+ )
|
|
assert (
|
|
assert (
|
|
- TestState.get_class_substate(("child_state", "grandchild_state"))
|
|
|
|
|
|
+ TestState.get_class_substate(
|
|
|
|
+ (ChildState.get_name(), GrandchildState.get_name())
|
|
|
|
+ )
|
|
== GrandchildState
|
|
== GrandchildState
|
|
)
|
|
)
|
|
with pytest.raises(ValueError):
|
|
with pytest.raises(ValueError):
|
|
@@ -459,7 +480,7 @@ def test_get_class_substate():
|
|
with pytest.raises(ValueError):
|
|
with pytest.raises(ValueError):
|
|
TestState.get_class_substate(
|
|
TestState.get_class_substate(
|
|
(
|
|
(
|
|
- "child_state",
|
|
|
|
|
|
+ ChildState.get_name(),
|
|
"invalid_child",
|
|
"invalid_child",
|
|
)
|
|
)
|
|
)
|
|
)
|
|
@@ -471,13 +492,15 @@ def test_get_class_var():
|
|
assert TestState.get_class_var(("num2",)).equals(TestState.num2)
|
|
assert TestState.get_class_var(("num2",)).equals(TestState.num2)
|
|
assert ChildState.get_class_var(("value",)).equals(ChildState.value)
|
|
assert ChildState.get_class_var(("value",)).equals(ChildState.value)
|
|
assert GrandchildState.get_class_var(("value2",)).equals(GrandchildState.value2)
|
|
assert GrandchildState.get_class_var(("value2",)).equals(GrandchildState.value2)
|
|
- assert TestState.get_class_var(("child_state", "value")).equals(ChildState.value)
|
|
|
|
|
|
+ assert TestState.get_class_var((ChildState.get_name(), "value")).equals(
|
|
|
|
+ ChildState.value
|
|
|
|
+ )
|
|
assert TestState.get_class_var(
|
|
assert TestState.get_class_var(
|
|
- ("child_state", "grandchild_state", "value2")
|
|
|
|
|
|
+ (ChildState.get_name(), GrandchildState.get_name(), "value2")
|
|
).equals(
|
|
).equals(
|
|
GrandchildState.value2,
|
|
GrandchildState.value2,
|
|
)
|
|
)
|
|
- assert ChildState.get_class_var(("grandchild_state", "value2")).equals(
|
|
|
|
|
|
+ assert ChildState.get_class_var((GrandchildState.get_name(), "value2")).equals(
|
|
GrandchildState.value2,
|
|
GrandchildState.value2,
|
|
)
|
|
)
|
|
with pytest.raises(ValueError):
|
|
with pytest.raises(ValueError):
|
|
@@ -485,7 +508,7 @@ def test_get_class_var():
|
|
with pytest.raises(ValueError):
|
|
with pytest.raises(ValueError):
|
|
TestState.get_class_var(
|
|
TestState.get_class_var(
|
|
(
|
|
(
|
|
- "child_state",
|
|
|
|
|
|
+ ChildState.get_name(),
|
|
"invalid_var",
|
|
"invalid_var",
|
|
)
|
|
)
|
|
)
|
|
)
|
|
@@ -513,11 +536,15 @@ def test_set_parent_and_substates(test_state, child_state, grandchild_state):
|
|
grandchild_state: A grandchild state.
|
|
grandchild_state: A grandchild state.
|
|
"""
|
|
"""
|
|
assert len(test_state.substates) == 3
|
|
assert len(test_state.substates) == 3
|
|
- assert set(test_state.substates) == {"child_state", "child_state2", "child_state3"}
|
|
|
|
|
|
+ assert set(test_state.substates) == {
|
|
|
|
+ ChildState.get_name(),
|
|
|
|
+ ChildState2.get_name(),
|
|
|
|
+ ChildState3.get_name(),
|
|
|
|
+ }
|
|
|
|
|
|
assert child_state.parent_state == test_state
|
|
assert child_state.parent_state == test_state
|
|
assert len(child_state.substates) == 1
|
|
assert len(child_state.substates) == 1
|
|
- assert set(child_state.substates) == {"grandchild_state"}
|
|
|
|
|
|
+ assert set(child_state.substates) == {GrandchildState.get_name()}
|
|
|
|
|
|
assert grandchild_state.parent_state == child_state
|
|
assert grandchild_state.parent_state == child_state
|
|
assert len(grandchild_state.substates) == 0
|
|
assert len(grandchild_state.substates) == 0
|
|
@@ -584,18 +611,21 @@ def test_get_substate(test_state, child_state, child_state2, grandchild_state):
|
|
child_state2: A child state.
|
|
child_state2: A child state.
|
|
grandchild_state: A grandchild state.
|
|
grandchild_state: A grandchild state.
|
|
"""
|
|
"""
|
|
- assert test_state.get_substate(("child_state",)) == child_state
|
|
|
|
- assert test_state.get_substate(("child_state2",)) == child_state2
|
|
|
|
|
|
+ assert test_state.get_substate((ChildState.get_name(),)) == child_state
|
|
|
|
+ assert test_state.get_substate((ChildState2.get_name(),)) == child_state2
|
|
assert (
|
|
assert (
|
|
- test_state.get_substate(("child_state", "grandchild_state")) == grandchild_state
|
|
|
|
|
|
+ test_state.get_substate((ChildState.get_name(), GrandchildState.get_name()))
|
|
|
|
+ == grandchild_state
|
|
)
|
|
)
|
|
- assert child_state.get_substate(("grandchild_state",)) == grandchild_state
|
|
|
|
|
|
+ assert child_state.get_substate((GrandchildState.get_name(),)) == grandchild_state
|
|
with pytest.raises(ValueError):
|
|
with pytest.raises(ValueError):
|
|
test_state.get_substate(("invalid",))
|
|
test_state.get_substate(("invalid",))
|
|
with pytest.raises(ValueError):
|
|
with pytest.raises(ValueError):
|
|
- test_state.get_substate(("child_state", "invalid"))
|
|
|
|
|
|
+ test_state.get_substate((ChildState.get_name(), "invalid"))
|
|
with pytest.raises(ValueError):
|
|
with pytest.raises(ValueError):
|
|
- test_state.get_substate(("child_state", "grandchild_state", "invalid"))
|
|
|
|
|
|
+ test_state.get_substate(
|
|
|
|
+ (ChildState.get_name(), GrandchildState.get_name(), "invalid")
|
|
|
|
+ )
|
|
|
|
|
|
|
|
|
|
def test_set_dirty_var(test_state):
|
|
def test_set_dirty_var(test_state):
|
|
@@ -638,7 +668,7 @@ def test_set_dirty_substate(test_state, child_state, child_state2, grandchild_st
|
|
# Setting a var should mark it as dirty.
|
|
# Setting a var should mark it as dirty.
|
|
child_state.value = "test"
|
|
child_state.value = "test"
|
|
assert child_state.dirty_vars == {"value"}
|
|
assert child_state.dirty_vars == {"value"}
|
|
- assert test_state.dirty_substates == {"child_state"}
|
|
|
|
|
|
+ assert test_state.dirty_substates == {ChildState.get_name()}
|
|
assert child_state.dirty_substates == set()
|
|
assert child_state.dirty_substates == set()
|
|
|
|
|
|
# Cleaning the parent state should remove the dirty substate.
|
|
# Cleaning the parent state should remove the dirty substate.
|
|
@@ -648,12 +678,12 @@ def test_set_dirty_substate(test_state, child_state, child_state2, grandchild_st
|
|
|
|
|
|
# Setting a var on the grandchild should bubble up.
|
|
# Setting a var on the grandchild should bubble up.
|
|
grandchild_state.value2 = "test2"
|
|
grandchild_state.value2 = "test2"
|
|
- assert child_state.dirty_substates == {"grandchild_state"}
|
|
|
|
- assert test_state.dirty_substates == {"child_state"}
|
|
|
|
|
|
+ assert child_state.dirty_substates == {GrandchildState.get_name()}
|
|
|
|
+ assert test_state.dirty_substates == {ChildState.get_name()}
|
|
|
|
|
|
# Cleaning the middle state should keep the parent state dirty.
|
|
# Cleaning the middle state should keep the parent state dirty.
|
|
child_state._clean()
|
|
child_state._clean()
|
|
- assert test_state.dirty_substates == {"child_state"}
|
|
|
|
|
|
+ assert test_state.dirty_substates == {ChildState.get_name()}
|
|
assert child_state.dirty_substates == set()
|
|
assert child_state.dirty_substates == set()
|
|
assert grandchild_state.dirty_vars == set()
|
|
assert grandchild_state.dirty_vars == set()
|
|
|
|
|
|
@@ -698,7 +728,11 @@ def test_reset(test_state, child_state):
|
|
assert child_state.dirty_vars == {"count", "value"}
|
|
assert child_state.dirty_vars == {"count", "value"}
|
|
|
|
|
|
# The dirty substates should be reset.
|
|
# The dirty substates should be reset.
|
|
- assert test_state.dirty_substates == {"child_state", "child_state2", "child_state3"}
|
|
|
|
|
|
+ assert test_state.dirty_substates == {
|
|
|
|
+ ChildState.get_name(),
|
|
|
|
+ ChildState2.get_name(),
|
|
|
|
+ ChildState3.get_name(),
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
@pytest.mark.asyncio
|
|
@@ -719,8 +753,8 @@ async def test_process_event_simple(test_state):
|
|
# The delta should contain the changes, including computed vars.
|
|
# The delta should contain the changes, including computed vars.
|
|
# assert update.delta == {"test_state": {"num1": 69, "sum": 72.14}}
|
|
# assert update.delta == {"test_state": {"num1": 69, "sum": 72.14}}
|
|
assert update.delta == {
|
|
assert update.delta == {
|
|
- "test_state": {"num1": 69, "sum": 72.14, "upper": ""},
|
|
|
|
- "test_state.child_state3.grandchild_state3": {"computed": ""},
|
|
|
|
|
|
+ TestState.get_full_name(): {"num1": 69, "sum": 72.14, "upper": ""},
|
|
|
|
+ GrandchildState3.get_full_name(): {"computed": ""},
|
|
}
|
|
}
|
|
assert update.events == []
|
|
assert update.events == []
|
|
|
|
|
|
@@ -738,15 +772,17 @@ async def test_process_event_substate(test_state, child_state, grandchild_state)
|
|
assert child_state.value == ""
|
|
assert child_state.value == ""
|
|
assert child_state.count == 23
|
|
assert child_state.count == 23
|
|
event = Event(
|
|
event = Event(
|
|
- token="t", name="child_state.change_both", payload={"value": "hi", "count": 12}
|
|
|
|
|
|
+ token="t",
|
|
|
|
+ name=f"{ChildState.get_name()}.change_both",
|
|
|
|
+ payload={"value": "hi", "count": 12},
|
|
)
|
|
)
|
|
update = await test_state._process(event).__anext__()
|
|
update = await test_state._process(event).__anext__()
|
|
assert child_state.value == "HI"
|
|
assert child_state.value == "HI"
|
|
assert child_state.count == 24
|
|
assert child_state.count == 24
|
|
assert update.delta == {
|
|
assert update.delta == {
|
|
- "test_state": {"sum": 3.14, "upper": ""},
|
|
|
|
- "test_state.child_state": {"value": "HI", "count": 24},
|
|
|
|
- "test_state.child_state3.grandchild_state3": {"computed": ""},
|
|
|
|
|
|
+ TestState.get_full_name(): {"sum": 3.14, "upper": ""},
|
|
|
|
+ ChildState.get_full_name(): {"value": "HI", "count": 24},
|
|
|
|
+ GrandchildState3.get_full_name(): {"computed": ""},
|
|
}
|
|
}
|
|
test_state._clean()
|
|
test_state._clean()
|
|
|
|
|
|
@@ -754,15 +790,15 @@ async def test_process_event_substate(test_state, child_state, grandchild_state)
|
|
assert grandchild_state.value2 == ""
|
|
assert grandchild_state.value2 == ""
|
|
event = Event(
|
|
event = Event(
|
|
token="t",
|
|
token="t",
|
|
- name="child_state.grandchild_state.set_value2",
|
|
|
|
|
|
+ name=f"{GrandchildState.get_full_name()}.set_value2",
|
|
payload={"value": "new"},
|
|
payload={"value": "new"},
|
|
)
|
|
)
|
|
update = await test_state._process(event).__anext__()
|
|
update = await test_state._process(event).__anext__()
|
|
assert grandchild_state.value2 == "new"
|
|
assert grandchild_state.value2 == "new"
|
|
assert update.delta == {
|
|
assert update.delta == {
|
|
- "test_state": {"sum": 3.14, "upper": ""},
|
|
|
|
- "test_state.child_state.grandchild_state": {"value2": "new"},
|
|
|
|
- "test_state.child_state3.grandchild_state3": {"computed": ""},
|
|
|
|
|
|
+ TestState.get_full_name(): {"sum": 3.14, "upper": ""},
|
|
|
|
+ GrandchildState.get_full_name(): {"value2": "new"},
|
|
|
|
+ GrandchildState3.get_full_name(): {"computed": ""},
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -786,7 +822,7 @@ async def test_process_event_generator():
|
|
else:
|
|
else:
|
|
assert gen_state.value == count
|
|
assert gen_state.value == count
|
|
assert update.delta == {
|
|
assert update.delta == {
|
|
- "gen_state": {"value": count},
|
|
|
|
|
|
+ GenState.get_full_name(): {"value": count},
|
|
}
|
|
}
|
|
assert not update.final
|
|
assert not update.final
|
|
|
|
|
|
@@ -1955,7 +1991,7 @@ async def test_background_task_no_block(mock_app: rx.App, token: str):
|
|
mock_app,
|
|
mock_app,
|
|
Event(
|
|
Event(
|
|
token=token,
|
|
token=token,
|
|
- name=f"{BackgroundTaskState.get_name()}.background_task",
|
|
|
|
|
|
+ name=f"{BackgroundTaskState.get_full_name()}.background_task",
|
|
router_data=router_data,
|
|
router_data=router_data,
|
|
payload={},
|
|
payload={},
|
|
),
|
|
),
|
|
@@ -1975,7 +2011,7 @@ async def test_background_task_no_block(mock_app: rx.App, token: str):
|
|
mock_app,
|
|
mock_app,
|
|
Event(
|
|
Event(
|
|
token=token,
|
|
token=token,
|
|
- name=f"{BackgroundTaskState.get_name()}.other",
|
|
|
|
|
|
+ name=f"{BackgroundTaskState.get_full_name()}.other",
|
|
router_data=router_data,
|
|
router_data=router_data,
|
|
payload={},
|
|
payload={},
|
|
),
|
|
),
|
|
@@ -1986,7 +2022,7 @@ async def test_background_task_no_block(mock_app: rx.App, token: str):
|
|
# other task returns delta
|
|
# other task returns delta
|
|
assert update == StateUpdate(
|
|
assert update == StateUpdate(
|
|
delta={
|
|
delta={
|
|
- BackgroundTaskState.get_name(): {
|
|
|
|
|
|
+ BackgroundTaskState.get_full_name(): {
|
|
"order": [
|
|
"order": [
|
|
"background_task:start",
|
|
"background_task:start",
|
|
"other",
|
|
"other",
|
|
@@ -2022,10 +2058,13 @@ async def test_background_task_no_block(mock_app: rx.App, token: str):
|
|
emit_mock = mock_app.event_namespace.emit
|
|
emit_mock = mock_app.event_namespace.emit
|
|
|
|
|
|
first_ws_message = json.loads(emit_mock.mock_calls[0].args[1])
|
|
first_ws_message = json.loads(emit_mock.mock_calls[0].args[1])
|
|
- assert first_ws_message["delta"]["background_task_state"].pop("router") is not None
|
|
|
|
|
|
+ assert (
|
|
|
|
+ first_ws_message["delta"][BackgroundTaskState.get_full_name()].pop("router")
|
|
|
|
+ is not None
|
|
|
|
+ )
|
|
assert first_ws_message == {
|
|
assert first_ws_message == {
|
|
"delta": {
|
|
"delta": {
|
|
- "background_task_state": {
|
|
|
|
|
|
+ BackgroundTaskState.get_full_name(): {
|
|
"order": ["background_task:start"],
|
|
"order": ["background_task:start"],
|
|
"computed_order": ["background_task:start"],
|
|
"computed_order": ["background_task:start"],
|
|
}
|
|
}
|
|
@@ -2036,14 +2075,16 @@ async def test_background_task_no_block(mock_app: rx.App, token: str):
|
|
for call in emit_mock.mock_calls[1:5]:
|
|
for call in emit_mock.mock_calls[1:5]:
|
|
assert json.loads(call.args[1]) == {
|
|
assert json.loads(call.args[1]) == {
|
|
"delta": {
|
|
"delta": {
|
|
- "background_task_state": {"computed_order": ["background_task:start"]}
|
|
|
|
|
|
+ BackgroundTaskState.get_full_name(): {
|
|
|
|
+ "computed_order": ["background_task:start"],
|
|
|
|
+ }
|
|
},
|
|
},
|
|
"events": [],
|
|
"events": [],
|
|
"final": True,
|
|
"final": True,
|
|
}
|
|
}
|
|
assert json.loads(emit_mock.mock_calls[-2].args[1]) == {
|
|
assert json.loads(emit_mock.mock_calls[-2].args[1]) == {
|
|
"delta": {
|
|
"delta": {
|
|
- "background_task_state": {
|
|
|
|
|
|
+ BackgroundTaskState.get_full_name(): {
|
|
"order": exp_order,
|
|
"order": exp_order,
|
|
"computed_order": exp_order,
|
|
"computed_order": exp_order,
|
|
"dict_list": {},
|
|
"dict_list": {},
|
|
@@ -2054,7 +2095,7 @@ async def test_background_task_no_block(mock_app: rx.App, token: str):
|
|
}
|
|
}
|
|
assert json.loads(emit_mock.mock_calls[-1].args[1]) == {
|
|
assert json.loads(emit_mock.mock_calls[-1].args[1]) == {
|
|
"delta": {
|
|
"delta": {
|
|
- "background_task_state": {
|
|
|
|
|
|
+ BackgroundTaskState.get_full_name(): {
|
|
"computed_order": exp_order,
|
|
"computed_order": exp_order,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
@@ -2683,7 +2724,7 @@ async def test_preprocess(app_module_mock, token, test_state, expected, mocker):
|
|
assert isinstance(update, StateUpdate)
|
|
assert isinstance(update, StateUpdate)
|
|
updates.append(update)
|
|
updates.append(update)
|
|
assert len(updates) == 1
|
|
assert len(updates) == 1
|
|
- assert updates[0].delta["state"].pop("router") is not None
|
|
|
|
|
|
+ assert updates[0].delta[State.get_name()].pop("router") is not None
|
|
assert updates[0].delta == exp_is_hydrated(state, False)
|
|
assert updates[0].delta == exp_is_hydrated(state, False)
|
|
|
|
|
|
events = updates[0].events
|
|
events = updates[0].events
|
|
@@ -2727,7 +2768,7 @@ async def test_preprocess_multiple_load_events(app_module_mock, token, mocker):
|
|
assert isinstance(update, StateUpdate)
|
|
assert isinstance(update, StateUpdate)
|
|
updates.append(update)
|
|
updates.append(update)
|
|
assert len(updates) == 1
|
|
assert len(updates) == 1
|
|
- assert updates[0].delta["state"].pop("router") is not None
|
|
|
|
|
|
+ assert updates[0].delta[State.get_name()].pop("router") is not None
|
|
assert updates[0].delta == exp_is_hydrated(state, False)
|
|
assert updates[0].delta == exp_is_hydrated(state, False)
|
|
|
|
|
|
events = updates[0].events
|
|
events = updates[0].events
|
|
@@ -2759,22 +2800,27 @@ async def test_get_state(mock_app: rx.App, token: str):
|
|
if isinstance(mock_app.state_manager, StateManagerMemory):
|
|
if isinstance(mock_app.state_manager, StateManagerMemory):
|
|
# All substates are available
|
|
# All substates are available
|
|
assert tuple(sorted(test_state.substates)) == (
|
|
assert tuple(sorted(test_state.substates)) == (
|
|
- "child_state",
|
|
|
|
- "child_state2",
|
|
|
|
- "child_state3",
|
|
|
|
|
|
+ ChildState.get_name(),
|
|
|
|
+ ChildState2.get_name(),
|
|
|
|
+ ChildState3.get_name(),
|
|
)
|
|
)
|
|
else:
|
|
else:
|
|
# Sibling states are only populated if they have computed vars
|
|
# Sibling states are only populated if they have computed vars
|
|
- assert tuple(sorted(test_state.substates)) == ("child_state2", "child_state3")
|
|
|
|
|
|
+ assert tuple(sorted(test_state.substates)) == (
|
|
|
|
+ ChildState2.get_name(),
|
|
|
|
+ ChildState3.get_name(),
|
|
|
|
+ )
|
|
|
|
|
|
# Because ChildState3 has a computed var, it is always dirty, and always populated.
|
|
# Because ChildState3 has a computed var, it is always dirty, and always populated.
|
|
assert (
|
|
assert (
|
|
- test_state.substates["child_state3"].substates["grandchild_state3"].computed
|
|
|
|
|
|
+ test_state.substates[ChildState3.get_name()]
|
|
|
|
+ .substates[GrandchildState3.get_name()]
|
|
|
|
+ .computed
|
|
== ""
|
|
== ""
|
|
)
|
|
)
|
|
|
|
|
|
# Get the child_state2 directly.
|
|
# Get the child_state2 directly.
|
|
- child_state2_direct = test_state.get_substate(["child_state2"])
|
|
|
|
|
|
+ child_state2_direct = test_state.get_substate([ChildState2.get_name()])
|
|
child_state2_get_state = await test_state.get_state(ChildState2)
|
|
child_state2_get_state = await test_state.get_state(ChildState2)
|
|
# These should be the same object.
|
|
# These should be the same object.
|
|
assert child_state2_direct is child_state2_get_state
|
|
assert child_state2_direct is child_state2_get_state
|
|
@@ -2785,19 +2831,21 @@ async def test_get_state(mock_app: rx.App, token: str):
|
|
|
|
|
|
# Now the original root should have all substates populated.
|
|
# Now the original root should have all substates populated.
|
|
assert tuple(sorted(test_state.substates)) == (
|
|
assert tuple(sorted(test_state.substates)) == (
|
|
- "child_state",
|
|
|
|
- "child_state2",
|
|
|
|
- "child_state3",
|
|
|
|
|
|
+ ChildState.get_name(),
|
|
|
|
+ ChildState2.get_name(),
|
|
|
|
+ ChildState3.get_name(),
|
|
)
|
|
)
|
|
|
|
|
|
# ChildState should be retrievable
|
|
# ChildState should be retrievable
|
|
- child_state_direct = test_state.get_substate(["child_state"])
|
|
|
|
|
|
+ child_state_direct = test_state.get_substate([ChildState.get_name()])
|
|
child_state_get_state = await test_state.get_state(ChildState)
|
|
child_state_get_state = await test_state.get_state(ChildState)
|
|
# These should be the same object.
|
|
# These should be the same object.
|
|
assert child_state_direct is child_state_get_state
|
|
assert child_state_direct is child_state_get_state
|
|
|
|
|
|
# GrandchildState instance should be the same as the one retrieved from the child_state2.
|
|
# GrandchildState instance should be the same as the one retrieved from the child_state2.
|
|
- assert grandchild_state is child_state_direct.get_substate(["grandchild_state"])
|
|
|
|
|
|
+ assert grandchild_state is child_state_direct.get_substate(
|
|
|
|
+ [GrandchildState.get_name()]
|
|
|
|
+ )
|
|
grandchild_state.value2 = "set_value"
|
|
grandchild_state.value2 = "set_value"
|
|
|
|
|
|
assert test_state.get_delta() == {
|
|
assert test_state.get_delta() == {
|
|
@@ -2824,21 +2872,21 @@ async def test_get_state(mock_app: rx.App, token: str):
|
|
test_state._clean()
|
|
test_state._clean()
|
|
# All substates are available
|
|
# All substates are available
|
|
assert tuple(sorted(new_test_state.substates)) == (
|
|
assert tuple(sorted(new_test_state.substates)) == (
|
|
- "child_state",
|
|
|
|
- "child_state2",
|
|
|
|
- "child_state3",
|
|
|
|
|
|
+ ChildState.get_name(),
|
|
|
|
+ ChildState2.get_name(),
|
|
|
|
+ ChildState3.get_name(),
|
|
)
|
|
)
|
|
else:
|
|
else:
|
|
# With redis, we get a whole new instance
|
|
# With redis, we get a whole new instance
|
|
assert new_test_state is not test_state
|
|
assert new_test_state is not test_state
|
|
# Sibling states are only populated if they have computed vars
|
|
# Sibling states are only populated if they have computed vars
|
|
assert tuple(sorted(new_test_state.substates)) == (
|
|
assert tuple(sorted(new_test_state.substates)) == (
|
|
- "child_state2",
|
|
|
|
- "child_state3",
|
|
|
|
|
|
+ ChildState2.get_name(),
|
|
|
|
+ ChildState3.get_name(),
|
|
)
|
|
)
|
|
|
|
|
|
# Set a value on child_state2, should update cached var in grandchild_state2
|
|
# Set a value on child_state2, should update cached var in grandchild_state2
|
|
- child_state2 = new_test_state.get_substate(("child_state2",))
|
|
|
|
|
|
+ child_state2 = new_test_state.get_substate((ChildState2.get_name(),))
|
|
child_state2.value = "set_c2_value"
|
|
child_state2.value = "set_c2_value"
|
|
|
|
|
|
assert new_test_state.get_delta() == {
|
|
assert new_test_state.get_delta() == {
|
|
@@ -2929,8 +2977,8 @@ async def test_get_state_from_sibling_not_cached(mock_app: rx.App, token: str):
|
|
|
|
|
|
if isinstance(mock_app.state_manager, StateManagerRedis):
|
|
if isinstance(mock_app.state_manager, StateManagerRedis):
|
|
# When redis is used, only states with computed vars are pre-fetched.
|
|
# When redis is used, only states with computed vars are pre-fetched.
|
|
- assert "child2" not in root.substates
|
|
|
|
- assert "child3" in root.substates # (due to @rx.var)
|
|
|
|
|
|
+ assert Child2.get_name() not in root.substates
|
|
|
|
+ assert Child3.get_name() in root.substates # (due to @rx.var)
|
|
|
|
|
|
# Get the unconnected sibling state, which will be used to `get_state` other instances.
|
|
# Get the unconnected sibling state, which will be used to `get_state` other instances.
|
|
child = root.get_substate(Child.get_full_name().split("."))
|
|
child = root.get_substate(Child.get_full_name().split("."))
|