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:])
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()
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"]
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")