conftest.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. """Test fixtures."""
  2. import platform
  3. from typing import Generator, List
  4. import pytest
  5. import pynecone as pc
  6. from pynecone.event import EventSpec
  7. @pytest.fixture(scope="function")
  8. def windows_platform() -> Generator:
  9. """Check if system is windows.
  10. Yields:
  11. whether system is windows.
  12. """
  13. yield platform.system() == "Windows"
  14. @pytest.fixture
  15. def list_mutation_state():
  16. """Create a state with list mutation features.
  17. Returns:
  18. A state with list mutation features.
  19. """
  20. class TestState(pc.State):
  21. """The test state."""
  22. # plain list
  23. plain_friends = ["Tommy"]
  24. def make_friend(self):
  25. self.plain_friends.append("another-fd")
  26. def change_first_friend(self):
  27. self.plain_friends[0] = "Jenny"
  28. def unfriend_all_friends(self):
  29. self.plain_friends.clear()
  30. def unfriend_first_friend(self):
  31. del self.plain_friends[0]
  32. def remove_last_friend(self):
  33. self.plain_friends.pop()
  34. def make_friends_with_colleagues(self):
  35. colleagues = ["Peter", "Jimmy"]
  36. self.plain_friends.extend(colleagues)
  37. def remove_tommy(self):
  38. self.plain_friends.remove("Tommy")
  39. # list in dict
  40. friends_in_dict = {"Tommy": ["Jenny"]}
  41. def remove_jenny_from_tommy(self):
  42. self.friends_in_dict["Tommy"].remove("Jenny")
  43. def add_jimmy_to_tommy_friends(self):
  44. self.friends_in_dict["Tommy"].append("Jimmy")
  45. def tommy_has_no_fds(self):
  46. self.friends_in_dict["Tommy"].clear()
  47. # nested list
  48. friends_in_nested_list = [["Tommy"], ["Jenny"]]
  49. def remove_first_group(self):
  50. self.friends_in_nested_list.pop(0)
  51. def remove_first_person_from_first_group(self):
  52. self.friends_in_nested_list[0].pop(0)
  53. def add_jimmy_to_second_group(self):
  54. self.friends_in_nested_list[1].append("Jimmy")
  55. return TestState()
  56. @pytest.fixture
  57. def dict_mutation_state():
  58. """Create a state with dict mutation features.
  59. Returns:
  60. A state with dict mutation features.
  61. """
  62. class TestState(pc.State):
  63. """The test state."""
  64. # plain dict
  65. details = {"name": "Tommy"}
  66. def add_age(self):
  67. self.details.update({"age": 20}) # type: ignore
  68. def change_name(self):
  69. self.details["name"] = "Jenny"
  70. def remove_last_detail(self):
  71. self.details.popitem()
  72. def clear_details(self):
  73. self.details.clear()
  74. def remove_name(self):
  75. del self.details["name"]
  76. def pop_out_age(self):
  77. self.details.pop("age")
  78. # dict in list
  79. address = [{"home": "home address"}, {"work": "work address"}]
  80. def remove_home_address(self):
  81. self.address[0].pop("home")
  82. def add_street_to_home_address(self):
  83. self.address[0]["street"] = "street address"
  84. # nested dict
  85. friend_in_nested_dict = {"name": "Nikhil", "friend": {"name": "Alek"}}
  86. def change_friend_name(self):
  87. self.friend_in_nested_dict["friend"]["name"] = "Tommy"
  88. def remove_friend(self):
  89. self.friend_in_nested_dict.pop("friend")
  90. def add_friend_age(self):
  91. self.friend_in_nested_dict["friend"]["age"] = 30
  92. return TestState()
  93. class UploadState(pc.State):
  94. """The base state for uploading a file."""
  95. img: str
  96. img_list: List[str]
  97. async def handle_upload1(self, file: pc.UploadFile):
  98. """Handle the upload of a file.
  99. Args:
  100. file: The uploaded file.
  101. """
  102. pass
  103. async def multi_handle_upload(self, files: List[pc.UploadFile]):
  104. """Handle the upload of a file.
  105. Args:
  106. files: The uploaded files.
  107. """
  108. pass
  109. class BaseState(pc.State):
  110. """The test base state."""
  111. pass
  112. class SubUploadState(BaseState):
  113. """The test substate."""
  114. img: str
  115. async def handle_upload(self, file: pc.UploadFile):
  116. """Handle the upload of a file.
  117. Args:
  118. file: The uploaded file.
  119. """
  120. pass
  121. @pytest.fixture
  122. def upload_event_spec():
  123. """Create an event Spec for a base state.
  124. Returns:
  125. Event Spec.
  126. """
  127. return EventSpec(handler=UploadState.handle_upload1, upload=True) # type: ignore
  128. @pytest.fixture
  129. def upload_sub_state_event_spec():
  130. """Create an event Spec for a substate.
  131. Returns:
  132. Event Spec.
  133. """
  134. return EventSpec(handler=SubUploadState.handle_upload, upload=True) # type: ignore
  135. @pytest.fixture
  136. def multi_upload_event_spec():
  137. """Create an event Spec for a multi-upload base state.
  138. Returns:
  139. Event Spec.
  140. """
  141. return EventSpec(handler=UploadState.multi_handle_upload, upload=True) # type: ignore
  142. @pytest.fixture
  143. def upload_state(tmp_path):
  144. """Create upload state.
  145. Args:
  146. tmp_path: pytest tmp_path
  147. Returns:
  148. The state
  149. """
  150. class FileUploadState(pc.State):
  151. """The base state for uploading a file."""
  152. img: str
  153. img_list: List[str]
  154. async def handle_upload2(self, file):
  155. """Handle the upload of a file.
  156. Args:
  157. file: The uploaded file.
  158. """
  159. upload_data = await file.read()
  160. outfile = f"{tmp_path}/{file.filename}"
  161. # Save the file.
  162. with open(outfile, "wb") as file_object:
  163. file_object.write(upload_data)
  164. # Update the img var.
  165. self.img = file.filename
  166. async def multi_handle_upload(self, files: List[pc.UploadFile]):
  167. """Handle the upload of a file.
  168. Args:
  169. files: The uploaded files.
  170. """
  171. for file in files:
  172. upload_data = await file.read()
  173. outfile = f"{tmp_path}/{file.filename}"
  174. # Save the file.
  175. with open(outfile, "wb") as file_object:
  176. file_object.write(upload_data)
  177. # Update the img var.
  178. self.img_list.append(file.filename)
  179. return FileUploadState