diff options
Diffstat (limited to 'test.py')
-rw-r--r-- | test.py | 111 |
1 files changed, 26 insertions, 85 deletions
@@ -783,51 +783,9 @@ class MarketTest(WebMockTestCase): | |||
783 | self.assertEqual(D("0.7575"), call[0][1]["XEM"].value) | 783 | self.assertEqual(D("0.7575"), call[0][1]["XEM"].value) |
784 | m.report.log_stage.assert_called_once_with("prepare_trades", | 784 | m.report.log_stage.assert_called_once_with("prepare_trades", |
785 | base_currency='BTC', compute_value='average', | 785 | base_currency='BTC', compute_value='average', |
786 | liquidity='medium', only=None) | 786 | liquidity='medium', only=None, repartition=None) |
787 | m.report.log_balances.assert_called_once_with(tag="tag") | 787 | m.report.log_balances.assert_called_once_with(tag="tag") |
788 | 788 | ||
789 | @mock.patch.object(portfolio.Portfolio, "repartition") | ||
790 | @mock.patch.object(market.Market, "get_ticker") | ||
791 | @mock.patch.object(market.TradeStore, "compute_trades") | ||
792 | def test_prepare_trades_to_sell_all(self, compute_trades, get_ticker, repartition): | ||
793 | def _get_ticker(c1, c2): | ||
794 | if c1 == "USDT" and c2 == "BTC": | ||
795 | return { "average": D("0.0001") } | ||
796 | if c1 == "XVG" and c2 == "BTC": | ||
797 | return { "average": D("0.000001") } | ||
798 | self.fail("Should be called with {}, {}".format(c1, c2)) | ||
799 | get_ticker.side_effect = _get_ticker | ||
800 | |||
801 | with mock.patch("market.ReportStore"): | ||
802 | m = market.Market(self.ccxt) | ||
803 | self.ccxt.fetch_all_balances.return_value = { | ||
804 | "USDT": { | ||
805 | "exchange_free": D("10000.0"), | ||
806 | "exchange_used": D("0.0"), | ||
807 | "exchange_total": D("10000.0"), | ||
808 | "total": D("10000.0") | ||
809 | }, | ||
810 | "XVG": { | ||
811 | "exchange_free": D("10000.0"), | ||
812 | "exchange_used": D("0.0"), | ||
813 | "exchange_total": D("10000.0"), | ||
814 | "total": D("10000.0") | ||
815 | }, | ||
816 | } | ||
817 | |||
818 | m.balances.fetch_balances(tag="tag") | ||
819 | |||
820 | m.prepare_trades_to_sell_all() | ||
821 | |||
822 | repartition.assert_not_called() | ||
823 | compute_trades.assert_called() | ||
824 | |||
825 | call = compute_trades.call_args | ||
826 | self.assertEqual(1, call[0][0]["USDT"].value) | ||
827 | self.assertEqual(D("0.01"), call[0][0]["XVG"].value) | ||
828 | self.assertEqual(D("1.01"), call[0][1]["BTC"].value) | ||
829 | m.report.log_stage.assert_called_once_with("prepare_trades_to_sell_all") | ||
830 | m.report.log_balances.assert_called_once_with(tag="tag") | ||
831 | 789 | ||
832 | @mock.patch.object(portfolio.time, "sleep") | 790 | @mock.patch.object(portfolio.time, "sleep") |
833 | @mock.patch.object(market.TradeStore, "all_orders") | 791 | @mock.patch.object(market.TradeStore, "all_orders") |
@@ -2840,57 +2798,41 @@ class HelperTest(WebMockTestCase): | |||
2840 | 2798 | ||
2841 | self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;") | 2799 | self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;") |
2842 | 2800 | ||
2843 | @mock.patch("helper.process_sell_all__1_all_sell") | 2801 | @mock.patch("helper.Processor.process") |
2844 | @mock.patch("helper.process_sell_all__2_wait") | 2802 | def test_main_process_market(self, process): |
2845 | @mock.patch("helper.process_sell_all__3_all_buy") | ||
2846 | def test_main_process_market(self, buy, wait, sell): | ||
2847 | with self.subTest(before=False, after=False): | 2803 | with self.subTest(before=False, after=False): |
2848 | helper.main_process_market("user", None) | 2804 | m = mock.Mock() |
2849 | 2805 | helper.main_process_market(m, None) | |
2850 | wait.assert_not_called() | 2806 | |
2851 | buy.assert_not_called() | 2807 | process.assert_not_called() |
2852 | sell.assert_not_called() | ||
2853 | 2808 | ||
2854 | buy.reset_mock() | 2809 | process.reset_mock() |
2855 | wait.reset_mock() | ||
2856 | sell.reset_mock() | ||
2857 | with self.subTest(before=True, after=False): | 2810 | with self.subTest(before=True, after=False): |
2858 | helper.main_process_market("user", None, before=True) | 2811 | helper.main_process_market(m, None, before=True) |
2859 | |||
2860 | wait.assert_not_called() | ||
2861 | buy.assert_not_called() | ||
2862 | sell.assert_called_once_with("user") | ||
2863 | 2812 | ||
2864 | buy.reset_mock() | 2813 | process.assert_called_once_with("sell_all", steps="before") |
2865 | wait.reset_mock() | 2814 | |
2866 | sell.reset_mock() | 2815 | process.reset_mock() |
2867 | with self.subTest(before=False, after=True): | 2816 | with self.subTest(before=False, after=True): |
2868 | helper.main_process_market("user", None, after=True) | 2817 | helper.main_process_market(m, None, after=True) |
2869 | 2818 | ||
2870 | wait.assert_called_once_with("user") | 2819 | process.assert_called_once_with("sell_all", steps="after") |
2871 | buy.assert_called_once_with("user") | ||
2872 | sell.assert_not_called() | ||
2873 | 2820 | ||
2874 | buy.reset_mock() | 2821 | process.reset_mock() |
2875 | wait.reset_mock() | ||
2876 | sell.reset_mock() | ||
2877 | with self.subTest(before=True, after=True): | 2822 | with self.subTest(before=True, after=True): |
2878 | helper.main_process_market("user", None, before=True, after=True) | 2823 | helper.main_process_market(m, None, before=True, after=True) |
2879 | |||
2880 | wait.assert_called_once_with("user") | ||
2881 | buy.assert_called_once_with("user") | ||
2882 | sell.assert_called_once_with("user") | ||
2883 | 2824 | ||
2884 | buy.reset_mock() | 2825 | process.assert_has_calls([ |
2885 | wait.reset_mock() | 2826 | mock.call("sell_all", steps="before"), |
2886 | sell.reset_mock() | 2827 | mock.call("sell_all", steps="after"), |
2828 | ]) | ||
2829 | |||
2830 | process.reset_mock() | ||
2887 | with self.subTest(action="print_balances"),\ | 2831 | with self.subTest(action="print_balances"),\ |
2888 | mock.patch("helper.print_balances") as print_balances: | 2832 | mock.patch("helper.print_balances") as print_balances: |
2889 | helper.main_process_market("user", ["print_balances"]) | 2833 | helper.main_process_market("user", ["print_balances"]) |
2890 | 2834 | ||
2891 | buy.assert_not_called() | 2835 | process.assert_not_called() |
2892 | wait.assert_not_called() | ||
2893 | sell.assert_not_called() | ||
2894 | print_balances.assert_called_once_with("user") | 2836 | print_balances.assert_called_once_with("user") |
2895 | 2837 | ||
2896 | with self.subTest(action="print_orders"),\ | 2838 | with self.subTest(action="print_orders"),\ |
@@ -2898,9 +2840,7 @@ class HelperTest(WebMockTestCase): | |||
2898 | mock.patch("helper.print_balances") as print_balances: | 2840 | mock.patch("helper.print_balances") as print_balances: |
2899 | helper.main_process_market("user", ["print_orders", "print_balances"]) | 2841 | helper.main_process_market("user", ["print_orders", "print_balances"]) |
2900 | 2842 | ||
2901 | buy.assert_not_called() | 2843 | process.assert_not_called() |
2902 | wait.assert_not_called() | ||
2903 | sell.assert_not_called() | ||
2904 | print_orders.assert_called_once_with("user") | 2844 | print_orders.assert_called_once_with("user") |
2905 | print_balances.assert_called_once_with("user") | 2845 | print_balances.assert_called_once_with("user") |
2906 | 2846 | ||
@@ -3074,7 +3014,8 @@ class HelperTest(WebMockTestCase): | |||
3074 | mock.call(tag="process_sell_all__1_all_sell_begin"), | 3014 | mock.call(tag="process_sell_all__1_all_sell_begin"), |
3075 | mock.call(tag="process_sell_all__1_all_sell_end"), | 3015 | mock.call(tag="process_sell_all__1_all_sell_end"), |
3076 | ]) | 3016 | ]) |
3077 | self.m.prepare_trades_to_sell_all.assert_called_with(base_currency="BTC") | 3017 | self.m.prepare_trades.assert_called_with(base_currency="BTC", |
3018 | liquidity="medium", repartition={'BTC': (1, 'long')}) | ||
3078 | self.m.trades.prepare_orders.assert_called_with(compute_value="average") | 3019 | self.m.trades.prepare_orders.assert_called_with(compute_value="average") |
3079 | self.m.trades.run_orders.assert_called() | 3020 | self.m.trades.run_orders.assert_called() |
3080 | self.m.follow_orders.assert_called() | 3021 | self.m.follow_orders.assert_called() |