conftest.py 6.9 KB

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