Skip to content

Commit

Permalink
TST: Adjusted the tests to fit to the recent bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
AvishaiW committed Jul 12, 2018
1 parent 76d01f2 commit 5b9a8c2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
20 changes: 10 additions & 10 deletions catalyst/exchange/ccxt/ccxt_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ def _fetch_missing_order(self, dt_before, symbol):
return None, missing_order

def _handle_request_timeout(self, dt_before, asset, amount, is_buy, style,
adj_amount):
prec_amount):
"""
Check if an order was received during the timeout, if it appeared
on the orders dict return it to the user.
Expand All @@ -846,15 +846,15 @@ def _handle_request_timeout(self, dt_before, asset, amount, is_buy, style,
:param amount: float
:param is_buy: Bool
:param style:
:param adj_amount: int
:param prec_amount: int
:return: missing_order: Order/ None
"""
symbol = asset.asset_name.replace(' ', '')
missing_order_id, missing_order = self._fetch_missing_order(
dt_before=dt_before, symbol=symbol)

if missing_order_id:
final_amount = adj_amount if amount > 0 else -adj_amount
final_amount = prec_amount if amount > 0 else -prec_amount
missing_order = Order(
dt=dt_before,
asset=asset,
Expand Down Expand Up @@ -899,14 +899,14 @@ def create_order(self, asset, amount, is_buy, style):
)

adj_amount = round(abs(amount), asset.decimals)
adj_amount = self.api.amount_to_precision(symbol, adj_amount)
prec_amount = self.api.amount_to_precision(symbol, adj_amount)
before_order_dt = pd.Timestamp.utcnow()
try:
result = self.api.create_order(
symbol=symbol,
type=order_type,
side=side,
amount=adj_amount,
amount=prec_amount,
price=price
)
except InvalidOrder as e:
Expand All @@ -926,7 +926,7 @@ def create_order(self, asset, amount, is_buy, style):
retry_exceptions=(RequestTimeout, ExchangeError),
cleanup=lambda: log.warn('Checking missing order again..'),
args=(
before_order_dt, asset, amount, is_buy, style, adj_amount
before_order_dt, asset, amount, is_buy, style, prec_amount
)
)
if missing_order is None:
Expand All @@ -951,7 +951,7 @@ def create_order(self, asset, amount, is_buy, style):
raise ExchangeRequestError(error=e)

exchange_amount = None
if 'amount' in result and result['amount'] != adj_amount:
if 'amount' in result and result['amount'] != prec_amount:
exchange_amount = result['amount']

elif 'info' in result:
Expand All @@ -961,15 +961,15 @@ def create_order(self, asset, amount, is_buy, style):
if exchange_amount:
log.info(
'order amount adjusted by {} from {} to {}'.format(
self.name, adj_amount, exchange_amount
self.name, prec_amount, exchange_amount
)
)
adj_amount = exchange_amount
prec_amount = exchange_amount

if 'info' not in result:
raise ValueError('cannot use order without info attribute')

final_amount = adj_amount if side == 'buy' else -adj_amount
final_amount = prec_amount if side == 'buy' else -prec_amount
order_id = result['id']
order = Order(
dt=pd.Timestamp.utcnow(),
Expand Down
41 changes: 26 additions & 15 deletions tests/exchange/test_ccxt.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from logbook import Logger
from mock import patch, create_autospec, MagicMock
from mock import patch, create_autospec, MagicMock, Mock
import pandas as pd

from ccxt.base.errors import RequestTimeout

from catalyst.exchange.exchange_errors import ExchangeRequestError
# from catalyst.exchange.utils.stats_utils import set_print_settings
from .base import BaseExchangeTestCase
from catalyst.exchange.ccxt.ccxt_exchange import CCXT
from catalyst.exchange.exchange_execution import ExchangeLimitOrder
Expand Down Expand Up @@ -158,9 +157,9 @@ def compare_orders(self, observed, expected):
:return: bool
"""
return observed.id == expected.id and \
observed.amount == expected.amount and \
observed.asset == expected.asset and \
observed.limit == expected.limit
observed.amount == expected.amount and \
observed.asset == expected.asset and \
observed.limit == expected.limit

def test_create_order_timeout_order(self):
"""
Expand All @@ -177,7 +176,8 @@ def test_create_order_timeout_order(self):
price = 0.00254

self.exchange.api = MagicMock(
spec=[u'create_order', u'fetch_orders', u'orders', u'has'])
spec=[u'create_order', u'fetch_orders', u'orders', u'has',
u'amount_to_precision'])
self.exchange.api.create_order.side_effect = RequestTimeout

