X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2Ftest_ccxt_wrapper.py;h=44e660ef5633688cf2f71f1be86cb40c66c82881;hb=512972fa1df14df4e208a1182096b1c51b5d38d1;hp=f07674e7927e130ce94efb680c2420243cf56c65;hpb=1d72880c097ea8259ce9cc63cfe55e6cc7516bd2;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/tests/test_ccxt_wrapper.py b/tests/test_ccxt_wrapper.py index f07674e..44e660e 100644 --- a/tests/test_ccxt_wrapper.py +++ b/tests/test_ccxt_wrapper.py @@ -1,7 +1,8 @@ -from .helper import unittest, mock, D +from .helper import limits, unittest, mock, D import requests_mock import market +@unittest.skipUnless("unit" in limits, "Unit skipped") class poloniexETest(unittest.TestCase): def setUp(self): super().setUp() @@ -109,7 +110,23 @@ class poloniexETest(unittest.TestCase): retry_call.assert_not_called() def test_order_precision(self): - self.assertEqual(8, self.s.order_precision("FOO")) + self.s.markets = { + "FOO": { + "precision": { + "price": 5, + "amount": 6, + } + }, + "BAR": { + "precision": { + "price": 7, + "amount": 8, + } + } + } + with mock.patch.object(self.s, "load_markets") as load_markets: + self.assertEqual(5, self.s.order_precision("FOO")) + load_markets.assert_called_once() def test_transfer_balance(self): with self.subTest(success=True),\ @@ -166,33 +183,6 @@ class poloniexETest(unittest.TestCase): } self.assertEqual(expected, self.s.margin_summary()) - def test_create_order(self): - with mock.patch.object(self.s, "create_exchange_order") as exchange,\ - mock.patch.object(self.s, "create_margin_order") as margin: - with self.subTest(account="unspecified"): - self.s.create_order("symbol", "type", "side", "amount", price="price", lending_rate="lending_rate", params="params") - exchange.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params") - margin.assert_not_called() - exchange.reset_mock() - margin.reset_mock() - - with self.subTest(account="exchange"): - self.s.create_order("symbol", "type", "side", "amount", account="exchange", price="price", lending_rate="lending_rate", params="params") - exchange.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params") - margin.assert_not_called() - exchange.reset_mock() - margin.reset_mock() - - with self.subTest(account="margin"): - self.s.create_order("symbol", "type", "side", "amount", account="margin", price="price", lending_rate="lending_rate", params="params") - margin.assert_called_once_with("symbol", "type", "side", "amount", lending_rate="lending_rate", price="price", params="params") - exchange.assert_not_called() - exchange.reset_mock() - margin.reset_mock() - - with self.subTest(account="unknown"), self.assertRaises(NotImplementedError): - self.s.create_order("symbol", "type", "side", "amount", account="unknown") - def test_parse_ticker(self): ticker = { "high24hr": "12", @@ -438,11 +428,13 @@ class poloniexETest(unittest.TestCase): self.assertEqual(expected_doge, result["DOGE"]) self.assertEqual(expected_btc, result["BTC"]) - def test_create_margin_order(self): - with self.assertRaises(market.ExchangeError): - self.s.create_margin_order("FOO", "market", "buy", "10") + def test_create_order(self): + with self.subTest(type="market"),\ + self.assertRaises(market.ExchangeError): + self.s.create_order("FOO", "market", "buy", "10") - with mock.patch.object(self.s, "load_markets") as load_markets,\ + with self.subTest(type="limit", account="margin"),\ + mock.patch.object(self.s, "load_markets") as load_markets,\ mock.patch.object(self.s, "privatePostMarginBuy") as margin_buy,\ mock.patch.object(self.s, "privatePostMarginSell") as margin_sell,\ mock.patch.object(self.s, "market") as market_mock,\ @@ -459,22 +451,133 @@ class poloniexETest(unittest.TestCase): ptp.return_value = D("0.1") atp.return_value = D("12") - order = self.s.create_margin_order("BTC_ETC", "margin", "buy", "12", price="0.1") + order = self.s.create_order("BTC_ETC", "limit", "buy", "12", + account="margin", price="0.1") self.assertEqual(123, order["id"]) margin_buy.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12")}) margin_sell.assert_not_called() margin_buy.reset_mock() margin_sell.reset_mock() - order = self.s.create_margin_order("BTC_ETC", "margin", "sell", "12", lending_rate="0.01", price="0.1") + order = self.s.create_order("BTC_ETC", "limit", "sell", + "12", account="margin", lending_rate="0.01", price="0.1") self.assertEqual(456, order["id"]) margin_sell.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12"), "lendingRate": "0.01"}) margin_buy.assert_not_called() - def test_create_exchange_order(self): - with mock.patch.object(market.ccxt.poloniex, "create_order") as create_order: - self.s.create_order("symbol", "type", "side", "amount", price="price", params="params") + with self.subTest(type="limit", account="exchange"),\ + mock.patch.object(self.s, "load_markets") as load_markets,\ + mock.patch.object(self.s, "privatePostBuy") as exchange_buy,\ + mock.patch.object(self.s, "privatePostSell") as exchange_sell,\ + mock.patch.object(self.s, "market") as market_mock,\ + mock.patch.object(self.s, "price_to_precision") as ptp,\ + mock.patch.object(self.s, "amount_to_precision") as atp: - create_order.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params") + exchange_buy.return_value = { + "orderNumber": 123 + } + exchange_sell.return_value = { + "orderNumber": 456 + } + market_mock.return_value = { "id": "BTC_ETC", "symbol": "BTC_ETC" } + ptp.return_value = D("0.1") + atp.return_value = D("12") + + order = self.s.create_order("BTC_ETC", "limit", "buy", "12", + account="exchange", price="0.1") + self.assertEqual(123, order["id"]) + exchange_buy.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12")}) + exchange_sell.assert_not_called() + exchange_buy.reset_mock() + exchange_sell.reset_mock() + + order = self.s.create_order("BTC_ETC", "limit", "sell", + "12", account="exchange", lending_rate="0.01", price="0.1") + self.assertEqual(456, order["id"]) + exchange_sell.assert_called_once_with({"currencyPair": "BTC_ETC", "rate": D("0.1"), "amount": D("12")}) + exchange_buy.assert_not_called() + with self.subTest(account="unknown"), self.assertRaises(NotImplementedError),\ + mock.patch.object(self.s, "load_markets") as load_markets: + self.s.create_order("symbol", "type", "side", "amount", account="unknown") + def test_common_currency_code(self): + self.assertEqual("FOO", self.s.common_currency_code("FOO")) + + def test_currency_id(self): + self.assertEqual("FOO", self.s.currency_id("FOO")) + + def test_amount_to_precision(self): + self.s.markets = { + "FOO": { + "precision": { + "price": 5, + "amount": 6, + } + }, + "BAR": { + "precision": { + "price": 7, + "amount": 8, + } + } + } + self.assertEqual("0.0001", self.s.amount_to_precision("FOO", D("0.0001"))) + self.assertEqual("0.0000001", self.s.amount_to_precision("BAR", D("0.0000001"))) + self.assertEqual("0.000001", self.s.amount_to_precision("FOO", D("0.000001"))) + + def test_price_to_precision(self): + self.s.markets = { + "FOO": { + "precision": { + "price": 5, + "amount": 6, + } + }, + "BAR": { + "precision": { + "price": 7, + "amount": 8, + } + } + } + self.assertEqual("0.0001", self.s.price_to_precision("FOO", D("0.0001"))) + self.assertEqual("0.0000001", self.s.price_to_precision("BAR", D("0.0000001"))) + self.assertEqual("0", self.s.price_to_precision("FOO", D("0.000001"))) + + def test_is_dust_trade(self): + self.assertTrue(self.s.is_dust_trade(D("0.0000009"), D("1000"))) + self.assertTrue(self.s.is_dust_trade(D("0.000001"), D("10"))) + self.assertFalse(self.s.is_dust_trade(D("0.000001"), D("100"))) + + def test_fetch_nth_order_book(self): + with mock.patch.object(self.s, "fetch_order_book") as t: + t.return_value = { + "asks": [ + [1.269e-05, 781.23105917], + [1.271e-05, 108.83577689], + [1.276e-05, 19162.15732141], + [1.277e-05, 34.13657561], + [1.28e-05, 95.14285714], + [1.281e-05, 11.13909862], + [1.282e-05, 43.42379871], + [1.284e-05, 493.67767887], + [1.288e-05, 6179.57843281], + [1.289e-05, 235.16250589] + ], + "bids": [ + [1.266e-05, 3496.42283539], + [1.23e-05, 9.02439024], + [1.229e-05, 3244.25987796], + [1.228e-05, 6692.16061185], + [1.207e-05, 9.19635459], + [1.206e-05, 4711.05943978], + [1.194e-05, 84.67400508], + [1.168e-05, 61.75268779], + [1.165e-05, 9.52789699], + [1.157e-05, 16.4900605] + ] + } + self.assertAlmostEqual(D("0.00001289"), self.s.fetch_nth_order_book("BTC/HUC", "ask", 10), 8) + t.assert_called_once_with("BTC/HUC", limit=10) + self.assertAlmostEqual(D("0.00001157"), self.s.fetch_nth_order_book("BTC/HUC", "bid", 10), 8)