]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - tests/test_store.py
Add increasing delay to fetching cryptoportfolio
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / tests / test_store.py
index 3097f6d5d49d30f97bea413c3138cac6c21683ce..4ab9bdf2f1587b199b146fbf203edd6a6bde9246 100644 (file)
@@ -1430,22 +1430,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")
@@ -1551,7 +1537,8 @@ class PortfolioTest(WebMockTestCase):
 
     @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:
@@ -1563,6 +1550,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)
@@ -1608,7 +1596,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__)
@@ -1626,7 +1614,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()
@@ -1640,8 +1628,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()
 
@@ -1667,4 +1657,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()
+