]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - tests/test_store.py
Don’t raise when some market is disabled
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / tests / test_store.py
index ee7e06349c4816263728b5c0d0504bb397e5018e..6f220c8bfd5f01f6bb90bd010ddd3875b0ea5ec7 100644 (file)
@@ -380,7 +380,7 @@ class BalanceStoreTest(WebMockTestCase):
             balance_store.fetch_balances(tag="foo")
             self.assertEqual(0, balance_store.all["ETC"].total)
             self.assertListEqual(["USDT", "XVG", "XMR", "ETC"], list(balance_store.currencies()))
-            self.m.report.log_balances.assert_called_with(tag="foo")
+            self.m.report.log_balances.assert_called_with(tag="foo", checkpoint=None)
 
         with self.subTest(log_tickers=True),\
                 mock.patch.object(balance_store, "in_currency") as in_currency:
@@ -388,11 +388,10 @@ class BalanceStoreTest(WebMockTestCase):
             balance_store.fetch_balances(log_tickers=True, ticker_currency="FOO",
                     ticker_compute_value="compute", ticker_type="type")
             self.m.report.log_balances.assert_called_with(compute_value='compute',
-                    tag=None, ticker_currency='FOO', tickers='tickers',
+                    tag=None, checkpoint=None, ticker_currency='FOO', tickers='tickers',
                     type='type')
 
         balance_store = market.BalanceStore(self.m)
