-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_async_full.py
More file actions
281 lines (257 loc) · 10.1 KB
/
Copy pathtest_async_full.py
File metadata and controls
281 lines (257 loc) · 10.1 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""Comprehensive async client tests covering all API methods."""
from __future__ import annotations
from typing import Any
import httpx
import respx
from cloudlayerio.client import AsyncCloudLayer
from cloudlayerio.types.asset import Asset
from cloudlayerio.types.job import Job
from cloudlayerio.types.storage import StorageDetail, StorageListItem
from cloudlayerio.types.template import PublicTemplate
class TestAsyncConversionAll:
@respx.mock
async def test_html_to_pdf(self) -> None:
respx.post("https://api.cloudlayer.io/v2/html/pdf").mock(
return_value=httpx.Response(
200,
json={
"id": "j1",
"uid": "u1",
"status": "success",
"timestamp": 1000,
},
headers={"content-type": "application/json"},
)
)
async with AsyncCloudLayer("key", api_version="v2") as client:
result = await client.html_to_pdf({"html": "PGh0bWw+"})
assert isinstance(result.data, Job)
@respx.mock
async def test_html_to_image(self) -> None:
respx.post("https://api.cloudlayer.io/v2/html/image").mock(
return_value=httpx.Response(
200,
json={
"id": "j1",
"uid": "u1",
"status": "success",
"timestamp": 1000,
},
headers={"content-type": "application/json"},
)
)
async with AsyncCloudLayer("key", api_version="v2") as client:
result = await client.html_to_image({"html": "PGh0bWw+"})
assert isinstance(result.data, Job)
@respx.mock
async def test_url_to_image(self) -> None:
respx.post("https://api.cloudlayer.io/v2/url/image").mock(
return_value=httpx.Response(
200,
json={
"id": "j1",
"uid": "u1",
"status": "success",
"timestamp": 1000,
},
headers={"content-type": "application/json"},
)
)
async with AsyncCloudLayer("key", api_version="v2") as client:
result = await client.url_to_image({"url": "https://example.com"})
assert isinstance(result.data, Job)
@respx.mock
async def test_template_to_pdf(self) -> None:
respx.post("https://api.cloudlayer.io/v2/template/pdf").mock(
return_value=httpx.Response(
200,
json={
"id": "j1",
"uid": "u1",
"status": "success",
"timestamp": 1000,
},
headers={"content-type": "application/json"},
)
)
async with AsyncCloudLayer("key", api_version="v2") as client:
result = await client.template_to_pdf({"template_id": "t1"})
assert isinstance(result.data, Job)
@respx.mock
async def test_template_to_image(self) -> None:
respx.post("https://api.cloudlayer.io/v2/template/image").mock(
return_value=httpx.Response(
200,
json={
"id": "j1",
"uid": "u1",
"status": "success",
"timestamp": 1000,
},
headers={"content-type": "application/json"},
)
)
async with AsyncCloudLayer("key", api_version="v2") as client:
result = await client.template_to_image({"template_id": "t1"})
assert isinstance(result.data, Job)
@respx.mock
async def test_docx_to_pdf(self) -> None:
respx.post("https://api.cloudlayer.io/v2/docx/pdf").mock(
return_value=httpx.Response(
200,
json={
"id": "j1",
"uid": "u1",
"status": "success",
"timestamp": 1000,
},
headers={"content-type": "application/json"},
)
)
async with AsyncCloudLayer("key", api_version="v2") as client:
result = await client.docx_to_pdf({"file": b"fake-bytes"})
assert isinstance(result.data, Job)
@respx.mock
async def test_docx_to_html(self) -> None:
respx.post("https://api.cloudlayer.io/v2/docx/html").mock(
return_value=httpx.Response(
200,
json={
"id": "j1",
"uid": "u1",
"status": "success",
"timestamp": 1000,
},
headers={"content-type": "application/json"},
)
)
async with AsyncCloudLayer("key", api_version="v2") as client:
result = await client.docx_to_html({"file": b"fake-bytes"})
assert isinstance(result.data, Job)
@respx.mock
async def test_pdf_to_docx(self) -> None:
respx.post("https://api.cloudlayer.io/v2/pdf/docx").mock(
return_value=httpx.Response(
200,
json={
"id": "j1",
"uid": "u1",
"status": "success",
"timestamp": 1000,
},
headers={"content-type": "application/json"},
)
)
async with AsyncCloudLayer("key", api_version="v2") as client:
result = await client.pdf_to_docx({"file": b"fake-bytes"})
assert isinstance(result.data, Job)
@respx.mock
async def test_merge_pdfs(self) -> None:
respx.post("https://api.cloudlayer.io/v2/pdf/merge").mock(
return_value=httpx.Response(
200,
json={
"id": "j1",
"uid": "u1",
"status": "success",
"timestamp": 1000,
},
headers={"content-type": "application/json"},
)
)
async with AsyncCloudLayer("key", api_version="v2") as client:
result = await client.merge_pdfs({"url": "https://example.com/1.pdf"})
assert isinstance(result.data, Job)
class TestAsyncDataAll:
@respx.mock
async def test_list_assets(self) -> None:
respx.get("https://api.cloudlayer.io/v2/assets").mock(
return_value=httpx.Response(
200,
json=[
{"uid": "u1", "fileId": "f1"},
],
)
)
async with AsyncCloudLayer("key", api_version="v2") as client:
assets = await client.list_assets()
assert len(assets) == 1
assert isinstance(assets[0], Asset)
@respx.mock
async def test_get_asset(self) -> None:
respx.get("https://api.cloudlayer.io/v2/assets/a1").mock(
return_value=httpx.Response(200, json={"uid": "u1", "fileId": "f1"})
)
async with AsyncCloudLayer("key", api_version="v2") as client:
asset = await client.get_asset("a1")
assert isinstance(asset, Asset)
@respx.mock
async def test_list_storage(self) -> None:
respx.get("https://api.cloudlayer.io/v2/storage").mock(
return_value=httpx.Response(200, json=[{"id": "s1", "title": "T"}])
)
async with AsyncCloudLayer("key", api_version="v2") as client:
items = await client.list_storage()
assert len(items) == 1
assert isinstance(items[0], StorageListItem)
@respx.mock
async def test_get_storage(self) -> None:
respx.get("https://api.cloudlayer.io/v2/storage/s1").mock(
return_value=httpx.Response(
200, json={"data": "d", "id": "s1", "title": "T", "uid": "u1"}
)
)
async with AsyncCloudLayer("key", api_version="v2") as client:
detail = await client.get_storage("s1")
assert isinstance(detail, StorageDetail)
@respx.mock
async def test_add_storage(self) -> None:
respx.post("https://api.cloudlayer.io/v2/storage").mock(
return_value=httpx.Response(
200, json={"data": "d", "id": "s-new", "title": "T", "uid": "u1"}
)
)
async with AsyncCloudLayer("key", api_version="v2") as client:
result = await client.add_storage(
{
"title": "T",
"region": "r",
"access_key_id": "a",
"secret_access_key": "s",
"bucket": "b",
}
)
assert isinstance(result, StorageDetail)
@respx.mock
async def test_delete_storage(self) -> None:
respx.delete("https://api.cloudlayer.io/v2/storage/s1").mock(
return_value=httpx.Response(204)
)
async with AsyncCloudLayer("key", api_version="v2") as client:
result = await client.delete_storage("s1")
assert result is None
@respx.mock
async def test_get_account(self, sample_account: dict[str, Any]) -> None:
respx.get("https://api.cloudlayer.io/v2/account").mock(
return_value=httpx.Response(200, json=sample_account)
)
async with AsyncCloudLayer("key", api_version="v2") as client:
account = await client.get_account()
@respx.mock
async def test_list_templates(self) -> None:
respx.get("https://api.cloudlayer.io/v2/templates").mock(
return_value=httpx.Response(200, json=[{"id": "t1"}])
)
async with AsyncCloudLayer("key", api_version="v2") as client:
templates = await client.list_templates()
assert len(templates) == 1
assert isinstance(templates[0], PublicTemplate)
@respx.mock
async def test_get_template(self) -> None:
respx.get("https://api.cloudlayer.io/v2/template/t1").mock(
return_value=httpx.Response(200, json={"id": "t1", "name": "Test"})
)
async with AsyncCloudLayer("key", api_version="v2") as client:
template = await client.get_template("t1")
assert template.id == "t1"