X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git;a=blobdiff_plain;f=test.py;h=740921268cf35f01ee3a046752099cd8d3045ec1;hp=7ec8ba76fa28e2c74b1fb9dbf8209824c348ec80;hb=9db7d156833cd384baa64b6148b5c646bfcc41f8;hpb=7bd830a83b662874c145ea9548edfde79eadc68f diff --git a/test.py b/test.py index 7ec8ba7..7409212 100644 --- a/test.py +++ b/test.py @@ -783,51 +783,9 @@ class MarketTest(WebMockTestCase): self.assertEqual(D("0.7575"), call[0][1]["XEM"].value) m.report.log_stage.assert_called_once_with("prepare_trades", base_currency='BTC', compute_value='average', - liquidity='medium', only=None) + liquidity='medium', only=None, repartition=None) m.report.log_balances.assert_called_once_with(tag="tag") - @mock.patch.object(portfolio.Portfolio, "repartition") - @mock.patch.object(market.Market, "get_ticker") - @mock.patch.object(market.TradeStore, "compute_trades") - def test_prepare_trades_to_sell_all(self, compute_trades, get_ticker, repartition): - def _get_ticker(c1, c2): - if c1 == "USDT" and c2 == "BTC": - return { "average": D("0.0001") } - if c1 == "XVG" and c2 == "BTC": - return { "average": D("0.000001") } - self.fail("Should be called with {}, {}".format(c1, c2)) - get_ticker.side_effect = _get_ticker - - with mock.patch("market.ReportStore"): - m = market.Market(self.ccxt) - self.ccxt.fetch_all_balances.return_value = { - "USDT": { - "exchange_free": D("10000.0"), - "exchange_used": D("0.0"), - "exchange_total": D("10000.0"), - "total": D("10000.0") - }, - "XVG": { - "exchange_free": D("10000.0"), - "exchange_used": D("0.0"), - "exchange_total": D("10000.0"), - "total": D("10000.0") - }, - } - - m.balances.fetch_balances(tag="tag") - - m.prepare_trades_to_sell_all() - - repartition.assert_not_called() - compute_trades.assert_called() - - call = compute_trades.call_args - self.assertEqual(1, call[0][0]["USDT"].value) - self.assertEqual(D("0.01"), call[0][0]["XVG"].value) - self.assertEqual(D("1.01"), call[0][1]["BTC"].value) - m.report.log_stage.assert_called_once_with("prepare_trades_to_sell_all") - m.report.log_balances.assert_called_once_with(tag="tag") @mock.patch.object(portfolio.time, "sleep") @mock.patch.object(market.TradeStore, "all_orders") @@ -2840,57 +2798,41 @@ class HelperTest(WebMockTestCase): self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;") - @mock.patch("helper.process_sell_all__1_all_sell") - @mock.patch("helper.process_sell_all__2_wait") - @mock.patch("helper.process_sell_all__3_all_buy") - def test_main_process_market(self, buy, wait, sell): + @mock.patch("helper.Processor.process") + def test_main_process_market(self, process): with self.subTest(before=False, after=False): - helper.main_process_market("user", None) - - wait.assert_not_called() - buy.assert_not_called() - sell.assert_not_called() + m = mock.Mock() + helper.main_process_market(m, None) + + process.assert_not_called() - buy.reset_mock() - wait.reset_mock() - sell.reset_mock() + process.reset_mock() with self.subTest(before=True, after=False): - helper.main_process_market("user", None, before=True) - - wait.assert_not_called() - buy.assert_not_called() - sell.assert_called_once_with("user") + helper.main_process_market(m, None, before=True) - buy.reset_mock() - wait.reset_mock() - sell.reset_mock() + process.assert_called_once_with("sell_all", steps="before") + + process.reset_mock() with self.subTest(before=False, after=True): - helper.main_process_market("user", None, after=True) + helper.main_process_market(m, None, after=True) - wait.assert_called_once_with("user") - buy.assert_called_once_with("user") - sell.assert_not_called() + process.assert_called_once_with("sell_all", steps="after") - buy.reset_mock() - wait.reset_mock() - sell.reset_mock() + process.reset_mock() with self.subTest(before=True, after=True): - helper.main_process_market("user", None, before=True, after=True) - - wait.assert_called_once_with("user") - buy.assert_called_once_with("user") - sell.assert_called_once_with("user") + helper.main_process_market(m, None, before=True, after=True) - buy.reset_mock() - wait.reset_mock() - sell.reset_mock() + process.assert_has_calls([ + mock.call("sell_all", steps="before"), + mock.call("sell_all", steps="after"), + ]) + + process.reset_mock() with self.subTest(action="print_balances"),\ mock.patch("helper.print_balances") as print_balances: helper.main_process_market("user", ["print_balances"]) - buy.assert_not_called() - wait.assert_not_called() - sell.assert_not_called() + process.assert_not_called() print_balances.assert_called_once_with("user") with self.subTest(action="print_orders"),\ @@ -2898,9 +2840,7 @@ class HelperTest(WebMockTestCase): mock.patch("helper.print_balances") as print_balances: helper.main_process_market("user", ["print_orders", "print_balances"]) - buy.assert_not_called() - wait.assert_not_called() - sell.assert_not_called() + process.assert_not_called() print_orders.assert_called_once_with("user") print_balances.assert_called_once_with("user") @@ -3074,7 +3014,8 @@ class HelperTest(WebMockTestCase): mock.call(tag="process_sell_all__1_all_sell_begin"), mock.call(tag="process_sell_all__1_all_sell_end"), ]) - self.m.prepare_trades_to_sell_all.assert_called_with(base_currency="BTC") + self.m.prepare_trades.assert_called_with(base_currency="BTC", + liquidity="medium", repartition={'BTC': (1, 'long')}) self.m.trades.prepare_orders.assert_called_with(compute_value="average") self.m.trades.run_orders.assert_called() self.m.follow_orders.assert_called()