def test_resetting_value(self):
        # Simulate an upload, then resetting the value from the
        # kernel.
        uploader = FileUpload()
        message = {'value': [FILE_UPLOAD_FRONTEND_CONTENT]}
        uploader.set_state(message)

        uploader.value = []  # reset value to an empty file list

        assert uploader.get_state(key='value') == {'value': []}
 def test_receive_single_file(self):
     uploader = FileUpload()
     message = {'value': [FILE_UPLOAD_FRONTEND_CONTENT]}
     uploader.set_state(message)
     assert len(uploader.value) == 1
     (uploaded_file, ) = uploader.value
     assert uploaded_file.name == 'file-name.txt'
     assert uploaded_file.type == 'text/plain'
     assert uploaded_file.size == 20760
     assert uploaded_file.content.tobytes() == b'file content'
     assert (uploaded_file.last_modified == dt.datetime(
         2020, 1, 9, 13, 58, 16, 434000, tzinfo=dt.timezone.utc))
 def test_receive_multiple_files(self):
     uploader = FileUpload(multiple=True)
     message = {
         'value': [
             FILE_UPLOAD_FRONTEND_CONTENT, {
                 **FILE_UPLOAD_FRONTEND_CONTENT,
                 **{
                     'name': 'other-file-name.txt'
                 }
             }
         ]
     }
     uploader.set_state(message)
     assert len(uploader.value) == 2
     assert uploader.value[0].name == 'file-name.txt'
     assert uploader.value[1].name == 'other-file-name.txt'
    def test_serialization_deserialization_integrity(self):
        # The value traitlet needs to remain unchanged following
        # a serialization / deserialization roundtrip, otherwise
        # the kernel dispatches it back to the frontend following
        # a state change, because it doesn't recognize that the
        # property_lock entry is the same as the new value.
        from ipykernel.comm import Comm
        uploader = FileUpload()
        mock_comm = MagicMock(spec=Comm)
        mock_comm.kernel = 'does not matter'
        mock_comm.send = MagicMock()
        uploader.comm = mock_comm
        message = {'value': [FILE_UPLOAD_FRONTEND_CONTENT]}
        uploader.set_state(message)

        # Check that no message is sent back to the frontend
        # as a result of setting the state.
        mock_comm.send.assert_not_called()