Skip to content

Commit 85957f2

Browse files
Update tests
1 parent edd0c17 commit 85957f2

File tree

2 files changed

+100
-153
lines changed

2 files changed

+100
-153
lines changed

tests/spot/test_spot_base_api.py

Lines changed: 59 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import tempfile
1414
from asyncio import run
1515
from contextlib import suppress
16-
from datetime import datetime
1716
from pathlib import Path
1817
from time import sleep
1918
from typing import TYPE_CHECKING
@@ -125,7 +124,10 @@ async def check() -> None:
125124
@pytest.mark.spot
126125
@pytest.mark.spot_auth
127126
@pytest.mark.timeout(120)
127+
@pytest.mark.flaky(retries=0)
128+
@pytest.mark.parametrize("report", ["trades", "ledgers"])
128129
def test_spot_rest_async_client_post_report(
130+
report: str,
129131
spot_api_key: str,
130132
spot_secret_key: str,
131133
) -> None:
@@ -137,95 +139,65 @@ def test_spot_rest_async_client_post_report(
137139
async def check() -> None:
138140
client = SpotAsyncClient(spot_api_key, spot_secret_key)
139141

140-
first_of_current_month = int(datetime.now().replace(day=1).timestamp())
141142
try:
142-
for report in ("trades", "ledgers"):
143-
if report == "trades":
144-
fields = [
145-
"ordertxid",
146-
"time",
147-
"ordertype",
148-
"price",
149-
"cost",
150-
"fee",
151-
"vol",
152-
"margin",
153-
"misc",
154-
"ledgers",
155-
]
156-
else:
157-
fields = [
158-
"refid",
159-
"time",
160-
"type",
161-
"aclass",
162-
"asset",
163-
"amount",
164-
"fee",
165-
"balance",
166-
]
167-
168-
export_descr = f"{report}-export-{random.randint(0, 10000)}"
169-
response = await client.request(
170-
"POST",
171-
"/0/private/AddExport",
172-
params={
173-
"format": "CSV",
174-
"fields": fields,
175-
"report": report,
176-
"description": export_descr,
177-
"endtm": first_of_current_month + 100 * 100,
178-
},
179-
timeout=30,
180-
)
181-
assert is_not_error(response)
143+
export_descr = f"{report}-export-{random.randint(0, 10000)}"
144+
response = await client.request(
145+
"POST",
146+
"/0/private/AddExport",
147+
params={
148+
"report": report,
149+
"description": export_descr,
150+
},
151+
timeout=30,
152+
)
153+
assert is_not_error(response)
154+
assert "id" in response
155+
sleep(2)
156+
157+
status = await client.request(
158+
"POST",
159+
"/0/private/ExportStatus",
160+
params={"report": report},
161+
)
162+
assert isinstance(status, list)
163+
sleep(5)
164+
165+
result = await client.request(
166+
"POST",
167+
"/0/private/RetrieveExport",
168+
params={"id": response["id"]},
169+
timeout=30,
170+
return_raw=True,
171+
)
172+
173+
with tempfile.TemporaryDirectory() as tmp_dir:
174+
file_path = Path(tmp_dir) / f"{export_descr}.zip"
175+
176+
with file_path.open("wb") as file:
177+
async for chunk in result.content.iter_chunked(1024):
178+
file.write(chunk)
179+
180+
status = await client.request(
181+
"POST",
182+
"/0/private/ExportStatus",
183+
params={"report": report},
184+
)
185+
assert isinstance(status, list)
186+
for response in status:
182187
assert "id" in response
188+
with suppress(Exception):
189+
assert isinstance(
190+
await client.request(
191+
"POST",
192+
"/0/private/RemoveExport",
193+
params={
194+
"id": response["id"],
195+
"type": "delete",
196+
},
197+
),
198+
dict,
199+
)
183200
sleep(2)
184-
185-
status = await client.request(
186-
"POST",
187-
"/0/private/ExportStatus",
188-
params={"report": report},
189-
)
190-
assert isinstance(status, list)
191-
sleep(5)
192-
193-
result = await client.request(
194-
"POST",
195-
"/0/private/RetrieveExport",
196-
params={"id": response["id"]},
197-
timeout=30,
198-
return_raw=True,
199-
)
200-
201-
with tempfile.TemporaryDirectory() as tmp_dir:
202-
file_path = Path(tmp_dir) / f"{export_descr}.zip"
203-
204-
with file_path.open("wb") as file:
205-
async for chunk in result.content.iter_chunked(1024):
206-
file.write(chunk)
207-
208-
status = await client.request(
209-
"POST",
210-
"/0/private/ExportStatus",
211-
params={"report": report},
212-
)
213-
assert isinstance(status, list)
214-
for response in status:
215-
assert "id" in response
216-
with suppress(Exception):
217-
assert isinstance(
218-
await client.request(
219-
"POST",
220-
"/0/private/RemoveExport",
221-
params={
222-
"id": response["id"],
223-
"type": "delete",
224-
},
225-
),
226-
dict,
227-
)
228-
sleep(2)
229201
finally:
230202
await client.close()
231203

tests/spot/test_spot_user.py

Lines changed: 41 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,8 @@ def test_get_trade_volume(spot_auth_user: User) -> None:
312312
@pytest.mark.spot_auth
313313
@pytest.mark.spot_user
314314
@pytest.mark.timeout(120)
315-
def test_request_save_export_report(spot_auth_user: User) -> None:
315+
@pytest.mark.parametrize("report", ["trades", "ledgers"])
316+
def test_request_save_export_report(report: str, spot_auth_user: User) -> None:
316317
"""
317318
Checks the ``save_export_report`` function by requesting an
318319
report and saving them.
@@ -327,74 +328,48 @@ def test_request_save_export_report(spot_auth_user: User) -> None:
327328
)
328329

329330
first_of_current_month = int(datetime.now().replace(day=1).timestamp())
330-
for report in ("trades", "ledgers"):
331-
if report == "trades":
332-
fields = [
333-
"ordertxid",
334-
"time",
335-
"ordertype",
336-
"price",
337-
"cost",
338-
"fee",
339-
"vol",
340-
"margin",
341-
"misc",
342-
"ledgers",
343-
]
344-
else:
345-
fields = [
346-
"refid",
347-
"time",
348-
"type",
349-
"aclass",
350-
"asset",
351-
"amount",
352-
"fee",
353-
"balance",
354-
]
355-
356-
export_descr = f"{report}-export-{random.randint(0, 10000)}"
357-
response = spot_auth_user.request_export_report(
358-
report=report,
359-
description=export_descr,
360-
fields=fields,
361-
format_="CSV",
362-
starttm=first_of_current_month,
363-
endtm=first_of_current_month + 100 * 100,
364-
timeout=30,
365-
)
366-
assert is_not_error(response)
331+
export_descr = f"{report}-export-{random.randint(0, 10000)}"
332+
response = spot_auth_user.request_export_report(
333+
report=report,
334+
description=export_descr,
335+
fields="all",
336+
format_="CSV",
337+
starttm=first_of_current_month,
338+
endtm=first_of_current_month + 100 * 100,
339+
timeout=30,
340+
)
341+
assert is_not_error(response)
342+
assert "id" in response
343+
sleep(2)
344+
345+
status = spot_auth_user.get_export_report_status(report=report)
346+
assert isinstance(status, list)
347+
sleep(5)
348+
349+
result = spot_auth_user.retrieve_export(id_=response["id"], timeout=30)
350+
351+
with tempfile.TemporaryDirectory() as tmp_dir:
352+
file_path: Path = Path(tmp_dir) / f"{export_descr}.zip"
353+
354+
with file_path.open("wb") as file:
355+
for chunk in result.iter_content(chunk_size=512):
356+
if chunk:
357+
file.write(chunk)
358+
359+
status = spot_auth_user.get_export_report_status(report=report)
360+
assert isinstance(status, list)
361+
for response in status:
367362
assert "id" in response
363+
with suppress(Exception):
364+
assert isinstance(
365+
spot_auth_user.delete_export_report(
366+
id_=response["id"],
367+
type_="delete",
368+
),
369+
dict,
370+
)
368371
sleep(2)
369372

370-
status = spot_auth_user.get_export_report_status(report=report)
371-
assert isinstance(status, list)
372-
sleep(5)
373-
374-
result = spot_auth_user.retrieve_export(id_=response["id"], timeout=30)
375-
376-
with tempfile.TemporaryDirectory() as tmp_dir:
377-
file_path: Path = Path(tmp_dir) / f"{export_descr}.zip"
378-
379-
with file_path.open("wb") as file:
380-
for chunk in result.iter_content(chunk_size=512):
381-
if chunk:
382-
file.write(chunk)
383-
384-
status = spot_auth_user.get_export_report_status(report=report)
385-
assert isinstance(status, list)
386-
for response in status:
387-
assert "id" in response
388-
with suppress(Exception):
389-
assert isinstance(
390-
spot_auth_user.delete_export_report(
391-
id_=response["id"],
392-
type_="delete",
393-
),
394-
dict,
395-
)
396-
sleep(2)
397-
398373

399374
@pytest.mark.spot
400375
@pytest.mark.spot_auth

0 commit comments

Comments
 (0)