mutation.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. """Test states for mutable vars."""
  2. from typing import Dict, List, Set, Union
  3. import reflex as rx
  4. from reflex.state import BaseState
  5. class DictMutationTestState(BaseState):
  6. """A state for testing ReflexDict mutation."""
  7. # plain dict
  8. details = {"name": "Tommy"}
  9. def add_age(self):
  10. """Add an age to the dict."""
  11. self.details.update({"age": 20}) # type: ignore
  12. def change_name(self):
  13. """Change the name in the dict."""
  14. self.details["name"] = "Jenny"
  15. def remove_last_detail(self):
  16. """Remove the last item in the dict."""
  17. self.details.popitem()
  18. def clear_details(self):
  19. """Clear the dict."""
  20. self.details.clear()
  21. def remove_name(self):
  22. """Remove the name from the dict."""
  23. del self.details["name"]
  24. def pop_out_age(self):
  25. """Pop out the age from the dict."""
  26. self.details.pop("age")
  27. # dict in list
  28. address = [{"home": "home address"}, {"work": "work address"}]
  29. def remove_home_address(self):
  30. """Remove the home address from dict in the list."""
  31. self.address[0].pop("home")
  32. def add_street_to_home_address(self):
  33. """Set street key in the dict in the list."""
  34. self.address[0]["street"] = "street address"
  35. # nested dict
  36. friend_in_nested_dict = {"name": "Nikhil", "friend": {"name": "Alek"}}
  37. def change_friend_name(self):
  38. """Change the friend's name in the nested dict."""
  39. self.friend_in_nested_dict["friend"]["name"] = "Tommy"
  40. def remove_friend(self):
  41. """Remove the friend from the nested dict."""
  42. self.friend_in_nested_dict.pop("friend")
  43. def add_friend_age(self):
  44. """Add an age to the friend in the nested dict."""
  45. self.friend_in_nested_dict["friend"]["age"] = 30
  46. class ListMutationTestState(BaseState):
  47. """A state for testing ReflexList mutation."""
  48. # plain list
  49. plain_friends = ["Tommy"]
  50. def make_friend(self):
  51. """Add a friend to the list."""
  52. self.plain_friends.append("another-fd")
  53. def change_first_friend(self):
  54. """Change the first friend in the list."""
  55. self.plain_friends[0] = "Jenny"
  56. def unfriend_all_friends(self):
  57. """Unfriend all friends in the list."""
  58. self.plain_friends.clear()
  59. def unfriend_first_friend(self):
  60. """Unfriend the first friend in the list."""
  61. del self.plain_friends[0]
  62. def remove_last_friend(self):
  63. """Remove the last friend in the list."""
  64. self.plain_friends.pop()
  65. def make_friends_with_colleagues(self):
  66. """Add list of friends to the list."""
  67. colleagues = ["Peter", "Jimmy"]
  68. self.plain_friends.extend(colleagues)
  69. def remove_tommy(self):
  70. """Remove Tommy from the list."""
  71. self.plain_friends.remove("Tommy")
  72. # list in dict
  73. friends_in_dict = {"Tommy": ["Jenny"]}
  74. def remove_jenny_from_tommy(self):
  75. """Remove Jenny from Tommy's friends list."""
  76. self.friends_in_dict["Tommy"].remove("Jenny")
  77. def add_jimmy_to_tommy_friends(self):
  78. """Add Jimmy to Tommy's friends list."""
  79. self.friends_in_dict["Tommy"].append("Jimmy")
  80. def tommy_has_no_fds(self):
  81. """Clear Tommy's friends list."""
  82. self.friends_in_dict["Tommy"].clear()
  83. # nested list
  84. friends_in_nested_list = [["Tommy"], ["Jenny"]]
  85. def remove_first_group(self):
  86. """Remove the first group of friends from the nested list."""
  87. self.friends_in_nested_list.pop(0)
  88. def remove_first_person_from_first_group(self):
  89. """Remove the first person from the first group of friends in the nested list."""
  90. self.friends_in_nested_list[0].pop(0)
  91. def add_jimmy_to_second_group(self):
  92. """Add Jimmy to the second group of friends in the nested list."""
  93. self.friends_in_nested_list[1].append("Jimmy")
  94. class OtherBase(rx.Base):
  95. """A Base model with a str field."""
  96. bar: str = ""
  97. class CustomVar(rx.Base):
  98. """A Base model with multiple fields."""
  99. foo: str = ""
  100. array: List[str] = []
  101. hashmap: Dict[str, str] = {}
  102. test_set: Set[str] = set()
  103. custom: OtherBase = OtherBase()
  104. class MutableTestState(BaseState):
  105. """A test state."""
  106. array: List[Union[str, List, Dict[str, str]]] = [
  107. "value",
  108. [1, 2, 3],
  109. {"key": "value"},
  110. ]
  111. hashmap: Dict[str, Union[List, str, Dict[str, str]]] = {
  112. "key": ["list", "of", "values"],
  113. "another_key": "another_value",
  114. "third_key": {"key": "value"},
  115. }
  116. test_set: Set[Union[str, int]] = {1, 2, 3, 4, "five"}
  117. custom: CustomVar = CustomVar()
  118. _be_custom: CustomVar = CustomVar()
  119. def reassign_mutables(self):
  120. """Assign mutable fields to different values."""
  121. self.array = ["modified_value", [1, 2, 3], {"mod_key": "mod_value"}]
  122. self.hashmap = {
  123. "mod_key": ["list", "of", "values"],
  124. "mod_another_key": "another_value",
  125. "mod_third_key": {"key": "value"},
  126. }
  127. self.test_set = {1, 2, 3, 4, "five"}