1
0
fin-depo/test/test_kucoin.py
Jon Michael Aanes bcae3d4e29
Some checks failed
Test Python / Test (push) Failing after 26s
Ruff
2024-07-27 03:14:12 +02:00

125 lines
3.3 KiB
Python

import datetime
from decimal import Decimal
import fin_defs
import pytest
import fin_depo
from . import secrets
TEST_MARKET_ORDERS = True
needs_secrets = pytest.mark.skipif(
not secrets.KUCOIN_KEY,
reason='Secret kucoin_USERNAME required',
)
fin_depo.defi_kucoin.logger.setLevel('INFO')
NOW = datetime.datetime.now(tz=datetime.UTC)
@needs_secrets
def test_get_depo():
"""Can inspect kucoin depository."""
fetcher = fin_depo.defi_kucoin.KucoinDepoFetcher(
secrets.KUCOIN_KEY,
secrets.KUCOIN_SECRET,
secrets.KUCOIN_PASS,
)
depo = fetcher.get_depo()
assert isinstance(depo, fin_depo.data.DepoGroup)
for nested_depo in depo.nested:
assert isinstance(nested_depo, fin_depo.data.DepoSingle)
@needs_secrets
def test_place_market_order_requires_allow_trades():
"""Client fails if allow_trades is not enabled when doing market orders."""
fetcher = fin_depo.defi_kucoin.KucoinDepoFetcher(
secrets.KUCOIN_KEY,
secrets.KUCOIN_SECRET,
secrets.KUCOIN_PASS,
allow_trades=False,
)
with pytest.raises(PermissionError) as m:
fetcher.place_market_order(fin_defs.MPC, Decimal(1), fin_defs.USDT)
assert 'KucoinDepoFetcher.allow_trades is not enabled: Cannot make trades' in str(m)
@needs_secrets
def test_place_buy_side_order():
"""Client can place buy side market orders."""
fetcher = fin_depo.defi_kucoin.KucoinDepoFetcher(
secrets.KUCOIN_KEY,
secrets.KUCOIN_SECRET,
secrets.KUCOIN_PASS,
allow_trades=TEST_MARKET_ORDERS,
)
input_amount = Decimal('0.1')
order_details = fetcher.place_market_order(
fin_defs.USDT,
input_amount,
fin_defs.MPC,
)
assert order_details is not None
assert order_details.order_id is not None
assert order_details.raw_order_details is not None
assert order_details.input_asset == fin_defs.USDT
assert order_details.output_asset == fin_defs.MPC
assert order_details.input_amount <= input_amount
assert order_details.output_amount >= 0
assert order_details.input_amount != order_details.output_amount
assert order_details.fee_asset == fin_defs.USDT
assert order_details.fee_amount <= Decimal('0.0002')
assert NOW <= order_details.executed_time <= NOW + datetime.timedelta(minutes=10)
@needs_secrets
def test_place_sell_side_order():
"""Client can place sell side market orders."""
fetcher = fin_depo.defi_kucoin.KucoinDepoFetcher(
secrets.KUCOIN_KEY,
secrets.KUCOIN_SECRET,
secrets.KUCOIN_PASS,
allow_trades=TEST_MARKET_ORDERS,
)
input_amount = Decimal('1')
order_details = fetcher.place_market_order(
fin_defs.MPC,
input_amount,
fin_defs.USDT,
)
assert order_details is not None
assert order_details.order_id is not None
assert order_details.raw_order_details is not None
assert order_details.input_asset == fin_defs.MPC
assert order_details.output_asset == fin_defs.USDT
assert order_details.input_amount <= input_amount
assert order_details.output_amount >= 0
assert order_details.input_amount != order_details.output_amount
assert order_details.fee_asset == fin_defs.USDT
assert order_details.fee_amount is not None
assert NOW <= order_details.executed_time <= NOW + datetime.timedelta(minutes=10)