11# coding: utf-8
22
33from __future__ import unicode_literals
4- from mock import mock_open , patch
4+ from mock import patch
55import six
6+ from test .util .streamable_mock_open import streamable_mock_open
67
78
89def test_upload_then_update (box_client , test_file_path , test_file_content , update_file_content , file_name ):
9- with patch ('boxsdk.object.folder.open' , mock_open (read_data = test_file_content ), create = True ):
10+ with patch ('boxsdk.object.folder.open' , streamable_mock_open (read_data = test_file_content ), create = True ):
1011 file_object = box_client .folder ('0' ).upload (test_file_path , file_name )
1112 assert file_object .name == file_name
1213 file_object_with_info = file_object .get ()
@@ -20,13 +21,15 @@ def test_upload_then_update(box_client, test_file_path, test_file_content, updat
2021 assert len (folder_items ) == 1
2122 assert folder_items [0 ].object_id == file_object .object_id
2223 assert folder_items [0 ].name == file_object .name
23- with patch ('boxsdk.object.file.open' , mock_open (read_data = update_file_content ), create = True ):
24+ with patch ('boxsdk.object.file.open' , streamable_mock_open (read_data = update_file_content ), create = True ):
2425 updated_file_object = file_object .update_contents (test_file_path )
2526 assert updated_file_object .name == file_name
2627 file_object_with_info = updated_file_object .get ()
2728 assert file_object_with_info .id == updated_file_object .object_id
2829 assert file_object_with_info .name == file_name
2930 file_content = updated_file_object .content ()
31+ expected_file_content = update_file_content .encode ('utf-8' ) if isinstance (update_file_content , six .text_type )\
32+ else update_file_content
3033 assert file_content == expected_file_content
3134 folder_items = box_client .folder ('0' ).get_items (100 )
3235 assert len (folder_items ) == 1
@@ -35,10 +38,23 @@ def test_upload_then_update(box_client, test_file_path, test_file_content, updat
3538
3639
3740def test_upload_then_download (box_client , test_file_path , test_file_content , file_name ):
38- with patch ('boxsdk.object.folder.open' , mock_open (read_data = test_file_content ), create = True ):
41+ with patch ('boxsdk.object.folder.open' , streamable_mock_open (read_data = test_file_content ), create = True ):
3942 file_object = box_client .folder ('0' ).upload (test_file_path , file_name )
4043 writeable_stream = six .BytesIO ()
4144 file_object .download_to (writeable_stream )
4245 expected_file_content = test_file_content .encode ('utf-8' ) if isinstance (test_file_content , six .text_type )\
4346 else test_file_content
4447 assert writeable_stream .getvalue () == expected_file_content
48+
49+
50+ if __name__ == '__main__' :
51+ from test .functional .conftest import box_client , box_oauth , mock_box , Box
52+
53+ class MonkeyPatch :
54+ def setattr (self , target , attr , value ):
55+ setattr (target , attr , value )
56+
57+ client_id , client_secret , login = 'client_id' , 'client_secret' , 'login'
58+ box = mock_box (Box (), MonkeyPatch (), client_id , client_secret , 'user' , login )
59+ client = box_client (box_oauth (client_id , client_secret , login ))
60+ test_upload_then_update (client , '/path/to/file' , 'Hello' , 'Goodbye' , 'foo.txt' )
0 commit comments