-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_storage_api.py
More file actions
247 lines (186 loc) · 7.89 KB
/
test_storage_api.py
File metadata and controls
247 lines (186 loc) · 7.89 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# coding: utf-8
"""
--------------------------------------------------------------------------------------------------------------------
<copyright company="Aspose" file="test_storage_api.py">
Copyright (c) 2022 Aspose.HTML for Cloud
</copyright>
<summary>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
</summary>
--------------------------------------------------------------------------------------------------------------------
"""
from __future__ import absolute_import
import os
import six
import unittest
import datetime
from asposehtmlcloud.rest import ApiException
from test.test_helper import TestHelper
class TestStorageApi(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.api = TestHelper.storage
# **************************************************
# Test storage Api
# **************************************************
def test_get_disc_usage(self):
"""Test case for get_disc_usage
Check the disk usage of the current account
"""
res = self.api.get_disc_usage()
res = res.to_dict()
self.assertTrue('total_size' in res)
self.assertTrue('used_size' in res)
self.assertTrue(isinstance(res['total_size'], int if six.PY3 else long))
self.assertTrue(isinstance(res['used_size'], int if six.PY3 else long))
print(res)
def test_object_exists(self):
"""Test case for object_exists
Check if a specific file or folder exists
"""
exist_folder = TestHelper.get_folder()
res = self.api.object_exists(exist_folder)
res = res.to_dict()
self.assertTrue('exists' in res)
self.assertTrue('is_folder' in res)
self.assertTrue(isinstance(res['exists'], bool))
self.assertTrue(isinstance(res['is_folder'], bool))
self.assertTrue(res['exists'])
self.assertTrue(res['is_folder'])
print(res)
non_exist_file = TestHelper.get_folder() + 'non_exist_file.gif'
res = self.api.object_exists(non_exist_file)
res = res.to_dict()
self.assertFalse(res['exists'])
self.assertFalse(res['is_folder'])
print(res)
def test_storage_exist(self):
"""Test case for storage_exist
Check if storage exists
"""
not_exist = "Not_exist_storage"
res = self.api.storage_exists(not_exist)
res = res.to_dict()
self.assertTrue('exists' in res)
self.assertTrue(isinstance(res['exists'], bool))
self.assertFalse(res['exists'])
print(res)
# **************************************************
# Test file Api
# **************************************************
def test_upload_file(self):
"""Test case for upload_file
Upload a specific file
"""
file_name = "test_upload_file.jpg"
src = TestHelper.get_local_folder() + file_name
folder = TestHelper.get_folder()
result = self.api.upload_file(folder, src)
self.assertTrue(len(result.uploaded) == 1)
self.assertTrue(len(result.errors) == 0)
# check file exists
res = self.api.object_exists(result.uploaded[0])
res = res.to_dict()
self.assertTrue(res['exists'])
self.assertFalse(res['is_folder'])
print (res)
def test_delete_file(self):
"""Test case for delete_file
Remove a specific file
"""
# upload file to storage
file_name = "test_for_delete.html"
src = TestHelper.get_local_folder() + file_name
folder = 'TestDelete'
res = self.api.upload_file(folder, src)
self.assertTrue(len(res.uploaded) == 1)
self.assertTrue(len(res.errors) == 0)
# delete file in the storage
self.api.delete_file(folder + '/' + file_name)
def test_download_file(self):
"""Test case for download_file
Download a specific file
"""
file_name = "test_upload_file.jpg"
size = TestHelper.get_file_size(file_name)
# put file to storage
res = TestHelper.upload_file(file_name)
self.assertTrue(len(res.uploaded) == 1)
self.assertTrue(len(res.errors) == 0)
download_file = TestHelper.get_folder() + "/" + file_name
save_file = TestHelper.get_local_dest_folder() + "test_upload_file.jpg"
src = self.api.download_file(download_file)
TestHelper.move_file(src, save_file)
self.assertTrue(os.path.getsize(save_file) == size)
def test_download_non_existin_file(self):
"""Test case for download_file
Download a specific file
"""
self.assertRaises(ApiException, self.api.download_file, "non_existing_file.txt")
def test_create_folder(self):
"""Test case for create_folder
Create the folder
"""
folder_name = "test_folder"
self.api.create_folder(folder_name)
# Checking if the folder has been created
res = self.api.object_exists(folder_name)
self.assertTrue(res.exists)
self.assertTrue(res.is_folder)
print(res)
# Delete folder
self.api.delete_folder(folder_name)
# Checking if the folder has been deleted
res = self.api.object_exists(folder_name)
self.assertFalse(res.exists)
self.assertFalse(res.is_folder)
print(res)
def test_delete_folder(self):
"""Test case for delete_folder
Remove a specific folder
"""
# Create test folder
folder_name = "tmp_folder"
self.api.create_folder(folder_name)
# Checking if the folder has been created
res = self.api.object_exists(folder_name)
self.assertTrue(res.exists)
self.assertTrue(res.is_folder)
print(res)
# Delete folder
self.api.delete_folder(folder_name)
# Checking if the folder has been deleted
res = self.api.object_exists(folder_name)
self.assertFalse(res.exists)
self.assertFalse(res.is_folder)
print(res)
def test_get_files_list(self):
"""Test case for get_files_list
Get the file listing of a specific folder
"""
folder = TestHelper.get_folder()
res = self.api.get_files_list(folder)
self.assertTrue(isinstance(res.value, list))
one_file = res.value[0]
self.assertTrue(isinstance(one_file.name, str))
self.assertTrue(isinstance(one_file.is_folder, bool))
self.assertTrue(isinstance(one_file.modified_date, datetime.datetime))
self.assertTrue(isinstance(one_file.size, int))
self.assertTrue(isinstance(one_file.path, str))
print (res)
if __name__ == '__main__':
unittest.main()