diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-08 01:18:21 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-08 01:18:21 +0100 |
commit | a18ce2f16973155c81f983643aba675f62dea7af (patch) | |
tree | 28276c8b9a4bf5b4d22114811c0504cd70af729e | |
parent | d00dc02b02b23079671bdd1c37629faad7efa858 (diff) | |
download | Trader-a18ce2f16973155c81f983643aba675f62dea7af.tar.gz Trader-a18ce2f16973155c81f983643aba675f62dea7af.tar.zst Trader-a18ce2f16973155c81f983643aba675f62dea7af.zip |
Move market processing to single method
-rw-r--r-- | main.py | 15 | ||||
-rw-r--r-- | test.py | 47 |
2 files changed, 38 insertions, 24 deletions
@@ -126,18 +126,21 @@ def parse_args(argv): | |||
126 | 126 | ||
127 | return args | 127 | return args |
128 | 128 | ||
129 | def process(market_config, user_id, report_path, args): | ||
130 | try: | ||
131 | market.Market\ | ||
132 | .from_config(market_config, debug=args.debug, user_id=user_id, report_path=report_path)\ | ||
133 | .process(args.action, before=args.before, after=args.after) | ||
134 | except Exception as e: | ||
135 | print("{}: {}".format(e.__class__.__name__, e)) | ||
136 | |||
129 | def main(argv): | 137 | def main(argv): |
130 | args = parse_args(argv) | 138 | args = parse_args(argv) |
131 | 139 | ||
132 | pg_config, report_path = parse_config(args.config) | 140 | pg_config, report_path = parse_config(args.config) |
133 | 141 | ||
134 | for market_config, user_id in fetch_markets(pg_config, args.user): | 142 | for market_config, user_id in fetch_markets(pg_config, args.user): |
135 | try: | 143 | process(market_config, user_id, report_path, args) |
136 | market.Market\ | ||
137 | .from_config(market_config, debug=args.debug, user_id=user_id, report_path=report_path)\ | ||
138 | .process(args.action, before=args.before, after=args.after) | ||
139 | except Exception as e: | ||
140 | print("{}: {}".format(e.__class__.__name__, e)) | ||
141 | 144 | ||
142 | if __name__ == '__main__': # pragma: no cover | 145 | if __name__ == '__main__': # pragma: no cover |
143 | main(sys.argv[1:]) | 146 | main(sys.argv[1:]) |
@@ -3333,11 +3333,8 @@ class MainTest(WebMockTestCase): | |||
3333 | self.assertIsInstance(m, market.Market) | 3333 | self.assertIsInstance(m, market.Market) |
3334 | self.assertTrue(m.debug) | 3334 | self.assertTrue(m.debug) |
3335 | 3335 | ||
3336 | def test_main(self): | 3336 | def test_process(self): |
3337 | with mock.patch("main.parse_args") as parse_args,\ | 3337 | with mock.patch("market.Market") as market_mock,\ |
3338 | mock.patch("main.parse_config") as parse_config,\ | ||
3339 | mock.patch("main.fetch_markets") as fetch_markets,\ | ||
3340 | mock.patch("market.Market") as market_mock,\ | ||
3341 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: | 3338 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: |
3342 | 3339 | ||
3343 | args_mock = mock.Mock() | 3340 | args_mock = mock.Mock() |
@@ -3347,6 +3344,29 @@ class MainTest(WebMockTestCase): | |||
3347 | args_mock.debug = "debug" | 3344 | args_mock.debug = "debug" |
3348 | args_mock.before = "before" | 3345 | args_mock.before = "before" |
3349 | args_mock.after = "after" | 3346 | args_mock.after = "after" |
3347 | self.assertEqual("", stdout_mock.getvalue()) | ||
3348 | |||
3349 | main.process("config", 1, "report_path", args_mock) | ||
3350 | |||
3351 | market_mock.from_config.assert_has_calls([ | ||
3352 | mock.call("config", debug="debug", user_id=1, report_path="report_path"), | ||
3353 | mock.call().process("action", before="before", after="after"), | ||
3354 | ]) | ||
3355 | |||
3356 | with self.subTest(exception=True): | ||
3357 | market_mock.from_config.side_effect = Exception("boo") | ||
3358 | main.process("config", 1, "report_path", args_mock) | ||
3359 | self.assertEqual("Exception: boo\n", stdout_mock.getvalue()) | ||
3360 | |||
3361 | def test_main(self): | ||
3362 | with mock.patch("main.parse_args") as parse_args,\ | ||
3363 | mock.patch("main.parse_config") as parse_config,\ | ||
3364 | mock.patch("main.fetch_markets") as fetch_markets,\ | ||
3365 | mock.patch("main.process") as process: | ||
3366 | |||
3367 | args_mock = mock.Mock() | ||
3368 | args_mock.config = "config" | ||
3369 | args_mock.user = "user" | ||
3350 | parse_args.return_value = args_mock | 3370 | parse_args.return_value = args_mock |
3351 | 3371 | ||
3352 | parse_config.return_value = ["pg_config", "report_path"] | 3372 | parse_config.return_value = ["pg_config", "report_path"] |
@@ -3359,21 +3379,12 @@ class MainTest(WebMockTestCase): | |||
3359 | parse_config.assert_called_with("config") | 3379 | parse_config.assert_called_with("config") |
3360 | fetch_markets.assert_called_with("pg_config", "user") | 3380 | fetch_markets.assert_called_with("pg_config", "user") |
3361 | 3381 | ||
3362 | self.assertEqual(2, market_mock.from_config.call_count) | 3382 | self.assertEqual(2, process.call_count) |
3363 | market_mock.from_config.assert_has_calls([ | 3383 | process.assert_has_calls([ |
3364 | mock.call("config1", debug="debug", user_id=1, report_path="report_path"), | 3384 | mock.call("config1", 1, "report_path", args_mock), |
3365 | mock.call().process("action", before="before", after="after"), | 3385 | mock.call("config2", 2, "report_path", args_mock), |
3366 | mock.call("config2", debug="debug", user_id=2, report_path="report_path"), | ||
3367 | mock.call().process("action", before="before", after="after") | ||
3368 | ]) | 3386 | ]) |
3369 | 3387 | ||
3370 | self.assertEqual("", stdout_mock.getvalue()) | ||
3371 | |||
3372 | with self.subTest(exception=True): | ||
3373 | market_mock.from_config.side_effect = Exception("boo") | ||
3374 | main.main(["Foo", "Bar"]) | ||
3375 | self.assertEqual("Exception: boo\nException: boo\n", stdout_mock.getvalue()) | ||
3376 | |||
3377 | @mock.patch.object(main.sys, "exit") | 3388 | @mock.patch.object(main.sys, "exit") |
3378 | @mock.patch("main.configparser") | 3389 | @mock.patch("main.configparser") |
3379 | @mock.patch("main.os") | 3390 | @mock.patch("main.os") |