aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2018-08-04 20:33:16 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2018-08-04 20:33:16 +0200
commit445185355d0d13d8d7251fa9add8e249c5361aa7 (patch)
treefd139a1304dbb0bdb56a5019bc916dd2c6565a3c
parentef8fa5e5454a17c49fe14f6a2c1cffa9cd985bdb (diff)
downloadTrader-445185355d0d13d8d7251fa9add8e249c5361aa7.tar.gz
Trader-445185355d0d13d8d7251fa9add8e249c5361aa7.tar.zst
Trader-445185355d0d13d8d7251fa9add8e249c5361aa7.zip
Refactor a bit the options passing process
-rw-r--r--market.py22
-rw-r--r--tests/test_market.py19
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:
93 self.ccxt.check_required_credentials() 93 self.ccxt.check_required_credentials()
94 for action in actions: 94 for action in actions:
95 if bool(before) is bool(after): 95 if bool(before) is bool(after):
96 self.processor.process(action, steps="all") 96 self.processor.process(action, steps="all", options=self.options)
97 elif before: 97 elif before:
98 self.processor.process(action, steps="before") 98 self.processor.process(action, steps="before", options=self.options)
99 elif after: 99 elif after:
100 self.processor.process(action, steps="after") 100 self.processor.process(action, steps="after", options=self.options)
101 except AuthenticationError: 101 except AuthenticationError:
102 self.report.log_error("market_authentication", message="Impossible to authenticate to market") 102 self.report.log_error("market_authentication", message="Impossible to authenticate to market")
103 except Exception as e: 103 except Exception as e:
@@ -408,7 +408,7 @@ class Processor:
408 def can_process(self, scenario_name): 408 def can_process(self, scenario_name):
409 return scenario_name in self.scenarios 409 return scenario_name in self.scenarios
410 410
411 def process(self, scenario_name, steps="all", **kwargs): 411 def process(self, scenario_name, steps="all", options={}):
412 if not self.can_process(scenario_name): 412 if not self.can_process(scenario_name):
413 raise TypeError("Unknown scenario {}".format(scenario_name)) 413 raise TypeError("Unknown scenario {}".format(scenario_name))
414 scenario = self.scenarios[scenario_name] 414 scenario = self.scenarios[scenario_name]
@@ -420,9 +420,9 @@ class Processor:
420 for step in steps: 420 for step in steps:
421 selected_steps += self.select_steps(scenario, step) 421 selected_steps += self.select_steps(scenario, step)
422 for step in selected_steps: 422 for step in selected_steps:
423 self.process_step(scenario_name, step, kwargs) 423 self.process_step(scenario_name, step, options)
424 424
425 def process_step(self, scenario_name, step, kwargs): 425 def process_step(self, scenario_name, step, options):
426 process_name = "process_{}__{}_{}".format(scenario_name, step["number"], step["name"]) 426 process_name = "process_{}__{}_{}".format(scenario_name, step["number"], step["name"])
427 self.market.report.log_stage("{}_begin".format(process_name)) 427 self.market.report.log_stage("{}_begin".format(process_name))
428 428
@@ -432,7 +432,7 @@ class Processor:
432 432
433 for action in self.ordered_actions: 433 for action in self.ordered_actions:
434 if action in step: 434 if action in step:
435 self.run_action(action, step[action], kwargs) 435 self.run_action(action, step[action], options)
436 436
437 if "fetch_balances_end" in step: 437 if "fetch_balances_end" in step:
438 self.market.balances.fetch_balances(tag="{}_end".format(process_name), 438 self.market.balances.fetch_balances(tag="{}_end".format(process_name),
@@ -466,9 +466,9 @@ class Processor:
466 466
467 return [method, kwargs] 467 return [method, kwargs]
468 468
469 def parse_args(self, action, default_args, kwargs): 469 def parse_args(self, action, default_args, options):
470 method, allowed_arguments = self.method_arguments(action) 470 method, allowed_arguments = self.method_arguments(action)
471 args = {k: v for k, v in {**default_args, **kwargs, **self.market.options}.items() if k in allowed_arguments } 471 args = {k: v for k, v in {**default_args, **options}.items() if k in allowed_arguments }
472 472
473 if "repartition" in args and "base_currency" in args["repartition"]: 473 if "repartition" in args and "base_currency" in args["repartition"]:
474 r = args["repartition"] 474 r = args["repartition"]
@@ -476,7 +476,7 @@ class Processor:
476 476
477 return method, args 477 return method, args
478 478
479 def run_action(self, action, default_args, kwargs): 479 def run_action(self, action, default_args, options):
480 method, args = self.parse_args(action, default_args, kwargs) 480 method, args = self.parse_args(action, default_args, options)
481 481
482 method(**args) 482 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):
883 @mock.patch("market.ReportStore.log_error") 883 @mock.patch("market.ReportStore.log_error")
884 @mock.patch("market.Market.store_report") 884 @mock.patch("market.Market.store_report")
885 def test_process(self, store_report, log_error, process): 885 def test_process(self, store_report, log_error, process):
886 m = market.Market(self.ccxt, self.market_args()) 886 m = market.Market(self.ccxt, self.market_args(), options={"foo": "bar"})
887 with self.subTest(actions=[], before=False, after=False): 887 with self.subTest(actions=[], before=False, after=False):
888 m.process([]) 888 m.process([])
889 889
@@ -897,7 +897,7 @@ class MarketTest(WebMockTestCase):
897 with self.subTest(before=True, after=False): 897 with self.subTest(before=True, after=False):
898 m.process(["foo"], before=True) 898 m.process(["foo"], before=True)
899 899
900 process.assert_called_once_with("foo", steps="before") 900 process.assert_called_once_with("foo", options={"foo": "bar"}, steps="before")
901 store_report.assert_called_once() 901 store_report.assert_called_once()
902 log_error.assert_not_called() 902 log_error.assert_not_called()
903 903
@@ -907,7 +907,7 @@ class MarketTest(WebMockTestCase):
907 with self.subTest(before=False, after=True): 907 with self.subTest(before=False, after=True):
908 m.process(["sell_all"], after=True) 908 m.process(["sell_all"], after=True)
909 909
910 process.assert_called_once_with("sell_all", steps="after") 910 process.assert_called_once_with("sell_all", options={"foo": "bar"}, steps="after")
911 store_report.assert_called_once() 911 store_report.assert_called_once()
912 log_error.assert_not_called() 912 log_error.assert_not_called()
913 913
@@ -917,7 +917,7 @@ class MarketTest(WebMockTestCase):
917 with self.subTest(before=False, after=False): 917 with self.subTest(before=False, after=False):
918 m.process(["foo"]) 918 m.process(["foo"])
919 919
920 process.assert_called_once_with("foo", steps="all") 920 process.assert_called_once_with("foo", options={"foo": "bar"}, steps="all")
921 store_report.assert_called_once() 921 store_report.assert_called_once()
922 log_error.assert_not_called() 922 log_error.assert_not_called()
923 923
@@ -927,7 +927,7 @@ class MarketTest(WebMockTestCase):
927 with self.subTest(before=True, after=True): 927 with self.subTest(before=True, after=True):
928 m.process(["sell_all"], before=True, after=True) 928 m.process(["sell_all"], before=True, after=True)
929 929
930 process.assert_called_once_with("sell_all", steps="all") 930 process.assert_called_once_with("sell_all", options={"foo": "bar"}, steps="all")
931 store_report.assert_called_once() 931 store_report.assert_called_once()
932 log_error.assert_not_called() 932 log_error.assert_not_called()
933 933
@@ -1010,7 +1010,7 @@ class ProcessorTest(WebMockTestCase):
1010 with self.subTest("nominal case"): 1010 with self.subTest("nominal case"):
1011 processor = market.Processor(self.m) 1011 processor = market.Processor(self.m)
1012 1012
1013 processor.process("sell_all", foo="bar") 1013 processor.process("sell_all", options="bar")
1014 self.assertEqual(3, process_step.call_count) 1014 self.assertEqual(3, process_step.call_count)
1015 1015
1016 steps = list(map(lambda x: x[1][1]["name"], process_step.mock_calls)) 1016 steps = list(map(lambda x: x[1][1]["name"], process_step.mock_calls))
@@ -1018,7 +1018,7 @@ class ProcessorTest(WebMockTestCase):
1018 kwargs = list(map(lambda x: x[1][2], process_step.mock_calls)) 1018 kwargs = list(map(lambda x: x[1][2], process_step.mock_calls))
1019 self.assertEqual(["all_sell", "wait", "all_buy"], steps) 1019 self.assertEqual(["all_sell", "wait", "all_buy"], steps)
1020 self.assertEqual(["sell_all", "sell_all", "sell_all"], scenario_names) 1020 self.assertEqual(["sell_all", "sell_all", "sell_all"], scenario_names)
1021 self.assertEqual([{"foo":"bar"}, {"foo":"bar"}, {"foo":"bar"}], kwargs) 1021 self.assertEqual(["bar", "bar", "bar"], kwargs)
1022 1022
1023 process_step.reset_mock() 1023 process_step.reset_mock()
1024 1024
@@ -1126,13 +1126,12 @@ class ProcessorTest(WebMockTestCase):
1126 method_mock = mock.Mock() 1126 method_mock = mock.Mock()
1127 method_arguments.return_value = [ 1127 method_arguments.return_value = [
1128 method_mock, 1128 method_mock,
1129 ["foo2", "foo", "foo3"] 1129 ["foo2", "foo"]
1130 ] 1130 ]
1131 self.m.options = { "foo3": "coucou"}
1132 method, args = processor.parse_args("action", {"foo": "bar", "foo2": "bar"}, {"foo": "bar2", "bla": "bla"}) 1131 method, args = processor.parse_args("action", {"foo": "bar", "foo2": "bar"}, {"foo": "bar2", "bla": "bla"})
1133 1132
1134 self.assertEqual(method_mock, method) 1133 self.assertEqual(method_mock, method)
1135 self.assertEqual({"foo": "bar2", "foo2": "bar", "foo3": "coucou"}, args) 1134 self.assertEqual({"foo": "bar2", "foo2": "bar"}, args)
1136 1135
1137 with mock.patch.object(processor, "method_arguments") as method_arguments: 1136 with mock.patch.object(processor, "method_arguments") as method_arguments:
1138 method_mock = mock.Mock() 1137 method_mock = mock.Mock()