mutation.py 5.0 KB

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