forked from cloudconvert/cloudconvert-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestTask.py
More file actions
161 lines (124 loc) · 5.11 KB
/
Copy pathtestTask.py
File metadata and controls
161 lines (124 loc) · 5.11 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
###################################################################
## Test cases for Cloud Convert API tasks endpoints ##
## ##
## How to run ? : ##
## $ python tests/unit/testTask.py ##
###################################################################
import sys
import os
sys.path.append(os.getcwd())
import unittest
import requests_mock
import cloudconvert
import json
from cloudconvert.config import SANDBOX_API_KEY
class TaskTestCase(unittest.TestCase):
def setUp(self):
"""
Test case setup method
:return:
"""
print("Setting up Task test cases")
self.cloudconvert = cloudconvert
# setup the client with the provided API key by configuring
self.cloudconvert.configure(api_key = SANDBOX_API_KEY, sandbox = True)
self.responses_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "responses")
def testCreateTask(self):
"""
Create Task
:return:
"""
print("testcase for creating task..")
# create dict for new task
new_import_url_task = {
"url": "https://github.com/cloudconvert/cloudconvert-php/raw/master/tests/Integration/files/input.pdf"
}
with requests_mock.mock() as m:
with open("{}/{}".format(self.responses_path, "task_created.json")) as f:
response_json = json.load(f)
m.post("https://api.sandbox.cloudconvert.com/v2/import/url", json=response_json)
task = self.cloudconvert.Task.create(operation="import/url", payload=new_import_url_task)
self.assertEqual(first=task['id'], second="2f901289-c9fe-4c89-9c4b-98be526bdfbf")
print(m.called)
def testWaitTask(self):
"""
Wait Task
:return:
"""
print("testcase for waiting task..")
with requests_mock.mock() as m:
with open("{}/{}".format(self.responses_path, "task.json")) as f:
response_json = json.load(f)
task_id = "4c80f1ae-5b3a-43d5-bb58-1a5c4eb4e46b"
m.get("https://api.sandbox.cloudconvert.com/v2/tasks/{}/wait".format(task_id), json=response_json)
task = self.cloudconvert.Task.wait(id=task_id)
self.assertEqual(first=task['id'], second="4c80f1ae-5b3a-43d5-bb58-1a5c4eb4e46b")
print(m.called)
def testShowTask(self):
"""
Show Task
:return:
"""
print("testcase for show task..")
with requests_mock.mock() as m:
with open("{}/{}".format(self.responses_path, "task.json")) as f:
response_json = json.load(f)
task_id = "4c80f1ae-5b3a-43d5-bb58-1a5c4eb4e46b"
m.get("https://api.sandbox.cloudconvert.com/v2/tasks/{}".format(task_id), json=response_json)
task = self.cloudconvert.Task.show(id=task_id)
self.assertEqual(first=task['id'], second="4c80f1ae-5b3a-43d5-bb58-1a5c4eb4e46b")
print(m.called)
def testListTask(self):
"""
List Task
:return:
"""
print("testcase for listing tasks..")
with requests_mock.mock() as m:
with open("{}/{}".format(self.responses_path, "tasks.json")) as f:
response_json = json.load(f)
m.get("https://api.sandbox.cloudconvert.com/v2/tasks", json=response_json)
tasks = self.cloudconvert.Task.all()
self.assertEqual(isinstance(tasks, list), True)
print(m.called)
def testRetryTask(self):
"""
Retry Task
:return:
"""
print("testcase for retrying a task")
with requests_mock.mock() as m:
with open("{}/{}".format(self.responses_path, "retry.json")) as f:
response_json = json.load(f)
task_id = "66bd538e-1500-4e4b-b908-0e429b357e77"
m.post("https://api.sandbox.cloudconvert.com/v2/tasks/{}/retry".format(task_id), json=response_json)
tasks = self.cloudconvert.Task.retry(task_id)
self.assertEqual(tasks["retry_of_task_id"], task_id)
print(m.called)
def testDeleteTask(self):
"""
Delete Task
:return:
"""
print("testcase for delete task..")
with requests_mock.mock() as m:
task_id = "4c80f1ae-5b3a-43d5-bb58-1a5c4eb4e46b"
m.delete("https://api.sandbox.cloudconvert.com/v2/tasks/{}".format(task_id), json={})
isDeleted = self.cloudconvert.Task.delete(id=task_id)
self.assertEqual(first=isDeleted, second=True)
print(m.called)
def testDownloadOutput(self):
"""
Testcase to download output file
:return:
"""
res = cloudconvert.download(filename="path/to/save/file.ext", url="url/to/file/download")
print(res)
def tearDown(self):
"""
Teardown method
:return:
"""
print("Tearing down task test cases..")
if __name__ == '__main__':
unittest.main()