]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - tests/test_ccxt_wrapper.py
Merge branch 'dev'
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / tests / test_ccxt_wrapper.py
index d32469a83cee251933dc1176c75f84c1e1d9c812..c326f0a202dae9d2755a4fb79eba2a87e4bdd067 100644 (file)
@@ -22,11 +22,13 @@ class poloniexETest(unittest.TestCase):
             ccxt = market.ccxt.poloniexE()
             ccxt._market = mock.Mock
             ccxt._market.report = mock.Mock()
+            ccxt._market.market_id = 3
+            ccxt._market.user_id = 3
 
             ccxt.session.request("GET", "URL", data="data",
-                    headers="headers")
+                    headers={})
             ccxt._market.report.log_http_request.assert_called_with('GET', 'URL', 'data',
-                    'headers', 'response')
+                    {'X-market-id': '3', 'X-user-id': '3'}, 'response')
 
         with self.subTest("Raising"),\
                 mock.patch("market.ccxt.poloniexE.session") as session:
@@ -35,12 +37,14 @@ class poloniexETest(unittest.TestCase):
             ccxt = market.ccxt.poloniexE()
             ccxt._market = mock.Mock
             ccxt._market.report = mock.Mock()
+            ccxt._market.market_id = 3
+            ccxt._market.user_id = 3
 
             with self.assertRaises(market.ccxt.RequestException, msg="Boo") as cm:
                 ccxt.session.request("GET", "URL", data="data",
-                        headers="headers")
+                        headers={})
             ccxt._market.report.log_http_request.assert_called_with('GET', 'URL', 'data',
-                    'headers', cm.exception)
+                    {'X-market-id': '3', 'X-user-id': '3'}, cm.exception)
 
 
     def test_nanoseconds(self):
@@ -106,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),\
@@ -163,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",
@@ -435,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,\
@@ -456,22 +451,101 @@ 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:
+
+            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()
 
-            create_order.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params")
+            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")))