diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-01 15:23:27 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-01 15:23:27 +0100 |
commit | 23f4616a85bef7e9acc57740f889a2a0346788af (patch) | |
tree | d701f03a699c107e16a562a106bf67a1071a7e2f | |
parent | 7192b2e1315c3dfd1204d609d716dd07e90f3bf4 (diff) | |
download | Trader-23f4616a85bef7e9acc57740f889a2a0346788af.tar.gz Trader-23f4616a85bef7e9acc57740f889a2a0346788af.tar.zst Trader-23f4616a85bef7e9acc57740f889a2a0346788af.zip |
Dynamically use process methods
-rw-r--r-- | helper.py | 18 | ||||
-rw-r--r-- | test.py | 10 |
2 files changed, 15 insertions, 13 deletions
@@ -83,8 +83,8 @@ def main_parse_args(argv): | |||
83 | parser.add_argument("--user", | 83 | parser.add_argument("--user", |
84 | default=None, required=False, help="Only run for that user") | 84 | default=None, required=False, help="Only run for that user") |
85 | parser.add_argument("--action", | 85 | parser.add_argument("--action", |
86 | default=None, required=False, | 86 | action='append', |
87 | help="Do a different action than trading") | 87 | help="Do a different action than trading (add several times to chain)") |
88 | 88 | ||
89 | args = parser.parse_args(argv) | 89 | args = parser.parse_args(argv) |
90 | 90 | ||
@@ -124,19 +124,19 @@ def main_fetch_markets(pg_config, user): | |||
124 | for row in cursor: | 124 | for row in cursor: |
125 | yield row | 125 | yield row |
126 | 126 | ||
127 | def main_process_market(user_market, action, before=False, after=False): | 127 | def main_process_market(user_market, actions, before=False, after=False): |
128 | if action is None: | 128 | if len(actions or []) == 0: |
129 | if before: | 129 | if before: |
130 | process_sell_all__1_all_sell(user_market) | 130 | process_sell_all__1_all_sell(user_market) |
131 | if after: | 131 | if after: |
132 | portfolio.Portfolio.wait_for_recent(user_market) | 132 | portfolio.Portfolio.wait_for_recent(user_market) |
133 | process_sell_all__2_all_buy(user_market) | 133 | process_sell_all__2_all_buy(user_market) |
134 | elif action == "print_balances": | ||
135 | print_balances(user_market) | ||
136 | elif action == "print_orders": | ||
137 | print_orders(user_market) | ||
138 | else: | 134 | else: |
139 | raise NotImplementedError("Unknown action {}".format(action)) | 135 | for action in actions: |
136 | if action in globals(): | ||
137 | (globals()[action])(user_market) | ||
138 | else: | ||
139 | raise NotImplementedError("Unknown action {}".format(action)) | ||
140 | 140 | ||
141 | def main_store_report(report_path, user_id, user_market): | 141 | def main_store_report(report_path, user_id, user_market): |
142 | try: | 142 | try: |
@@ -2920,7 +2920,7 @@ class HelperTest(WebMockTestCase): | |||
2920 | sell.reset_mock() | 2920 | sell.reset_mock() |
2921 | with self.subTest(action="print_balances"),\ | 2921 | with self.subTest(action="print_balances"),\ |
2922 | mock.patch("helper.print_balances") as print_balances: | 2922 | mock.patch("helper.print_balances") as print_balances: |
2923 | helper.main_process_market("user", "print_balances") | 2923 | helper.main_process_market("user", ["print_balances"]) |
2924 | 2924 | ||
2925 | buy.assert_not_called() | 2925 | buy.assert_not_called() |
2926 | wait.assert_not_called() | 2926 | wait.assert_not_called() |
@@ -2928,17 +2928,19 @@ class HelperTest(WebMockTestCase): | |||
2928 | print_balances.assert_called_once_with("user") | 2928 | print_balances.assert_called_once_with("user") |
2929 | 2929 | ||
2930 | with self.subTest(action="print_orders"),\ | 2930 | with self.subTest(action="print_orders"),\ |
2931 | mock.patch("helper.print_orders") as print_orders: | 2931 | mock.patch("helper.print_orders") as print_orders,\ |
2932 | helper.main_process_market("user", "print_orders") | 2932 | mock.patch("helper.print_balances") as print_balances: |
2933 | helper.main_process_market("user", ["print_orders", "print_balances"]) | ||
2933 | 2934 | ||
2934 | buy.assert_not_called() | 2935 | buy.assert_not_called() |
2935 | wait.assert_not_called() | 2936 | wait.assert_not_called() |
2936 | sell.assert_not_called() | 2937 | sell.assert_not_called() |
2937 | print_orders.assert_called_once_with("user") | 2938 | print_orders.assert_called_once_with("user") |
2939 | print_balances.assert_called_once_with("user") | ||
2938 | 2940 | ||
2939 | with self.subTest(action="unknown"),\ | 2941 | with self.subTest(action="unknown"),\ |
2940 | self.assertRaises(NotImplementedError): | 2942 | self.assertRaises(NotImplementedError): |
2941 | helper.main_process_market("user", "unknown") | 2943 | helper.main_process_market("user", ["unknown"]) |
2942 | 2944 | ||
2943 | @mock.patch.object(helper, "psycopg2") | 2945 | @mock.patch.object(helper, "psycopg2") |
2944 | def test_fetch_markets(self, psycopg2): | 2946 | def test_fetch_markets(self, psycopg2): |