+ method_mock.assert_called_with(self.m, foo="bar")
+
+ def test_select_step(self):
+ processor = helper.Processor(self.m)
+
+ scenario = processor.scenarios["sell_all"]
+
+ self.assertEqual(scenario, processor.select_steps(scenario, "all"))
+ self.assertEqual(["all_sell"], list(map(lambda x: x["name"], processor.select_steps(scenario, "before"))))
+ self.assertEqual(["wait", "all_buy"], list(map(lambda x: x["name"], processor.select_steps(scenario, "after"))))
+ self.assertEqual(["wait"], list(map(lambda x: x["name"], processor.select_steps(scenario, 2))))
+ self.assertEqual(["wait"], list(map(lambda x: x["name"], processor.select_steps(scenario, "wait"))))
+
+ with self.assertRaises(TypeError):
+ processor.select_steps(scenario, ["wait"])
+
+ @mock.patch("helper.Processor.process_step")
+ def test_process(self, process_step):
+ processor = helper.Processor(self.m)
+
+ 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)
+
+ process_step.reset_mock()
+
+ 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)
+ m = market.Market(ccxt)
+
+ processor = helper.Processor(m)
+
+ method, arguments = processor.method_arguments("wait_for_recent")
+ self.assertEqual(portfolio.Portfolio.wait_for_recent, method)
+ self.assertEqual(["delta"], arguments)
+
+ method, arguments = processor.method_arguments("prepare_trades")
+ self.assertEqual(m.prepare_trades, method)
+ self.assertEqual(['base_currency', 'liquidity', 'compute_value', 'repartition', 'only'], arguments)
+
+ method, arguments = processor.method_arguments("prepare_orders")
+ self.assertEqual(m.trades.prepare_orders, method)
+
+ method, arguments = processor.method_arguments("move_balances")
+ self.assertEqual(m.move_balances, method)
+
+ method, arguments = processor.method_arguments("run_orders")
+ self.assertEqual(m.trades.run_orders, method)
+
+ method, arguments = processor.method_arguments("follow_orders")
+ self.assertEqual(m.follow_orders, method)
+
+ method, arguments = processor.method_arguments("close_trades")
+ self.assertEqual(m.trades.close_trades, method)
+
+ def test_process_step(self):
+ processor = helper.Processor(self.m)
+
+ with mock.patch.object(processor, "run_action") as run_action:
+ step = processor.scenarios["sell_needed"][1]
+
+ processor.process_step("foo", step, {"foo":"bar"})
+
+ self.m.report.log_stage.assert_has_calls([
+ mock.call("process_foo__1_sell_begin"),
+ mock.call("process_foo__1_sell_end"),
+ ])
+ self.m.balances.fetch_balances.assert_has_calls([
+ mock.call(tag="process_foo__1_sell_begin"),
+ mock.call(tag="process_foo__1_sell_end"),
+ ])
+
+ self.assertEqual(5, run_action.call_count)
+
+ run_action.assert_has_calls([
+ mock.call('prepare_trades', {}, {'foo': 'bar'}),
+ mock.call('prepare_orders', {'only': 'dispose', 'compute_value': 'average'}, {'foo': 'bar'}),
+ mock.call('run_orders', {}, {'foo': 'bar'}),
+ mock.call('follow_orders', {}, {'foo': 'bar'}),
+ mock.call('close_trades', {}, {'foo': 'bar'}),
+ ])
+
+ self.m.reset_mock()
+ with mock.patch.object(processor, "run_action") as run_action:
+ step = processor.scenarios["sell_needed"][0]
+
+ processor.process_step("foo", step, {"foo":"bar"})
+ self.m.balances.fetch_balances.assert_not_called()
+
+ def test_parse_args(self):
+ processor = helper.Processor(self.m)
+
+ with mock.patch.object(processor, "method_arguments") as method_arguments:
+ method_mock = mock.Mock()
+ method_arguments.return_value = [
+ method_mock,
+ ["foo2", "foo"]
+ ]
+ method, args = processor.parse_args("action", {"foo": "bar", "foo2": "bar"}, {"foo": "bar2", "bla": "bla"})
+
+ self.assertEqual(method_mock, method)
+ self.assertEqual({"foo": "bar2", "foo2": "bar"}, args)
+
+ with mock.patch.object(processor, "method_arguments") as method_arguments:
+ method_mock = mock.Mock()
+ method_arguments.return_value = [
+ method_mock,
+ ["repartition"]
+ ]
+ method, args = processor.parse_args("action", {"repartition": { "base_currency": 1 }}, {})
+
+ self.assertEqual(1, len(args["repartition"]))
+ self.assertIn("BTC", args["repartition"])
+
+ with mock.patch.object(processor, "method_arguments") as method_arguments:
+ method_mock = mock.Mock()
+ method_arguments.return_value = [
+ method_mock,
+ ["repartition", "base_currency"]
+ ]
+ method, args = processor.parse_args("action", {"repartition": { "base_currency": 1 }}, {"base_currency": "USDT"})
+
+ self.assertEqual(1, len(args["repartition"]))
+ self.assertIn("USDT", args["repartition"])
+
+ with mock.patch.object(processor, "method_arguments") as method_arguments:
+ method_mock = mock.Mock()
+ method_arguments.return_value = [
+ method_mock,
+ ["repartition", "base_currency"]
+ ]
+ method, args = processor.parse_args("action", {"repartition": { "ETH": 1 }}, {"base_currency": "USDT"})
+
+ self.assertEqual(1, len(args["repartition"]))
+ self.assertIn("ETH", args["repartition"])