aboutsummaryrefslogtreecommitdiff
path: root/tests/test_store.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_store.py')
-rw-r--r--tests/test_store.py50
1 files changed, 30 insertions, 20 deletions
diff --git a/tests/test_store.py b/tests/test_store.py
index 3097f6d..4ab9bdf 100644
--- a/tests/test_store.py
+++ b/tests/test_store.py
@@ -1430,22 +1430,8 @@ class PortfolioTest(WebMockTestCase):
1430 del(data["portfolio_2"]["weights"]) 1430 del(data["portfolio_2"]["weights"])
1431 market.Portfolio.data = store.LockedVar(data) 1431 market.Portfolio.data = store.LockedVar(data)
1432 1432
1433 market.Portfolio.parse_cryptoportfolio() 1433 with self.assertRaises(AssertionError):
1434 self.assertListEqual( 1434 market.Portfolio.parse_cryptoportfolio()
1435 ["medium", "high"],
1436 list(market.Portfolio.liquidities.get().keys()))
1437 self.assertEqual({}, market.Portfolio.liquidities.get("medium"))
1438
1439 with self.subTest(description="All missing weights"):
1440 data = store.json.loads(self.json_response, parse_int=D, parse_float=D)
1441 del(data["portfolio_1"]["weights"])
1442 del(data["portfolio_2"]["weights"])
1443 market.Portfolio.data = store.LockedVar(data)
1444
1445 market.Portfolio.parse_cryptoportfolio()
1446 self.assertEqual({}, market.Portfolio.liquidities.get("medium"))
1447 self.assertEqual({}, market.Portfolio.liquidities.get("high"))
1448 self.assertEqual(datetime.datetime(1,1,1), market.Portfolio.last_date.get())
1449 1435
1450 @mock.patch.object(store.dbs, "redis_connected") 1436 @mock.patch.object(store.dbs, "redis_connected")
1451 @mock.patch.object(store.dbs, "redis") 1437 @mock.patch.object(store.dbs, "redis")
@@ -1551,7 +1537,8 @@ class PortfolioTest(WebMockTestCase):
1551 1537
1552 @mock.patch.object(market.time, "sleep") 1538 @mock.patch.object(market.time, "sleep")
1553 @mock.patch.object(market.Portfolio, "get_cryptoportfolio") 1539 @mock.patch.object(market.Portfolio, "get_cryptoportfolio")
1554 def test_wait_for_recent(self, get_cryptoportfolio, sleep): 1540 @mock.patch.object(market.Portfolio, "next_wait_time")
1541 def test_wait_for_recent(self, next_wait_time, get_cryptoportfolio, sleep):
1555 self.call_count = 0 1542 self.call_count = 0
1556 def _get(refetch=False): 1543 def _get(refetch=False):
1557 if self.call_count != 0: 1544 if self.call_count != 0:
@@ -1563,6 +1550,7 @@ class PortfolioTest(WebMockTestCase):
1563 - store.datetime.timedelta(10)\ 1550 - store.datetime.timedelta(10)\
1564 + store.datetime.timedelta(self.call_count)) 1551 + store.datetime.timedelta(self.call_count))
1565 get_cryptoportfolio.side_effect = _get 1552 get_cryptoportfolio.side_effect = _get
1553 next_wait_time.return_value = 30
1566 1554
1567 market.Portfolio.wait_for_recent() 1555 market.Portfolio.wait_for_recent()
1568 sleep.assert_called_with(30) 1556 sleep.assert_called_with(30)
@@ -1608,7 +1596,7 @@ class PortfolioTest(WebMockTestCase):
1608 def test_start_worker(self): 1596 def test_start_worker(self):
1609 with mock.patch.object(store.Portfolio, "wait_for_notification") as notification: 1597 with mock.patch.object(store.Portfolio, "wait_for_notification") as notification:
1610 store.Portfolio.start_worker() 1598 store.Portfolio.start_worker()
1611 notification.assert_called_once_with(poll=30) 1599 notification.assert_called_once_with()
1612 1600
1613 self.assertEqual("lock", store.Portfolio.last_date.lock.__class__.__name__) 1601 self.assertEqual("lock", store.Portfolio.last_date.lock.__class__.__name__)
1614 self.assertEqual("lock", store.Portfolio.liquidities.lock.__class__.__name__) 1602 self.assertEqual("lock", store.Portfolio.liquidities.lock.__class__.__name__)
@@ -1626,7 +1614,7 @@ class PortfolioTest(WebMockTestCase):
1626 with mock.patch.object(store.Portfolio, "get_cryptoportfolio") as get,\ 1614 with mock.patch.object(store.Portfolio, "get_cryptoportfolio") as get,\
1627 mock.patch.object(store.Portfolio, "report") as report,\ 1615 mock.patch.object(store.Portfolio, "report") as report,\
1628 mock.patch.object(store.time, "sleep") as sleep: 1616 mock.patch.object(store.time, "sleep") as sleep:
1629 store.Portfolio.start_worker(poll=3) 1617 store.Portfolio.start_worker()
1630 store.Portfolio.stop_worker() 1618 store.Portfolio.stop_worker()
1631 store.Portfolio.worker.join() 1619 store.Portfolio.worker.join()
1632 get.assert_not_called() 1620 get.assert_not_called()
@@ -1640,8 +1628,10 @@ class PortfolioTest(WebMockTestCase):
1640 1628
1641 with mock.patch.object(store.Portfolio, "get_cryptoportfolio") as get,\ 1629 with mock.patch.object(store.Portfolio, "get_cryptoportfolio") as get,\
1642 mock.patch.object(store.Portfolio, "report") as report,\ 1630 mock.patch.object(store.Portfolio, "report") as report,\
1631 mock.patch.object(store.Portfolio, "next_wait_time") as wait,\
1643 mock.patch.object(store.time, "sleep") as sleep: 1632 mock.patch.object(store.time, "sleep") as sleep:
1644 store.Portfolio.start_worker(poll=3) 1633 wait.return_value = 3
1634 store.Portfolio.start_worker()
1645 1635
1646 store.Portfolio.worker_notify.set() 1636 store.Portfolio.worker_notify.set()
1647 1637
@@ -1667,4 +1657,24 @@ class PortfolioTest(WebMockTestCase):
1667 worker_notify.set.assert_called_once_with() 1657 worker_notify.set.assert_called_once_with()
1668 callback.wait.assert_called_once_with() 1658 callback.wait.assert_called_once_with()
1669 1659
1660 def test_next_wait_time(self):
1661 with self.subTest("first start"):
1662 self.assertEqual(30, store.Portfolio.next_wait_time())
1663 self.assertIsNotNone(store.Portfolio.poll_started_at)
1664 with self.subTest("25min"):
1665 store.Portfolio.poll_started_at = datetime.datetime.now() - datetime.timedelta(minutes=25)
1666 self.assertEqual(30, store.Portfolio.next_wait_time())
1667 with self.subTest("35min"):
1668 store.Portfolio.poll_started_at = datetime.datetime.now() - datetime.timedelta(minutes=35)
1669 self.assertEqual(60, store.Portfolio.next_wait_time())
1670 with self.subTest("1h15"):
1671 store.Portfolio.poll_started_at = datetime.datetime.now() - datetime.timedelta(minutes=75)
1672 self.assertEqual(300, store.Portfolio.next_wait_time())
1673 with self.subTest("5hours"):
1674 store.Portfolio.poll_started_at = datetime.datetime.now() - datetime.timedelta(hours=5)
1675 self.assertEqual(3600, store.Portfolio.next_wait_time())
1676 with self.subTest("overdue"), self.assertRaises(Exception):
1677 store.Portfolio.poll_started_at = datetime.datetime.now() - datetime.timedelta(hours=25)
1678 store.Portfolio.next_wait_time()
1679
1670 1680