Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 60 additions & 88 deletions tests/spot/test_spot_base_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import tempfile
from asyncio import run
from contextlib import suppress
from datetime import datetime
from pathlib import Path
from time import sleep
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -124,8 +123,9 @@ async def check() -> None:

@pytest.mark.spot
@pytest.mark.spot_auth
@pytest.mark.timeout(120)
@pytest.mark.parametrize("report", ["trades", "ledgers"])
def test_spot_rest_async_client_post_report(
report: str,
spot_api_key: str,
spot_secret_key: str,
) -> None:
Expand All @@ -137,95 +137,67 @@ def test_spot_rest_async_client_post_report(
async def check() -> None:
client = SpotAsyncClient(spot_api_key, spot_secret_key)

first_of_current_month = int(datetime.now().replace(day=1).timestamp())
try:
for report in ("trades", "ledgers"):
if report == "trades":
fields = [
"ordertxid",
"time",
"ordertype",
"price",
"cost",
"fee",
"vol",
"margin",
"misc",
"ledgers",
]
else:
fields = [
"refid",
"time",
"type",
"aclass",
"asset",
"amount",
"fee",
"balance",
]

export_descr = f"{report}-export-{random.randint(0, 10000)}"
response = await client.request(
"POST",
"/0/private/AddExport",
params={
"format": "CSV",
"fields": fields,
"report": report,
"description": export_descr,
"endtm": first_of_current_month + 100 * 100,
},
timeout=30,
)
assert is_not_error(response)
export_descr = f"{report}-export-{random.randint(0, 10000)}"
response = await client.request(
"POST",
"/0/private/AddExport",
params={
"report": report,
"description": export_descr,
},
)
assert is_not_error(response)
assert "id" in response
sleep(2)

status = await client.request(
"POST",
"/0/private/ExportStatus",
params={"report": report},
)
assert isinstance(status, list)
sleep(5)

result = await client.request(
"POST",
"/0/private/RetrieveExport",
params={"id": response["id"]},
timeout=30,
return_raw=True,
)

with tempfile.TemporaryDirectory() as tmp_dir:
file_path = Path(tmp_dir) / f"{export_descr}.zip"

with file_path.open("wb") as file:
async for chunk in result.content.iter_chunked(1024):
file.write(chunk)

status = await client.request(
"POST",
"/0/private/ExportStatus",
params={"report": report},
)
assert isinstance(status, list)
for response in status:
if response.get("delete"):
# ignore already deleted reports
continue
assert "id" in response
with suppress(Exception):
assert isinstance(
await client.request(
"POST",
"/0/private/RemoveExport",
params={
"id": response["id"],
"type": "delete",
},
),
dict,
)
sleep(2)

status = await client.request(
"POST",
"/0/private/ExportStatus",
params={"report": report},
)
assert isinstance(status, list)
sleep(5)

result = await client.request(
"POST",
"/0/private/RetrieveExport",
params={"id": response["id"]},
timeout=30,
return_raw=True,
)

with tempfile.TemporaryDirectory() as tmp_dir:
file_path = Path(tmp_dir) / f"{export_descr}.zip"

with file_path.open("wb") as file:
async for chunk in result.content.iter_chunked(1024):
file.write(chunk)

status = await client.request(
"POST",
"/0/private/ExportStatus",
params={"report": report},
)
assert isinstance(status, list)
for response in status:
assert "id" in response
with suppress(Exception):
assert isinstance(
await client.request(
"POST",
"/0/private/RemoveExport",
params={
"id": response["id"],
"type": "delete",
},
),
dict,
)
sleep(2)
finally:
await client.close()

Expand Down
110 changes: 43 additions & 67 deletions tests/spot/test_spot_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ def test_get_trade_volume(spot_auth_user: User) -> None:
@pytest.mark.spot
@pytest.mark.spot_auth
@pytest.mark.spot_user
@pytest.mark.timeout(120)
def test_request_save_export_report(spot_auth_user: User) -> None:
@pytest.mark.parametrize("report", ["trades", "ledgers"])
def test_request_save_export_report(report: str, spot_auth_user: User) -> None:
"""
Checks the ``save_export_report`` function by requesting an
report and saving them.
Expand All @@ -327,74 +327,50 @@ def test_request_save_export_report(spot_auth_user: User) -> None:
)

first_of_current_month = int(datetime.now().replace(day=1).timestamp())
for report in ("trades", "ledgers"):
if report == "trades":
fields = [
"ordertxid",
"time",
"ordertype",
"price",
"cost",
"fee",
"vol",
"margin",
"misc",
"ledgers",
]
else:
fields = [
"refid",
"time",
"type",
"aclass",
"asset",
"amount",
"fee",
"balance",
]

export_descr = f"{report}-export-{random.randint(0, 10000)}"
response = spot_auth_user.request_export_report(
report=report,
description=export_descr,
fields=fields,
format_="CSV",
starttm=first_of_current_month,
endtm=first_of_current_month + 100 * 100,
timeout=30,
)
assert is_not_error(response)
export_descr = f"{report}-export-{random.randint(0, 10000)}"
response = spot_auth_user.request_export_report(
report=report,
description=export_descr,
fields="all",
format_="CSV",
starttm=first_of_current_month,
endtm=first_of_current_month + 100 * 100,
)
assert is_not_error(response)
assert "id" in response
sleep(2)

status = spot_auth_user.get_export_report_status(report=report)
assert isinstance(status, list)
sleep(5)

result = spot_auth_user.retrieve_export(id_=response["id"], timeout=30)

with tempfile.TemporaryDirectory() as tmp_dir:
file_path: Path = Path(tmp_dir) / f"{export_descr}.zip"

with file_path.open("wb") as file:
for chunk in result.iter_content(chunk_size=512):
if chunk:
file.write(chunk)

status = spot_auth_user.get_export_report_status(report=report)
assert isinstance(status, list)
for response in status:
if response.get("delete"):
# ignore already deleted reports
continue
assert "id" in response
with suppress(Exception):
assert isinstance(
spot_auth_user.delete_export_report(
id_=response["id"],
type_="delete",
),
dict,
)
sleep(2)

status = spot_auth_user.get_export_report_status(report=report)
assert isinstance(status, list)
sleep(5)

result = spot_auth_user.retrieve_export(id_=response["id"], timeout=30)

with tempfile.TemporaryDirectory() as tmp_dir:
file_path: Path = Path(tmp_dir) / f"{export_descr}.zip"

with file_path.open("wb") as file:
for chunk in result.iter_content(chunk_size=512):
if chunk:
file.write(chunk)

status = spot_auth_user.get_export_report_status(report=report)
assert isinstance(status, list)
for response in status:
assert "id" in response
with suppress(Exception):
assert isinstance(
spot_auth_user.delete_export_report(
id_=response["id"],
type_="delete",
),
dict,
)
sleep(2)


@pytest.mark.spot
@pytest.mark.spot_auth
Expand Down
Loading