Test that file upload works correctly. Args: tmp_path: Temporary path. state: The state class. delta: Expected delta token: a Token. mocker: pytest mocker object.
(tmp_path, state, delta, token: str, mocker)
| 736 | ], |
| 737 | ) |
| 738 | async def test_upload_file(tmp_path, state, delta, token: str, mocker): |
| 739 | """Test that file upload works correctly. |
| 740 | |
| 741 | Args: |
| 742 | tmp_path: Temporary path. |
| 743 | state: The state class. |
| 744 | delta: Expected delta |
| 745 | token: a Token. |
| 746 | mocker: pytest mocker object. |
| 747 | """ |
| 748 | mocker.patch( |
| 749 | "nextpy.state.State.class_subclasses", |
| 750 | {state if state is FileUploadState else FileStateBase1}, |
| 751 | ) |
| 752 | state._tmp_path = tmp_path |
| 753 | # The App state must be the "root" of the state tree |
| 754 | app = App(state=State) |
| 755 | app.event_namespace.emit = AsyncMock() # type: ignore |
| 756 | current_state = await app.state_manager.get_state(token) |
| 757 | data = b"This is binary data" |
| 758 | |
| 759 | # Create a binary IO object and write data to it |
| 760 | bio = io.BytesIO() |
| 761 | bio.write(data) |
| 762 | |
| 763 | state_name = state.get_full_name().partition(".")[2] or state.get_name() |
| 764 | request_mock = unittest.mock.Mock() |
| 765 | request_mock.headers = { |
| 766 | "nextpy-client-token": token, |
| 767 | "nextpy-event-handler": f"state.{state_name}.multi_handle_upload", |
| 768 | } |
| 769 | |
| 770 | file1 = UploadFile( |
| 771 | filename=f"image1.jpg", |
| 772 | file=bio, |
| 773 | ) |
| 774 | file2 = UploadFile( |
| 775 | filename=f"image2.jpg", |
| 776 | file=bio, |
| 777 | ) |
| 778 | upload_fn = upload(app) |
| 779 | streaming_response = await upload_fn(request_mock, [file1, file2]) |
| 780 | async for state_update in streaming_response.body_iterator: |
| 781 | assert ( |
| 782 | state_update |
| 783 | == StateUpdate(delta=delta, events=[], final=True).json() + "\n" |
| 784 | ) |
| 785 | |
| 786 | current_state = await app.state_manager.get_state(token) |
| 787 | state_dict = current_state.dict()[state.get_full_name()] |
| 788 | assert state_dict["img_list"] == [ |
| 789 | "image1.jpg", |
| 790 | "image2.jpg", |
| 791 | ] |
| 792 | |
| 793 | if isinstance(app.state_manager, StateManagerRedis): |
| 794 | await app.state_manager.close() |
| 795 |
nothing calls this directly
no test coverage detected