]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/commitdiff
Move market processing to single method
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Thu, 8 Mar 2018 00:18:21 +0000 (01:18 +0100)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Thu, 8 Mar 2018 00:18:21 +0000 (01:18 +0100)
main.py
test.py

diff --git a/main.py b/main.py
index dcbc2a34c197e6540d8420cfb219d8152bdfa8c6..37f485d15c8008aaf8e1fdbc97f99e5f0cd8822a 100644 (file)
--- a/main.py
+++ b/main.py
@@ -126,18 +126,21 @@ def parse_args(argv):
 
     return args
 
+def process(market_config, user_id, report_path, args):
+    try:
+        market.Market\
+                .from_config(market_config, debug=args.debug, user_id=user_id, report_path=report_path)\
+                .process(args.action, before=args.before, after=args.after)
+    except Exception as e:
+        print("{}: {}".format(e.__class__.__name__, e))
+
 def main(argv):
     args = parse_args(argv)
 
     pg_config, report_path = parse_config(args.config)
 
     for market_config, user_id in fetch_markets(pg_config, args.user):
-        try:
-            market.Market\
-                    .from_config(market_config, debug=args.debug, user_id=user_id, report_path=report_path)\
-                    .process(args.action, before=args.before, after=args.after)
-        except Exception as e:
-            print("{}: {}".format(e.__class__.__name__, e))
+        process(market_config, user_id, report_path, args)
 
 if __name__ == '__main__': # pragma: no cover
     main(sys.argv[1:])
diff --git a/test.py b/test.py
index c2c70cb469db5eca98158c809ac9193aef38c656..bbe0697bbfd47273106bddc29b8536320d4f34e7 100644 (file)
--- a/test.py
+++ b/test.py
@@ -3333,11 +3333,8 @@ class MainTest(WebMockTestCase):
                 self.assertIsInstance(m, market.Market)
                 self.assertTrue(m.debug)
 
-    def test_main(self):
-        with mock.patch("main.parse_args") as parse_args,\
-                mock.patch("main.parse_config") as parse_config,\
-                mock.patch("main.fetch_markets") as fetch_markets,\
-                mock.patch("market.Market") as market_mock,\
+    def test_process(self):
+        with mock.patch("market.Market") as market_mock,\
                 mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
 
             args_mock = mock.Mock()
@@ -3347,6 +3344,29 @@ class MainTest(WebMockTestCase):
             args_mock.debug = "debug"
             args_mock.before = "before"
             args_mock.after = "after"
+            self.assertEqual("", stdout_mock.getvalue())
+
+            main.process("config", 1, "report_path", args_mock)
+
+            market_mock.from_config.assert_has_calls([
+                mock.call("config", debug="debug", user_id=1, report_path="report_path"),
+                mock.call().process("action", before="before", after="after"),
+                ])
+
+            with self.subTest(exception=True):
+                market_mock.from_config.side_effect = Exception("boo")
+                main.process("config", 1, "report_path", args_mock)
+                self.assertEqual("Exception: boo\n", stdout_mock.getvalue())
+
+    def test_main(self):
+        with mock.patch("main.parse_args") as parse_args,\
+                mock.patch("main.parse_config") as parse_config,\
+                mock.patch("main.fetch_markets") as fetch_markets,\
+                mock.patch("main.process") as process:
+
+            args_mock = mock.Mock()
+            args_mock.config = "config"
+            args_mock.user = "user"
             parse_args.return_value = args_mock
 
             parse_config.return_value = ["pg_config", "report_path"]
@@ -3359,21 +3379,12 @@ class MainTest(WebMockTestCase):
             parse_config.assert_called_with("config")
             fetch_markets.assert_called_with("pg_config", "user")
 
-            self.assertEqual(2, market_mock.from_config.call_count)
-            market_mock.from_config.assert_has_calls([
-                mock.call("config1", debug="debug", user_id=1, report_path="report_path"),
-                mock.call().process("action", before="before", after="after"),
-                mock.call("config2", debug="debug", user_id=2, report_path="report_path"),
-                mock.call().process("action", before="before", after="after")
+            self.assertEqual(2, process.call_count)
+            process.assert_has_calls([
+                mock.call("config1", 1, "report_path", args_mock),
+                mock.call("config2", 2, "report_path", args_mock),
                 ])
 
-            self.assertEqual("", stdout_mock.getvalue())
-
-            with self.subTest(exception=True):
-                market_mock.from_config.side_effect = Exception("boo")
-                main.main(["Foo", "Bar"])
-                self.assertEqual("Exception: boo\nException: boo\n", stdout_mock.getvalue())
-
     @mock.patch.object(main.sys, "exit")
     @mock.patch("main.configparser")
     @mock.patch("main.os")