-
         with self.subTest(add_portfolio=True),\
                 mock.patch.object(market.Portfolio, "repartition") as repartition:
             repartition.return_value = {
@@ -402,6 +401,207 @@ class BalanceStoreTest(WebMockTestCase):
             balance_store.fetch_balances(add_portfolio=True)
             self.assertListEqual(["USDT", "XVG", "XMR", "DOGE"], list(balance_store.currencies()))
 
+        self.m.ccxt.fetch_all_balances.return_value = {
+                "ETC": {
+                    "exchange_free": 0,
+                    "exchange_used": 0,
+                    "exchange_total": 0,
+                    "margin_total": 0,
+                    },
+                "XVG": {
+                    "exchange_free": 16,
+                    "exchange_used": 0,
+                    "exchange_total": 16,
+                    "margin_total": 0,
+                    },
+                "XMR": {
+                    "exchange_free": 0,
+                    "exchange_used": 0,
+                    "exchange_total": 0,
+                    "margin_total": D("-1.0"),
+                    "margin_free": 0,
+                    },
+                }
+
+        balance_store = market.BalanceStore(self.m)
+        with self.subTest(add_usdt=True),\
+                mock.patch.object(market.Portfolio, "repartition") as repartition:
+            repartition.return_value = {
+                    "DOGE": D("0.5"),
+                    "ETH": D("0.5"),
+                    }
+            balance_store.fetch_balances(add_usdt=True)
+            self.assertListEqual(["XVG", "XMR", "USDT"], list(balance_store.currencies()))
+
+    @mock.patch.object(market.Portfolio, "repartition")
+    def test_available_balances_for_repartition(self, repartition):
+        with self.subTest(available_balance_only=True):
+            def _get_ticker(c1, c2):
+                if c1 == "ZRC" and c2 == "BTC":
+                    return { "average": D("0.0001") }
+                if c1 == "DOGE" and c2 == "BTC":
+                    return { "average": D("0.000001") }
+                if c1 == "ETH" and c2 == "BTC":
+                    return { "average": D("0.1") }
+                if c1 == "FOO" and c2 == "BTC":
+                    return { "average": D("0.1") }
+                self.fail("Should not be called with {}, {}".format(c1, c2))
+            self.m.get_ticker.side_effect = _get_ticker
+
+            repartition.return_value = {
+                    "DOGE": (D("0.20"), "short"),
+                    "BTC": (D("0.20"), "long"),
+                    "ETH": (D("0.20"), "long"),
+                    "XMR": (D("0.20"), "long"),
+                    "FOO": (D("0.20"), "long"),
+                    }
+            self.m.ccxt.fetch_all_balances.return_value = {
+                    "ZRC": {
+                        "exchange_free": D("2.0"),
+                        "exchange_used": D("0.0"),
+                        "exchange_total": D("2.0"),
+                        "total": D("2.0")
+                        },
+                    "DOGE": {
+                        "exchange_free": D("5.0"),
+                        "exchange_used": D("0.0"),
+                        "exchange_total": D("5.0"),
+                        "total": D("5.0")
+                        },
+                    "BTC": {
+                        "exchange_free": D("0.065"),
+                        "exchange_used": D("0.02"),
+                        "exchange_total": D("0.085"),
+                        "margin_available": D("0.035"),
+                        "margin_in_position": D("0.01"),
+                        "margin_total": D("0.045"),
+                        "total": D("0.13")
+                        },
+                    "ETH": {
+                        "exchange_free": D("1.0"),
+                        "exchange_used": D("0.0"),
+                        "exchange_total": D("1.0"),
+                        "total": D("1.0")
+                        },
+                    "FOO": {
+                        "exchange_free": D("0.1"),
+                        "exchange_used": D("0.0"),
+                        "exchange_total": D("0.1"),
+                        "total": D("0.1"),
+                        },
+                    }
+
+            balance_store = market.BalanceStore(self.m)
+            balance_store.fetch_balances()
+            _repartition, total_base_value, amount_in_position = balance_store.available_balances_for_repartition()
+            repartition.assert_called_with(liquidity="medium")
+            self.assertEqual((D("0.20"), "short"), _repartition["DOGE"])
+            self.assertEqual((D("0.20"), "long"), _repartition["BTC"])
+            self.assertEqual((D("0.20"), "long"), _repartition["XMR"])
+            self.assertEqual((D("0.20"), "long"), _repartition["FOO"])
+            self.assertIsNone(_repartition.get("ETH"))
+            self.assertEqual(portfolio.Amount("BTC", "0.1"), total_base_value)
+            self.assertEqual(0, amount_in_position["DOGE"])
+            self.assertEqual(0, amount_in_position["BTC"])
+            self.assertEqual(0, amount_in_position["XMR"])
+            self.assertEqual(portfolio.Amount("BTC", "0.1"), amount_in_position["ETH"])
+            self.assertEqual(portfolio.Amount("BTC", "0.01"), amount_in_position["FOO"])
+
+        with self.subTest(available_balance_only=True, balance=0):
+            def _get_ticker(c1, c2):
+                if c1 == "ETH" and c2 == "BTC":
+                    return { "average": D("0.1") }
+                self.fail("Should not be called with {}, {}".format(c1, c2))
+            self.m.get_ticker.side_effect = _get_ticker
+
+            repartition.return_value = {
+                    "BTC": (D("0.5"), "long"),
+                    "ETH": (D("0.5"), "long"),
+                    }
+            self.m.ccxt.fetch_all_balances.return_value = {
+                    "ETH": {
+                        "exchange_free": D("1.0"),
+                        "exchange_used": D("0.0"),
+                        "exchange_total": D("1.0"),
+                        "total": D("1.0")
+                        },
+                    }
+
+            balance_store = market.BalanceStore(self.m)
+            balance_store.fetch_balances()
+            _repartition, total_base_value, amount_in_position = balance_store.available_balances_for_repartition(liquidity="high")
+
+            repartition.assert_called_with(liquidity="high")
+            self.assertEqual((D("0.5"), "long"), _repartition["BTC"])
+            self.assertIsNone(_repartition.get("ETH"))
+            self.assertEqual(0, total_base_value)
+            self.assertEqual(0, amount_in_position["BTC"])
+            self.assertEqual(0, amount_in_position["BTC"])
+
+        repartition.reset_mock()
+        with self.subTest(available_balance_only=True, balance=0,
+                repartition="present"):
+            def _get_ticker(c1, c2):
+                if c1 == "ETH" and c2 == "BTC":
+                    return { "average": D("0.1") }
+                self.fail("Should not be called with {}, {}".format(c1, c2))
+            self.m.get_ticker.side_effect = _get_ticker
+
+            _repartition = {
+                    "BTC": (D("0.5"), "long"),
+                    "ETH": (D("0.5"), "long"),
+                    }
+            self.m.ccxt.fetch_all_balances.return_value = {
+                    "ETH": {
+                        "exchange_free": D("1.0"),
+                        "exchange_used": D("0.0"),
+                        "exchange_total": D("1.0"),
+                        "total": D("1.0")
+                        },
+                    }
+
+            balance_store = market.BalanceStore(self.m)
+            balance_store.fetch_balances()
+            _repartition, total_base_value, amount_in_position = balance_store.available_balances_for_repartition(repartition=_repartition)
+            repartition.assert_not_called()
+
+            self.assertEqual((D("0.5"), "long"), _repartition["BTC"])
+            self.assertIsNone(_repartition.get("ETH"))
+            self.assertEqual(0, total_base_value)
+            self.assertEqual(0, amount_in_position["BTC"])
+            self.assertEqual(portfolio.Amount("BTC", "0.1"), amount_in_position["ETH"])
+
+        repartition.reset_mock()
+        with self.subTest(available_balance_only=True, balance=0,
+                repartition="present", base_currency="ETH"):
+            def _get_ticker(c1, c2):
+                if c1 == "ETH" and c2 == "BTC":
+                    return { "average": D("0.1") }
+                self.fail("Should not be called with {}, {}".format(c1, c2))
+            self.m.get_ticker.side_effect = _get_ticker
+
+            _repartition = {
+                    "BTC": (D("0.5"), "long"),
+                    "ETH": (D("0.5"), "long"),
+                    }
+            self.m.ccxt.fetch_all_balances.return_value = {
+                    "ETH": {
+                        "exchange_free": D("1.0"),
+                        "exchange_used": D("0.0"),
+                        "exchange_total": D("1.0"),
+                        "total": D("1.0")
+                        },
+                    }
+
+            balance_store = market.BalanceStore(self.m)
+            balance_store.fetch_balances()
+            _repartition, total_base_value, amount_in_position = balance_store.available_balances_for_repartition(repartition=_repartition, base_currency="ETH")
+
+            self.assertEqual((D("0.5"), "long"), _repartition["BTC"])
+            self.assertEqual((D("0.5"), "long"), _repartition["ETH"])
+            self.assertEqual(portfolio.Amount("ETH", 1), total_base_value)
+            self.assertEqual(0, amount_in_position["BTC"])
+            self.assertEqual(0, amount_in_position["ETH"])
 
     @mock.patch.object(market.Portfolio, "repartition")
     def test_dispatch_assets(self, repartition):
@@ -425,7 +625,7 @@ class BalanceStoreTest(WebMockTestCase):
         self.assertEqual(D("2.6"), amounts["BTC"].value)
         self.assertEqual(D("7.5"), amounts["XEM"].value)
         self.assertEqual(D("-1.0"), amounts["DASH"].value)
-        self.m.report.log_balances.assert_called_with(tag=None)
+        self.m.report.log_balances.assert_called_with(tag=None, checkpoint=None)
         self.m.report.log_dispatch.assert_called_once_with(portfolio.Amount("BTC",
             "11.1"), amounts, "medium", repartition_hash)
 
@@ -617,12 +817,14 @@ class ReportStoreTest(WebMockTestCase):
                 ])
             add_log.assert_called_once_with({
                 'type': 'balance',
+                'checkpoint': None,
                 'balances': 'json',
                 'tag': 'tag'
                 })
             add_redis_status.assert_called_once_with({
                 'type': 'balance',
                 'balances': 'json',
+                'checkpoint': None,
                 'tag': 'tag'
                 })
         add_log.reset_mock()
