X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=test.py;h=c229801246219870d99e5f8c574793dd96f1ddcc;hb=de40b4e7d6f13ee2ed65c39ba032f03d55632684;hp=5b9e2b4244cf7dcca5c8d917c7856ded88ab75c3;hpb=bfe841c557094afad2db0d2c63deadeea4ba63c6;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/test.py b/test.py index 5b9e2b4..c229801 100644 --- a/test.py +++ b/test.py @@ -101,7 +101,7 @@ class poloniexETest(unittest.TestCase): retry_call.assert_called_with(request, delay=1, tries=10, fargs=["foo"], fkwargs={'api': 'public', 'method': 'GET', 'params': {}, 'headers': None, 'body': None}, - exceptions=(market.ccxt.RequestTimeout,)) + exceptions=(market.ccxt.RequestTimeout, market.ccxt.InvalidNonce)) request.assert_not_called() with self.subTest(desc="private GET"): @@ -109,7 +109,7 @@ class poloniexETest(unittest.TestCase): retry_call.assert_called_with(request, delay=1, tries=10, fargs=["foo"], fkwargs={'api': 'private', 'method': 'GET', 'params': {}, 'headers': None, 'body': None}, - exceptions=(market.ccxt.RequestTimeout,)) + exceptions=(market.ccxt.RequestTimeout, market.ccxt.InvalidNonce)) request.assert_not_called() with self.subTest(desc="private POST regexp"): @@ -117,7 +117,7 @@ class poloniexETest(unittest.TestCase): retry_call.assert_called_with(request, delay=1, tries=10, fargs=["returnFoo"], fkwargs={'api': 'private', 'method': 'POST', 'params': {}, 'headers': None, 'body': None}, - exceptions=(market.ccxt.RequestTimeout,)) + exceptions=(market.ccxt.RequestTimeout, market.ccxt.InvalidNonce)) request.assert_not_called() with self.subTest(desc="private POST non-regexp"): @@ -125,7 +125,7 @@ class poloniexETest(unittest.TestCase): retry_call.assert_called_with(request, delay=1, tries=10, fargs=["getMarginPosition"], fkwargs={'api': 'private', 'method': 'POST', 'params': {}, 'headers': None, 'body': None}, - exceptions=(market.ccxt.RequestTimeout,)) + exceptions=(market.ccxt.RequestTimeout, market.ccxt.InvalidNonce)) request.assert_not_called() retry_call.reset_mock() request.reset_mock() @@ -1461,16 +1461,18 @@ class MarketTest(WebMockTestCase): m.ccxt.transfer_balance.side_effect = [ market.ccxt.RequestTimeout, + market.ccxt.InvalidNonce, True ] m.move_balances() self.ccxt.transfer_balance.assert_has_calls([ + mock.call("BTC", 3, "exchange", "margin"), mock.call("BTC", 3, "exchange", "margin"), mock.call("BTC", 3, "exchange", "margin") ]) - self.assertEqual(2, fetch_balances.call_count) + self.assertEqual(3, fetch_balances.call_count) m.report.log_error.assert_called_with(mock.ANY, message="Retrying", exception=mock.ANY) - self.assertEqual(2, m.report.log_move_balances.call_count) + self.assertEqual(3, m.report.log_move_balances.call_count) self.ccxt.transfer_balance.reset_mock() m.report.reset_mock() @@ -3213,6 +3215,48 @@ class OrderTest(WebMockTestCase): self.assertEqual(5, self.m.report.log_error.call_count) self.m.report.log_error.assert_called_with(mock.ANY, message="Giving up Order(buy long 0.00096060 ETH at 0.1 BTC [pending])", exception=mock.ANY) + self.m.reset_mock() + with self.subTest(invalid_nonce=True): + with self.subTest(retry_success=True): + order = portfolio.Order("buy", portfolio.Amount("ETH", "0.001"), + D("0.1"), "BTC", "long", self.m, "trade") + self.m.ccxt.create_order.side_effect = [ + portfolio.InvalidNonce, + portfolio.InvalidNonce, + { "id": 123 }, + ] + order.run() + self.m.ccxt.create_order.assert_has_calls([ + mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), + mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), + mock.call('ETH/BTC', 'limit', 'buy', D('0.0010'), account='exchange', price=D('0.1')), + ]) + self.assertEqual(3, self.m.ccxt.create_order.call_count) + self.assertEqual(3, order.tries) + self.m.report.log_error.assert_called() + self.assertEqual(2, self.m.report.log_error.call_count) + self.m.report.log_error.assert_called_with(mock.ANY, message="Retrying after invalid nonce", exception=mock.ANY) + self.assertEqual(123, order.id) + + self.m.reset_mock() + with self.subTest(retry_success=False): + order = portfolio.Order("buy", portfolio.Amount("ETH", "0.001"), + D("0.1"), "BTC", "long", self.m, "trade") + self.m.ccxt.create_order.side_effect = [ + portfolio.InvalidNonce, + portfolio.InvalidNonce, + portfolio.InvalidNonce, + portfolio.InvalidNonce, + portfolio.InvalidNonce, + ] + order.run() + self.assertEqual(5, self.m.ccxt.create_order.call_count) + self.assertEqual(5, order.tries) + self.m.report.log_error.assert_called() + self.assertEqual(5, self.m.report.log_error.call_count) + self.m.report.log_error.assert_called_with(mock.ANY, message="Giving up Order(buy long 0.00100000 ETH at 0.1 BTC [pending]) after invalid nonce", exception=mock.ANY) + self.assertEqual("error", order.status) + self.m.reset_mock() with self.subTest(request_timeout=True): order = portfolio.Order("buy", portfolio.Amount("ETH", "0.001"),