From 445185355d0d13d8d7251fa9add8e249c5361aa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Sat, 4 Aug 2018 20:33:16 +0200 Subject: Refactor a bit the options passing process --- market.py | 22 +++++++++++----------- tests/test_market.py | 19 +++++++++---------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/market.py b/market.py index 82df34f..546ee94 100644 --- a/market.py +++ b/market.py @@ -93,11 +93,11 @@ class Market: self.ccxt.check_required_credentials() for action in actions: if bool(before) is bool(after): - self.processor.process(action, steps="all") + self.processor.process(action, steps="all", options=self.options) elif before: - self.processor.process(action, steps="before") + self.processor.process(action, steps="before", options=self.options) elif after: - self.processor.process(action, steps="after") + self.processor.process(action, steps="after", options=self.options) except AuthenticationError: self.report.log_error("market_authentication", message="Impossible to authenticate to market") except Exception as e: @@ -408,7 +408,7 @@ class Processor: def can_process(self, scenario_name): return scenario_name in self.scenarios - def process(self, scenario_name, steps="all", **kwargs): + def process(self, scenario_name, steps="all", options={}): if not self.can_process(scenario_name): raise TypeError("Unknown scenario {}".format(scenario_name)) scenario = self.scenarios[scenario_name] @@ -420,9 +420,9 @@ class Processor: for step in steps: selected_steps += self.select_steps(scenario, step) for step in selected_steps: - self.process_step(scenario_name, step, kwargs) + self.process_step(scenario_name, step, options) - def process_step(self, scenario_name, step, kwargs): + def process_step(self, scenario_name, step, options): process_name = "process_{}__{}_{}".format(scenario_name, step["number"], step["name"]) self.market.report.log_stage("{}_begin".format(process_name)) @@ -432,7 +432,7 @@ class Processor: for action in self.ordered_actions: if action in step: - self.run_action(action, step[action], kwargs) + self.run_action(action, step[action], options) if "fetch_balances_end" in step: self.market.balances.fetch_balances(tag="{}_end".format(process_name), @@ -466,9 +466,9 @@ class Processor: return [method, kwargs] - def parse_args(self, action, default_args, kwargs): + def parse_args(self, action, default_args, options): method, allowed_arguments = self.method_arguments(action) - args = {k: v for k, v in {**default_args, **kwargs, **self.market.options}.items() if k in allowed_arguments } + args = {k: v for k, v in {**default_args, **options}.items() if k in allowed_arguments } if "repartition" in args and "base_currency" in args["repartition"]: r = args["repartition"] @@ -476,7 +476,7 @@ class Processor: return method, args - def run_action(self, action, default_args, kwargs): - method, args = self.parse_args(action, default_args, kwargs) + def run_action(self, action, default_args, options): + method, args = self.parse_args(action, default_args, options) method(**args) diff --git a/tests/test_market.py b/tests/test_market.py index aeb9f8e..49d159c 100644 --- a/tests/test_market.py +++ b/tests/test_market.py @@ -883,7 +883,7 @@ class MarketTest(WebMockTestCase): @mock.patch("market.ReportStore.log_error") @mock.patch("market.Market.store_report") def test_process(self, store_report, log_error, process): - m = market.Market(self.ccxt, self.market_args()) + m = market.Market(self.ccxt, self.market_args(), options={"foo": "bar"}) with self.subTest(actions=[], before=False, after=False): m.process([]) @@ -897,7 +897,7 @@ class MarketTest(WebMockTestCase): with self.subTest(before=True, after=False): m.process(["foo"], before=True) - process.assert_called_once_with("foo", steps="before") + process.assert_called_once_with("foo", options={"foo": "bar"}, steps="before") store_report.assert_called_once() log_error.assert_not_called() @@ -907,7 +907,7 @@ class MarketTest(WebMockTestCase): with self.subTest(before=False, after=True): m.process(["sell_all"], after=True) - process.assert_called_once_with("sell_all", steps="after") + process.assert_called_once_with("sell_all", options={"foo": "bar"}, steps="after") store_report.assert_called_once() log_error.assert_not_called() @@ -917,7 +917,7 @@ class MarketTest(WebMockTestCase): with self.subTest(before=False, after=False): m.process(["foo"]) - process.assert_called_once_with("foo", steps="all") + process.assert_called_once_with("foo", options={"foo": "bar"}, steps="all") store_report.assert_called_once() log_error.assert_not_called() @@ -927,7 +927,7 @@ class MarketTest(WebMockTestCase): with self.subTest(before=True, after=True): m.process(["sell_all"], before=True, after=True) - process.assert_called_once_with("sell_all", steps="all") + process.assert_called_once_with("sell_all", options={"foo": "bar"}, steps="all") store_report.assert_called_once() log_error.assert_not_called() @@ -1010,7 +1010,7 @@ class ProcessorTest(WebMockTestCase): with self.subTest("nominal case"): processor = market.Processor(self.m) - processor.process("sell_all", foo="bar") + processor.process("sell_all", options="bar") self.assertEqual(3, process_step.call_count) steps = list(map(lambda x: x[1][1]["name"], process_step.mock_calls)) @@ -1018,7 +1018,7 @@ class ProcessorTest(WebMockTestCase): 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) + self.assertEqual(["bar", "bar", "bar"], kwargs) process_step.reset_mock() @@ -1126,13 +1126,12 @@ class ProcessorTest(WebMockTestCase): method_mock = mock.Mock() method_arguments.return_value = [ method_mock, - ["foo2", "foo", "foo3"] + ["foo2", "foo"] ] - self.m.options = { "foo3": "coucou"} 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", "foo3": "coucou"}, args) + self.assertEqual({"foo": "bar2", "foo2": "bar"}, args) with mock.patch.object(processor, "method_arguments") as method_arguments: method_mock = mock.Mock() -- cgit v1.2.3