@@ -639,6 +841,7 @@ class ReportStoreTest(WebMockTestCase):
                     type="total")
             add_log.assert_called_once_with({
                 'type': 'balance',
+                'checkpoint': None,
                 'balances': 'json',
                 'tag': 'tag',
                 'tickers': {
@@ -658,6 +861,7 @@ class ReportStoreTest(WebMockTestCase):
                 })
             add_redis_status.assert_called_once_with({
                 'type': 'balance',
+                'checkpoint': None,
                 'balances': 'json',
                 'tag': 'tag',
                 'tickers': {
@@ -1175,11 +1379,19 @@ class PortfolioTest(WebMockTestCase):
                 with self.subTest(worker=False):
                     market.Portfolio.data = store.LockedVar(None)
                     market.Portfolio.worker = mock.Mock()
+                    market.Portfolio.worker_started = True
                     is_worker.return_value = False
                     market.Portfolio.get_cryptoportfolio()
                     notify.assert_called_once_with()
                     parse_cryptoportfolio.assert_not_called()
                     store_cryptoportfolio.assert_not_called()
+                with self.subTest(worker_started=False):
+                    market.Portfolio.data = store.LockedVar(None)
+                    market.Portfolio.worker = mock.Mock()
+                    market.Portfolio.worker_started = False
+                    is_worker.return_value = False
+                    with self.assertRaises(Exception):
+                        market.Portfolio.get_cryptoportfolio()
 
     def test_parse_cryptoportfolio(self):
         with self.subTest(description="Normal case"):
@@ -1226,22 +1438,8 @@ class PortfolioTest(WebMockTestCase):
             del(data["portfolio_2"]["weights"])
             market.Portfolio.data = store.LockedVar(data)
 
-            market.Portfolio.parse_cryptoportfolio()
-            self.assertListEqual(
-                    ["medium", "high"],
-                    list(market.Portfolio.liquidities.get().keys()))
-            self.assertEqual({}, market.Portfolio.liquidities.get("medium"))
-
-        with self.subTest(description="All missing weights"):
-            data = store.json.loads(self.json_response, parse_int=D, parse_float=D)
-            del(data["portfolio_1"]["weights"])
-            del(data["portfolio_2"]["weights"])
-            market.Portfolio.data = store.LockedVar(data)
-
-            market.Portfolio.parse_cryptoportfolio()
-            self.assertEqual({}, market.Portfolio.liquidities.get("medium"))
-            self.assertEqual({}, market.Portfolio.liquidities.get("high"))
-            self.assertEqual(datetime.datetime(1,1,1), market.Portfolio.last_date.get())
+            with self.assertRaises(AssertionError):
+                market.Portfolio.parse_cryptoportfolio()
 
     @mock.patch.object(store.dbs, "redis_connected")
     @mock.patch.object(store.dbs, "redis")
@@ -1309,33 +1507,46 @@ class PortfolioTest(WebMockTestCase):
         with self.subTest(from_cache=False):
             market.Portfolio.liquidities = store.LockedVar({
                     "medium": {
-                        "2018-03-01": "medium_2018-03-01",
-                        "2018-03-08": "medium_2018-03-08",
+                        "2018-03-01": ["medium_2018-03-01"],
+                        "2018-03-08": ["medium_2018-03-08"],
                         },
                     "high": {
-                        "2018-03-01": "high_2018-03-01",
-                        "2018-03-08": "high_2018-03-08",
+                        "2018-03-01": ["high_2018-03-01"],
+                        "2018-03-08": ["high_2018-03-08"],
                         }
                     })
             market.Portfolio.last_date = store.LockedVar("2018-03-08")
 
-            self.assertEqual("medium_2018-03-08", market.Portfolio.repartition())
+            self.assertEqual(["medium_2018-03-08"], market.Portfolio.repartition())
             get_cryptoportfolio.assert_called_once_with()
             retrieve_cryptoportfolio.assert_not_called()
-            self.assertEqual("medium_2018-03-08", market.Portfolio.repartition(liquidity="medium"))
-            self.assertEqual("high_2018-03-08", market.Portfolio.repartition(liquidity="high"))
+            self.assertEqual(["medium_2018-03-08"], market.Portfolio.repartition(liquidity="medium"))
+            self.assertEqual(["high_2018-03-08"], market.Portfolio.repartition(liquidity="high"))
 
         retrieve_cryptoportfolio.reset_mock()
         get_cryptoportfolio.reset_mock()
 
         with self.subTest(from_cache=True):
-            self.assertEqual("medium_2018-03-08", market.Portfolio.repartition(from_cache=True))
+            self.assertEqual(["medium_2018-03-08"], market.Portfolio.repartition(from_cache=True))
             get_cryptoportfolio.assert_called_once_with()
             retrieve_cryptoportfolio.assert_called_once_with()
 
+        retrieve_cryptoportfolio.reset_mock()
+        get_cryptoportfolio.reset_mock()
+
+        with self.subTest("absent liquidities"):
+            market.Portfolio.last_date = store.LockedVar("2018-03-15")
+            self.assertIsNone(market.Portfolio.repartition())
+
+        with self.subTest("no liquidities"):
+            market.Portfolio.liquidities = store.LockedVar({})
+            market.Portfolio.last_date = store.LockedVar("2018-03-08")
+            self.assertIsNone(market.Portfolio.repartition())
+
     @mock.patch.object(market.time, "sleep")
     @mock.patch.object(market.Portfolio, "get_cryptoportfolio")
-    def test_wait_for_recent(self, get_cryptoportfolio, sleep):
+    @mock.patch.object(market.Portfolio, "next_wait_time")
+    def test_wait_for_recent(self, next_wait_time, get_cryptoportfolio, sleep):
         self.call_count = 0
         def _get(refetch=False):
             if self.call_count != 0:
@@ -1347,6 +1558,7 @@ class PortfolioTest(WebMockTestCase):
                 - store.datetime.timedelta(10)\
                 + store.datetime.timedelta(self.call_count))
         get_cryptoportfolio.side_effect = _get
+        next_wait_time.return_value = 30
 
         market.Portfolio.wait_for_recent()
         sleep.assert_called_with(30)
@@ -1392,7 +1604,7 @@ class PortfolioTest(WebMockTestCase):
     def test_start_worker(self):
         with mock.patch.object(store.Portfolio, "wait_for_notification") as notification:
             store.Portfolio.start_worker()
-            notification.assert_called_once_with(poll=30)
+            notification.assert_called_once_with()
 
             self.assertEqual("lock", store.Portfolio.last_date.lock.__class__.__name__)
             self.assertEqual("lock", store.Portfolio.liquidities.lock.__class__.__name__)
@@ -1410,7 +1622,7 @@ class PortfolioTest(WebMockTestCase):
         with mock.patch.object(store.Portfolio, "get_cryptoportfolio") as get,\
                 mock.patch.object(store.Portfolio, "report") as report,\
                 mock.patch.object(store.time, "sleep") as sleep:
-            store.Portfolio.start_worker(poll=3)
+            store.Portfolio.start_worker()
             store.Portfolio.stop_worker()
             store.Portfolio.worker.join()
             get.assert_not_called()
@@ -1424,8 +1636,10 @@ class PortfolioTest(WebMockTestCase):
 
         with mock.patch.object(store.Portfolio, "get_cryptoportfolio") as get,\
                 mock.patch.object(store.Portfolio, "report") as report,\
+                mock.patch.object(store.Portfolio, "next_wait_time") as wait,\
                 mock.patch.object(store.time, "sleep") as sleep:
-            store.Portfolio.start_worker(poll=3)
+            wait.return_value = 3
+            store.Portfolio.start_worker()
 
             store.Portfolio.worker_notify.set()
 
@@ -1443,6 +1657,22 @@ class PortfolioTest(WebMockTestCase):
             store.Portfolio.worker.join()
             self.assertFalse(store.Portfolio.worker.is_alive())
 
+        with self.subTest("overdue"),\
+                mock.patch.object(store.Portfolio, "get_cryptoportfolio") as get,\
+                mock.patch.object(store.Portfolio, "report") as report,\
+                mock.patch.object(store.Portfolio, "next_wait_time") as wait,\
+                mock.patch.object(store.time, "sleep") as sleep:
+            wait.side_effect = Exception("Time over")
+            store.Portfolio.start_worker()
+
+            store.Portfolio.worker_notify.set()
+
+            store.Portfolio.callback.wait()
+
+            report.print_log.assert_called_once_with("[Worker] Fetching cryptoportfolio")
+            get.assert_called_once_with(refetch=True)
+            self.assertFalse(store.Portfolio.worker.is_alive())
+
     def test_notify_and_wait(self):
         with mock.patch.object(store.Portfolio, "callback") as callback,\
                 mock.patch.object(store.Portfolio, "worker_notify") as worker_notify:
@@ -1451,4 +1681,24 @@ class PortfolioTest(WebMockTestCase):
             worker_notify.set.assert_called_once_with()
             callback.wait.assert_called_once_with()
 
+    def test_next_wait_time(self):
+        with self.subTest("first start"):
+            self.assertEqual(30, store.Portfolio.next_wait_time())
+            self.assertIsNotNone(store.Portfolio.poll_started_at)
+        with self.subTest("25min"):
+            store.Portfolio.poll_started_at = datetime.datetime.now() - datetime.timedelta(minutes=25)
+            self.assertEqual(30, store.Portfolio.next_wait_time())
+        with self.subTest("35min"):
+            store.Portfolio.poll_started_at = datetime.datetime.now() - datetime.timedelta(minutes=35)
+            self.assertEqual(60, store.Portfolio.next_wait_time())
+        with self.subTest("1h15"):
+            store.Portfolio.poll_started_at = datetime.datetime.now() - datetime.timedelta(minutes=75)
+            self.assertEqual(300, store.Portfolio.next_wait_time())
+        with self.subTest("5hours"):
+            store.Portfolio.poll_started_at = datetime.datetime.now() - datetime.timedelta(hours=5)
+            self.assertEqual(3600, store.Portfolio.next_wait_time())
+        with self.subTest("overdue"), self.assertRaises(Exception):
+            store.Portfolio.poll_started_at = datetime.datetime.now() - datetime.timedelta(hours=25)
+            store.Portfolio.next_wait_time()
+