orders_dict = self.create_orders_dict(asset, self.last_order)
Expand All @@ -189,6 +189,9 @@ def test_create_order_timeout_order(self):
mock_style.get_limit_price.return_value = price
style = mock_style

self.exchange.api.amount_to_precision = Mock(
return_value=float(amount))

with patch('catalyst.exchange.ccxt.ccxt_exchange.CCXT.get_symbol') as \
mock_symbol:
mock_symbol.return_value = 'ETH/USDT'
Expand Down Expand Up @@ -222,7 +225,7 @@ def test_create_order_timeout_open(self):

self.exchange.api = MagicMock(
spec=[u'create_order', u'fetch_open_orders',
u'fetch_orders', u'orders', u'has'
u'fetch_orders', u'orders', u'has', u'amount_to_precision'
]
)
self.exchange.api.create_order.side_effect = RequestTimeout
Expand All @@ -240,6 +243,9 @@ def test_create_order_timeout_open(self):
mock_style.get_limit_price.return_value = price
style = mock_style

self.exchange.api.amount_to_precision = Mock(
return_value=float(amount))

with patch('catalyst.exchange.ccxt.ccxt_exchange.CCXT.get_symbol') as \
mock_symbol:
mock_symbol.return_value = 'ETH/USDT'
Expand Down Expand Up @@ -272,7 +278,8 @@ def test_create_order_timeout_closed(self):
price = 0.00254

self.exchange.api = MagicMock(
spec=[u'create_order', u'fetch_closed_orders', u'orders', u'has'])
spec=[u'create_order', u'fetch_closed_orders', u'orders', u'has',
u'amount_to_precision'])
self.exchange.api.create_order.side_effect = RequestTimeout

orders_dict = self.create_orders_dict(asset, self.last_order)
Expand All @@ -287,6 +294,9 @@ def test_create_order_timeout_closed(self):
mock_style.get_limit_price.return_value = price
style = mock_style

self.exchange.api.amount_to_precision = Mock(
return_value=float(amount))

with patch('catalyst.exchange.ccxt.ccxt_exchange.CCXT.get_symbol') as \
mock_symbol:
mock_symbol.return_value = 'ETH/USDT'
Expand Down Expand Up @@ -326,7 +336,8 @@ def test_create_order_timeout_trade(self):

self.exchange.api = MagicMock(
spec=[u'create_order', u'fetch_my_trades', u'has',
u'fetch_open_orders', u'orders', u'fetch_closed_orders']
u'fetch_open_orders', u'orders', u'fetch_closed_orders',
u'amount_to_precision']
)
self.exchange.api.create_order.side_effect = RequestTimeout

Expand All @@ -344,15 +355,17 @@ def test_create_order_timeout_trade(self):
mock_style.get_stop_price.return_value = stop_price
style = mock_style

self.exchange.api.amount_to_precision = Mock(
return_value=float(amount))

# check the case there are no new trades and an exception is raised
with patch('catalyst.exchange.ccxt.ccxt_exchange.CCXT.get_symbol') as \
mock_symbol:
mock_symbol.return_value = 'ETH/USDT'
try:
observed_fetchTrade_None = self.exchange.create_order(
asset, amount, is_buy, style)
print(observed_fetchTrade_None)
except ExchangeRequestError:
except ExchangeRequestError as e:
pass

# check the case there are trades which form a neew order
Expand Down Expand Up @@ -384,8 +397,7 @@ def test_create_order_timeout_trade(self):
try:
observed_fetchTradeOrder_None = self.exchange.create_order(
asset, amount, is_buy, style)
print(observed_fetchTradeOrder_None)
except ExchangeRequestError:
except ExchangeRequestError as e:
pass

def test_process_order_timeout(self):
Expand Down Expand Up @@ -419,8 +431,7 @@ def test_process_order_timeout(self):
mock_trades.side_effect = RequestTimeout
try:
observed_transactions = self.exchange.process_order(order)
print(observed_transactions)
except ExchangeRequestError:
except ExchangeRequestError as e:
pass

# def test_order(self):
Expand Down

0 comments on commit 5b9a8c2

Please sign in to comment.