From a18ce2f16973155c81f983643aba675f62dea7af Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Thu, 8 Mar 2018 01:18:21 +0100 Subject: [PATCH] Move market processing to single method --- main.py | 15 +++++++++------ test.py | 47 +++++++++++++++++++++++++++++------------------ 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/main.py b/main.py index dcbc2a3..37f485d 100644 --- 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 c2c70cb..bbe0697 100644 --- 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") -- 2.41.0