|
@@ -4,7 +4,8 @@ from typing import Generator
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
-from pynecone.state import State
|
|
|
+import pynecone as pc
|
|
|
+from pynecone.event import EventSpec
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
@@ -25,7 +26,7 @@ def list_mutation_state():
|
|
|
A state with list mutation features.
|
|
|
"""
|
|
|
|
|
|
- class TestState(State):
|
|
|
+ class TestState(pc.State):
|
|
|
"""The test state."""
|
|
|
|
|
|
# plain list
|
|
@@ -88,7 +89,7 @@ def dict_mutation_state():
|
|
|
A state with dict mutation features.
|
|
|
"""
|
|
|
|
|
|
- class TestState(State):
|
|
|
+ class TestState(pc.State):
|
|
|
"""The test state."""
|
|
|
|
|
|
# plain dict
|
|
@@ -134,3 +135,65 @@ def dict_mutation_state():
|
|
|
self.friend_in_nested_dict["friend"]["age"] = 30
|
|
|
|
|
|
return TestState()
|
|
|
+
|
|
|
+
|
|
|
+class UploadState(pc.State):
|
|
|
+ """The base state for uploading a file."""
|
|
|
+
|
|
|
+ img: str
|
|
|
+
|
|
|
+ async def handle_upload(self, file: pc.UploadFile):
|
|
|
+ """Handle the upload of a file.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ file: The uploaded file.
|
|
|
+ """
|
|
|
+ upload_data = await file.read()
|
|
|
+ outfile = f".web/public/{file.filename}"
|
|
|
+
|
|
|
+ # Save the file.
|
|
|
+ with open(outfile, "wb") as file_object:
|
|
|
+ file_object.write(upload_data)
|
|
|
+
|
|
|
+ # Update the img var.
|
|
|
+ self.img = file.filename
|
|
|
+
|
|
|
+
|
|
|
+class BaseState(pc.State):
|
|
|
+ """The test base state."""
|
|
|
+
|
|
|
+ pass
|
|
|
+
|
|
|
+
|
|
|
+class SubUploadState(BaseState):
|
|
|
+ """The test substate."""
|
|
|
+
|
|
|
+ img: str
|
|
|
+
|
|
|
+ async def handle_upload(self, file: pc.UploadFile):
|
|
|
+ """Handle the upload of a file.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ file: The uploaded file.
|
|
|
+ """
|
|
|
+ pass
|
|
|
+
|
|
|
+
|
|
|
+@pytest.fixture
|
|
|
+def upload_event_spec():
|
|
|
+ """Create an event Spec for a base state.
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ Event Spec.
|
|
|
+ """
|
|
|
+ return EventSpec(handler=UploadState.handle_upload, upload=True) # type: ignore
|
|
|
+
|
|
|
+
|
|
|
+@pytest.fixture
|
|
|
+def upload_sub_state_event_spec():
|
|
|
+ """Create an event Spec for a substate.
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ Event Spec.
|
|
|
+ """
|
|
|
+ return EventSpec(handler=SubUploadState.handle_upload, upload=True) # type: ignore
|