X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2Ftest_market.py;h=c02968672aa46cdadb64379ac40d87e970c1e7ea;hb=5f721612111af5c56b5757cb2f21da5f2fa388bf;hp=c89025be021383625a810a5eb8e33efb82bed6dd;hpb=d3fec88cb0d301120b137692900d96bf475d2745;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/tests/test_market.py b/tests/test_market.py index c89025b..c029686 100644 --- a/tests/test_market.py +++ b/tests/test_market.py @@ -186,14 +186,17 @@ class MarketTest(WebMockTestCase): 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)) get_ticker.side_effect = _get_ticker repartition.return_value = { - "DOGE": (D("0.25"), "short"), - "BTC": (D("0.25"), "long"), - "ETH": (D("0.25"), "long"), - "XMR": (D("0.25"), "long"), + "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"), } m = market.Market(self.ccxt, self.market_args()) self.ccxt.fetch_all_balances.return_value = { @@ -210,12 +213,12 @@ class MarketTest(WebMockTestCase): "total": D("5.0") }, "BTC": { - "exchange_free": D("0.075"), + "exchange_free": D("0.065"), "exchange_used": D("0.02"), - "exchange_total": D("0.095"), - "margin_available": D("0.025"), + "exchange_total": D("0.085"), + "margin_available": D("0.035"), "margin_in_position": D("0.01"), - "margin_total": D("0.035"), + "margin_total": D("0.045"), "total": D("0.13") }, "ETH": { @@ -224,6 +227,12 @@ class MarketTest(WebMockTestCase): "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"), + }, } m.balances.fetch_balances(tag="tag") @@ -236,12 +245,13 @@ class MarketTest(WebMockTestCase): self.assertEqual(portfolio.Amount("BTC", "-0.025"), new_repartition["DOGE"] - values_in_base["DOGE"]) - self.assertEqual(portfolio.Amount("BTC", "0.025"), - new_repartition["ETH"] - values_in_base["ETH"]) self.assertEqual(0, - new_repartition["ZRC"] - values_in_base["ZRC"]) + new_repartition["ETH"] - values_in_base["ETH"]) + self.assertIsNone(new_repartition.get("ZRC")) self.assertEqual(portfolio.Amount("BTC", "0.025"), new_repartition["XMR"]) + self.assertEqual(portfolio.Amount("BTC", "0.015"), + new_repartition["FOO"] - values_in_base["FOO"]) compute_trades.reset_mock() with self.subTest(available_balance_only=True, balance=0),\ @@ -392,6 +402,40 @@ class MarketTest(WebMockTestCase): m.report.log_order.assert_called_with(order_mock2, 2, new_order=new_order_mock) new_order_mock.run.assert_called_once_with() + with self.subTest("disappearing order no action to do"), \ + mock.patch("market.ReportStore"): + all_orders.reset_mock() + m = market.Market(self.ccxt, self.market_args()) + + order_mock1 = mock.Mock() + order_mock2 = mock.Mock() + all_orders.side_effect = [ + [order_mock1, order_mock2], + [order_mock1, order_mock2], + + [order_mock1, order_mock2], + [order_mock1, order_mock2], + + [] + ] + + order_mock1.get_status.side_effect = ["open", "closed"] + order_mock2.get_status.side_effect = ["open", "error_disappeared"] + + order_mock1.trade = mock.Mock() + trade_mock = mock.Mock() + order_mock2.trade = trade_mock + + trade_mock.tick_actions_recreate.return_value = "tick1" + trade_mock.prepare_order.return_value = None + + m.follow_orders() + + trade_mock.tick_actions_recreate.assert_called_once_with(2) + trade_mock.prepare_order.assert_called_once_with(compute_value="tick1") + m.report.log_error.assert_called_once_with("follow_orders", message=mock.ANY) + m.report.log_order.assert_called_with(order_mock2, 2, finished=True) + @mock.patch.object(market.BalanceStore, "fetch_balances") def test_move_balance(self, fetch_balances): for debug in [True, False]: