-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_gdrive.py
51 lines (39 loc) · 1.76 KB
/
test_gdrive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import unittest
from unittest.mock import patch, Mock
from googleapiclient.errors import HttpError
import httplib2
import gdrive # your module
class TestGDrive(unittest.TestCase):
@patch('gdrive.pickle.load')
@patch('gdrive.build')
@patch('gdrive.MediaFileUpload')
def test_upload_file(self, mock_media_file_upload, mock_build, mock_pickle_load):
# Mock the MediaFileUpload to not actually try to open a file
mock_media_file_upload.return_value = Mock()
# Mock the Google Drive API client
mock_service = mock_build.return_value
mock_service.files.return_value.create.return_value.execute.return_value = {'id': '123'}
# Mock the credentials
mock_creds = Mock()
mock_creds.valid = True
mock_pickle_load.return_value = mock_creds
result = gdrive.upload_file('filename', 'filepath')
self.assertEqual(result, '123')
@patch('gdrive.pickle.load')
@patch('gdrive.build')
@patch('gdrive.MediaFileUpload')
def test_upload_file_http_error(self, mock_media_file_upload, mock_build, mock_pickle_load):
# Mock the MediaFileUpload to not actually try to open a file
mock_media_file_upload.return_value = Mock()
# Mock the Google Drive API client to raise an HttpError
mock_service = mock_build.return_value
resp = httplib2.Response({"status": 400})
mock_service.files.return_value.create.return_value.execute.side_effect = HttpError(resp, b'content')
# Mock the credentials
mock_creds = Mock()
mock_creds.valid = True
mock_pickle_load.return_value = mock_creds
with self.assertRaises(HttpError):
gdrive.upload_file('filename', 'filepath')
if __name__ == '__main__':
unittest.main()