Skip to content

Commit cb80307

Browse files
Update tests (#397)
1 parent edd0c17 commit cb80307

2 files changed

Lines changed: 103 additions & 155 deletions

File tree

tests/spot/test_spot_base_api.py

Lines changed: 60 additions & 88 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
@@ -124,8 +123,9 @@ async def check() -> None:
124123

125124
@pytest.mark.spot
126125
@pytest.mark.spot_auth
127-
@pytest.mark.timeout(120)
126+
@pytest.mark.parametrize("report", ["trades", "ledgers"])
128127
def test_spot_rest_async_client_post_report(
128+
report: str,
129129
spot_api_key: str,
130130
spot_secret_key: str,
131131
) -> None:
@@ -137,95 +137,67 @@ def test_spot_rest_async_client_post_report(
137137
async def check() -> None:
138138
client = SpotAsyncClient(spot_api_key, spot_secret_key)
139139

140-
first_of_current_month = int(datetime.now().replace(day=1).timestamp())
141140
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)
141+
export_descr = f"{report}-export-{random.randint(0, 10000)}"
142+
response = await client.request(
143+
"POST",
144+
"/0/private/AddExport",
145+
params={
146+
"report": report,
147+
"description": export_descr,
148+
},
149+
)
150+
assert is_not_error(response)
151+
assert "id" in response
152+
sleep(2)
153+
154+
status = await client.request(
155+
"POST",
156+
"/0/private/ExportStatus",
157+
params={"report": report},
158+
)
159+
assert isinstance(status, list)
160+
sleep(5)
161+
162+
result = await client.request(
163+
"POST",
164+
"/0/private/RetrieveExport",
165+
params={"id": response["id"]},
166+
timeout=30,
167+
return_raw=True,
168+
)
169+
170+
with tempfile.TemporaryDirectory() as tmp_dir:
171+
file_path = Path(tmp_dir) / f"{export_descr}.zip"
172+
173+
with file_path.open("wb") as file:
174+
async for chunk in result.content.iter_chunked(1024):
175+
file.write(chunk)
176+
177+
status = await client.request(
178+
"POST",
179+
"/0/private/ExportStatus",
180+
params={"report": report},
181+
)
182+
assert isinstance(status, list)
183+
for response in status:
184+
if response.get("delete"):
185+
# ignore already deleted reports
186+
continue
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: 43 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ def test_get_trade_volume(spot_auth_user: User) -> None:
311311
@pytest.mark.spot
312312
@pytest.mark.spot_auth
313313
@pytest.mark.spot_user
314-
@pytest.mark.timeout(120)
315-
def test_request_save_export_report(spot_auth_user: User) -> None:
314+
@pytest.mark.parametrize("report", ["trades", "ledgers"])
315+
def test_request_save_export_report(report: str, spot_auth_user: User) -> None:
316316
"""
317317
Checks the ``save_export_report`` function by requesting an
318318
report and saving them.
@@ -327,74 +327,50 @@ def test_request_save_export_report(spot_auth_user: User) -> None:
327327
)
328328

329329
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)
330+
export_descr = f"{report}-export-{random.randint(0, 10000)}"
331+
response = spot_auth_user.request_export_report(
332+
report=report,
333+
description=export_descr,
334+
fields="all",
335+
format_="CSV",
336+
starttm=first_of_current_month,
337+
endtm=first_of_current_month + 100 * 100,
338+
)
339+
assert is_not_error(response)
340+
assert "id" in response
341+
sleep(2)
342+
343+
status = spot_auth_user.get_export_report_status(report=report)
344+
assert isinstance(status, list)
345+
sleep(5)
346+
347+
result = spot_auth_user.retrieve_export(id_=response["id"], timeout=30)
348+
349+
with tempfile.TemporaryDirectory() as tmp_dir:
350+
file_path: Path = Path(tmp_dir) / f"{export_descr}.zip"
351+
352+
with file_path.open("wb") as file:
353+
for chunk in result.iter_content(chunk_size=512):
354+
if chunk:
355+
file.write(chunk)
356+
357+
status = spot_auth_user.get_export_report_status(report=report)
358+
assert isinstance(status, list)
359+
for response in status:
360+
if response.get("delete"):
361+
# ignore already deleted reports
362+
continue
367363
assert "id" in response
364+
with suppress(Exception):
365+
assert isinstance(
366+
spot_auth_user.delete_export_report(
367+
id_=response["id"],
368+
type_="delete",
369+
),
370+
dict,
371+
)
368372
sleep(2)
369373

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-
398374

399375
@pytest.mark.spot
400376
@pytest.mark.spot_auth

0 commit comments

Comments
 (0)