X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2Ftest_market.py;fp=tests%2Ftest_market.py;h=b41cd6aadf245e73277c2f1074d5629e7d29a705;hb=ceb7fc4c9e76857fefbe1dfe3f4dd3830d065a6f;hp=14b23b5db968c6c886209aead82d13b847a067ac;hpb=ad64ff17e3aa7d7af40a3724a8cd9f19ca045422;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/tests/test_market.py b/tests/test_market.py index 14b23b5..b41cd6a 100644 --- a/tests/test_market.py +++ b/tests/test_market.py @@ -607,21 +607,7 @@ class MarketTest(WebMockTestCase): file_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) db_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) - def test_print_orders(self): - m = market.Market(self.ccxt, self.market_args()) - with mock.patch.object(m.report, "log_stage") as log_stage,\ - mock.patch.object(m.balances, "fetch_balances") as fetch_balances,\ - mock.patch.object(m, "prepare_trades") as prepare_trades,\ - mock.patch.object(m.trades, "prepare_orders") as prepare_orders: - m.print_orders() - - log_stage.assert_called_with("print_orders") - fetch_balances.assert_called_with(tag="print_orders") - prepare_trades.assert_called_with(base_currency="BTC", - compute_value="average") - prepare_orders.assert_called_with(compute_value="average") - - def test_print_balances(self): + def test_print_tickers(self): m = market.Market(self.ccxt, self.market_args()) with mock.patch.object(m.balances, "in_currency") as in_currency,\ @@ -634,10 +620,8 @@ class MarketTest(WebMockTestCase): "ETH": portfolio.Amount("BTC", "0.3"), } - m.print_balances() + m.print_tickers() - log_stage.assert_called_once_with("print_balances") - fetch_balances.assert_called_with() print_log.assert_has_calls([ mock.call("total:"), mock.call(portfolio.Amount("BTC", "0.95")), @@ -648,8 +632,8 @@ class MarketTest(WebMockTestCase): @mock.patch("market.Market.store_report") def test_process(self, store_report, log_error, process): m = market.Market(self.ccxt, self.market_args()) - with self.subTest(before=False, after=False): - m.process(None) + with self.subTest(actions=[], before=False, after=False): + m.process([]) process.assert_not_called() store_report.assert_called_once() @@ -659,9 +643,9 @@ class MarketTest(WebMockTestCase): log_error.reset_mock() store_report.reset_mock() with self.subTest(before=True, after=False): - m.process(None, before=True) + m.process(["foo"], before=True) - process.assert_called_once_with("sell_all", steps="before") + process.assert_called_once_with("foo", steps="before") store_report.assert_called_once() log_error.assert_not_called() @@ -669,7 +653,7 @@ class MarketTest(WebMockTestCase): log_error.reset_mock() store_report.reset_mock() with self.subTest(before=False, after=True): - m.process(None, after=True) + m.process(["sell_all"], after=True) process.assert_called_once_with("sell_all", steps="after") store_report.assert_called_once() @@ -678,54 +662,30 @@ class MarketTest(WebMockTestCase): process.reset_mock() log_error.reset_mock() store_report.reset_mock() - with self.subTest(before=True, after=True): - m.process(None, before=True, after=True) + with self.subTest(before=False, after=False): + m.process(["foo"]) - process.assert_has_calls([ - mock.call("sell_all", steps="before"), - mock.call("sell_all", steps="after"), - ]) + process.assert_called_once_with("foo", steps="all") store_report.assert_called_once() log_error.assert_not_called() process.reset_mock() log_error.reset_mock() store_report.reset_mock() - with self.subTest(action="print_balances"),\ - mock.patch.object(m, "print_balances") as print_balances: - m.process(["print_balances"]) + with self.subTest(before=True, after=True): + m.process(["sell_all"], before=True, after=True) - process.assert_not_called() - log_error.assert_not_called() + process.assert_called_once_with("sell_all", steps="all") store_report.assert_called_once() - print_balances.assert_called_once_with() - - log_error.reset_mock() - store_report.reset_mock() - with self.subTest(action="print_orders"),\ - mock.patch.object(m, "print_orders") as print_orders,\ - mock.patch.object(m, "print_balances") as print_balances: - m.process(["print_orders", "print_balances"]) - - process.assert_not_called() log_error.assert_not_called() - store_report.assert_called_once() - print_orders.assert_called_once_with() - print_balances.assert_called_once_with() - - log_error.reset_mock() - store_report.reset_mock() - with self.subTest(action="unknown"): - m.process(["unknown"]) - log_error.assert_called_once_with("market_process", message="Unknown action unknown") - store_report.assert_called_once() + process.reset_mock() log_error.reset_mock() store_report.reset_mock() with self.subTest(unhandled_exception=True): process.side_effect = Exception("bouh") - m.process(None, before=True) + m.process(["some_action"], before=True) log_error.assert_called_with("market_process", exception=mock.ANY) store_report.assert_called_once() @@ -768,24 +728,39 @@ class ProcessorTest(WebMockTestCase): with self.assertRaises(TypeError): processor.select_steps(scenario, ["wait"]) + def test_can_process(self): + processor = market.Processor(self.m) + + with self.subTest(True): + self.assertTrue(processor.can_process("sell_all")) + + with self.subTest(False): + self.assertFalse(processor.can_process("unknown_action")) + @mock.patch("market.Processor.process_step") def test_process(self, process_step): - processor = market.Processor(self.m) + with self.subTest("unknown action"): + processor = market.Processor(self.m) + with self.assertRaises(TypeError): + processor.process("unknown_action") + + with self.subTest("nominal case"): + processor = market.Processor(self.m) - processor.process("sell_all", foo="bar") - self.assertEqual(3, process_step.call_count) + processor.process("sell_all", foo="bar") + self.assertEqual(3, process_step.call_count) - steps = list(map(lambda x: x[1][1]["name"], process_step.mock_calls)) - scenario_names = list(map(lambda x: x[1][0], process_step.mock_calls)) - kwargs = list(map(lambda x: x[1][2], process_step.mock_calls)) - self.assertEqual(["all_sell", "wait", "all_buy"], steps) - self.assertEqual(["sell_all", "sell_all", "sell_all"], scenario_names) - self.assertEqual([{"foo":"bar"}, {"foo":"bar"}, {"foo":"bar"}], kwargs) + steps = list(map(lambda x: x[1][1]["name"], process_step.mock_calls)) + scenario_names = list(map(lambda x: x[1][0], process_step.mock_calls)) + kwargs = list(map(lambda x: x[1][2], process_step.mock_calls)) + self.assertEqual(["all_sell", "wait", "all_buy"], steps) + self.assertEqual(["sell_all", "sell_all", "sell_all"], scenario_names) + self.assertEqual([{"foo":"bar"}, {"foo":"bar"}, {"foo":"bar"}], kwargs) - process_step.reset_mock() + process_step.reset_mock() - processor.process("sell_needed", steps=["before", "after"]) - self.assertEqual(3, process_step.call_count) + processor.process("sell_needed", steps=["before", "after"]) + self.assertEqual(3, process_step.call_count) def test_method_arguments(self): ccxt = mock.Mock(spec=market.ccxt.poloniexE) @@ -816,6 +791,9 @@ class ProcessorTest(WebMockTestCase): method, arguments = processor.method_arguments("close_trades") self.assertEqual(m.trades.close_trades, method) + method, arguments = processor.method_arguments("print_tickers") + self.assertEqual(m.print_tickers, method) + def test_process_step(self): processor = market.Processor(self.m)