X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2Ftest_store.py;h=3097f6d5d49d30f97bea413c3138cac6c21683ce;hb=5f721612111af5c56b5757cb2f21da5f2fa388bf;hp=12999d36e1b7e9bd64f0d7eda8ad002dd14b8411;hpb=9eb0de20f243bb78de0bf9118289f01f1ea1f77c;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/tests/test_store.py b/tests/test_store.py index 12999d3..3097f6d 100644 --- a/tests/test_store.py +++ b/tests/test_store.py @@ -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,9 +388,221 @@ 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 = { + "DOGE": D("0.5"), + "USDT": D("0.5"), + } + 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): self.m.ccxt.fetch_all_balances.return_value = self.fetch_balance @@ -413,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) @@ -605,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() @@ -627,6 +841,7 @@ class ReportStoreTest(WebMockTestCase): type="total") add_log.assert_called_once_with({ 'type': 'balance', + 'checkpoint': None, 'balances': 'json', 'tag': 'tag', 'tickers': { @@ -646,6 +861,7 @@ class ReportStoreTest(WebMockTestCase): }) add_redis_status.assert_called_once_with({ 'type': 'balance', + 'checkpoint': None, 'balances': 'json', 'tag': 'tag', 'tickers': { @@ -1101,7 +1317,8 @@ class PortfolioTest(WebMockTestCase): self.wm.get(market.Portfolio.URL, text=self.json_response) @mock.patch.object(market.Portfolio, "parse_cryptoportfolio") - def test_get_cryptoportfolio(self, parse_cryptoportfolio): + @mock.patch.object(market.Portfolio, "store_cryptoportfolio") + def test_get_cryptoportfolio(self, store_cryptoportfolio, parse_cryptoportfolio): with self.subTest(parallel=False): self.wm.get(market.Portfolio.URL, [ {"text":'{ "foo": "bar" }', "status_code": 200}, @@ -1116,23 +1333,28 @@ class PortfolioTest(WebMockTestCase): market.Portfolio.report.log_error.assert_not_called() market.Portfolio.report.log_http_request.assert_called_once() parse_cryptoportfolio.assert_called_once_with() + store_cryptoportfolio.assert_called_once_with() market.Portfolio.report.log_http_request.reset_mock() parse_cryptoportfolio.reset_mock() + store_cryptoportfolio.reset_mock() market.Portfolio.data = store.LockedVar(None) market.Portfolio.get_cryptoportfolio() self.assertIsNone(market.Portfolio.data.get()) self.assertEqual(2, self.wm.call_count) parse_cryptoportfolio.assert_not_called() + store_cryptoportfolio.assert_not_called() market.Portfolio.report.log_error.assert_not_called() market.Portfolio.report.log_http_request.assert_called_once() market.Portfolio.report.log_http_request.reset_mock() parse_cryptoportfolio.reset_mock() + store_cryptoportfolio.reset_mock() market.Portfolio.data = store.LockedVar("Foo") market.Portfolio.get_cryptoportfolio() self.assertEqual(2, self.wm.call_count) parse_cryptoportfolio.assert_not_called() + store_cryptoportfolio.assert_not_called() market.Portfolio.get_cryptoportfolio(refetch=True) self.assertEqual("Foo", market.Portfolio.data.get()) @@ -1153,6 +1375,7 @@ class PortfolioTest(WebMockTestCase): market.Portfolio.get_cryptoportfolio() self.assertIn("foo", market.Portfolio.data.get()) parse_cryptoportfolio.reset_mock() + store_cryptoportfolio.reset_mock() with self.subTest(worker=False): market.Portfolio.data = store.LockedVar(None) market.Portfolio.worker = mock.Mock() @@ -1160,6 +1383,7 @@ class PortfolioTest(WebMockTestCase): market.Portfolio.get_cryptoportfolio() notify.assert_called_once_with() parse_cryptoportfolio.assert_not_called() + store_cryptoportfolio.assert_not_called() def test_parse_cryptoportfolio(self): with self.subTest(description="Normal case"): @@ -1223,25 +1447,107 @@ class PortfolioTest(WebMockTestCase): self.assertEqual({}, market.Portfolio.liquidities.get("high")) self.assertEqual(datetime.datetime(1,1,1), market.Portfolio.last_date.get()) - - @mock.patch.object(market.Portfolio, "get_cryptoportfolio") - def test_repartition(self, get_cryptoportfolio): - market.Portfolio.liquidities = store.LockedVar({ + @mock.patch.object(store.dbs, "redis_connected") + @mock.patch.object(store.dbs, "redis") + def test_store_cryptoportfolio(self, redis, redis_connected): + store.Portfolio.liquidities = store.LockedVar({ "medium": { - "2018-03-01": "medium_2018-03-01", - "2018-03-08": "medium_2018-03-08", + datetime.datetime(2018,3,1): "medium_2018-03-01", + datetime.datetime(2018,3,8): "medium_2018-03-08", }, "high": { - "2018-03-01": "high_2018-03-01", - "2018-03-08": "high_2018-03-08", + datetime.datetime(2018,3,1): "high_2018-03-01", + datetime.datetime(2018,3,8): "high_2018-03-08", } }) - market.Portfolio.last_date = store.LockedVar("2018-03-08") + store.Portfolio.last_date = store.LockedVar(datetime.datetime(2018,3,8)) + + with self.subTest(redis_connected=False): + redis_connected.return_value = False + store.Portfolio.store_cryptoportfolio() + redis.set.assert_not_called() + + with self.subTest(redis_connected=True): + redis_connected.return_value = True + store.Portfolio.store_cryptoportfolio() + redis.set.assert_has_calls([ + mock.call("/cryptoportfolio/repartition/latest", '{"medium": "medium_2018-03-08", "high": "high_2018-03-08"}'), + mock.call("/cryptoportfolio/repartition/date", "2018-03-08"), + ]) + + @mock.patch.object(store.dbs, "redis_connected") + @mock.patch.object(store.dbs, "redis") + def test_retrieve_cryptoportfolio(self, redis, redis_connected): + with self.subTest(redis_connected=False): + redis_connected.return_value = False + store.Portfolio.retrieve_cryptoportfolio() + redis.get.assert_not_called() + self.assertIsNone(store.Portfolio.data.get()) + + with self.subTest(redis_connected=True, value=None): + redis_connected.return_value = True + redis.get.return_value = None + store.Portfolio.retrieve_cryptoportfolio() + self.assertEqual(2, redis.get.call_count) + + redis.reset_mock() + with self.subTest(redis_connected=True, value="present"): + redis_connected.return_value = True + redis.get.side_effect = [ + b'{ "medium": "medium_repartition", "high": "high_repartition" }', + b"2018-03-08" + ] + store.Portfolio.retrieve_cryptoportfolio() + self.assertEqual(2, redis.get.call_count) + self.assertEqual(datetime.datetime(2018,3,8), store.Portfolio.last_date.get()) + self.assertEqual("", store.Portfolio.data.get()) + expected_liquidities = { + 'high': { datetime.datetime(2018, 3, 8): 'high_repartition' }, + 'medium': { datetime.datetime(2018, 3, 8): 'medium_repartition' }, + } + self.assertEqual(expected_liquidities, store.Portfolio.liquidities.get()) + + @mock.patch.object(market.Portfolio, "get_cryptoportfolio") + @mock.patch.object(market.Portfolio, "retrieve_cryptoportfolio") + def test_repartition(self, retrieve_cryptoportfolio, get_cryptoportfolio): + 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"], + }, + "high": { + "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()) + 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")) + + 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)) + 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()) - self.assertEqual("medium_2018-03-08", market.Portfolio.repartition()) - get_cryptoportfolio.assert_called_once_with() - self.assertEqual("medium_2018-03-08", market.Portfolio.repartition(liquidity="medium")) - self.assertEqual("high_2018-03-08", market.Portfolio.repartition(liquidity="high")) + 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")