conftest.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_upload(self, file: pc.UploadFile):
  98. """Handle the upload of a file.
  99. Args:
  100. file: The uploaded file.
  101. """
  102. upload_data = await file.read()
  103. outfile = f".web/public/{file.filename}"
  104. # Save the file.
  105. with open(outfile, "wb") as file_object:
  106. file_object.write(upload_data)
  107. # Update the img var.
  108. self.img = file.filename
  109. async def multi_handle_upload(self, files: List[pc.UploadFile]):
  110. """Handle the upload of a file.
  111. Args:
  112. files: The uploaded files.
  113. """
  114. for file in files:
  115. upload_data = await file.read()
  116. outfile = f".web/public/{file.filename}"
  117. # Save the file.
  118. with open(outfile, "wb") as file_object:
  119. file_object.write(upload_data)
  120. # Update the img var.
  121. self.img_list.append(file.filename)
  122. class BaseState(pc.State):
  123. """The test base state."""
  124. pass
  125. class SubUploadState(BaseState):
  126. """The test substate."""
  127. img: str
  128. async def handle_upload(self, file: pc.UploadFile):
  129. """Handle the upload of a file.
  130. Args:
  131. file: The uploaded file.
  132. """
  133. pass
  134. @pytest.fixture
  135. def upload_event_spec():
  136. """Create an event Spec for a base state.
  137. Returns:
  138. Event Spec.
  139. """
  140. return EventSpec(handler=UploadState.handle_upload, upload=True) # type: ignore
  141. @pytest.fixture
  142. def upload_sub_state_event_spec():
  143. """Create an event Spec for a substate.
  144. Returns:
  145. Event Spec.
  146. """
  147. return EventSpec(handler=SubUploadState.handle_upload, upload=True) # type: ignore
  148. @pytest.fixture
  149. def multi_upload_event_spec():
  150. """Create an event Spec for a multi-upload base state.
  151. Returns:
  152. Event Spec.
  153. """
  154. return EventSpec(handler=UploadState.multi_handle_upload, upload=True) # type: ignore