From a18ce2f16973155c81f983643aba675f62dea7af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Thu, 8 Mar 2018 01:18:21 +0100 Subject: Move market processing to single method --- test.py | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) (limited to 'test.py') 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") -- cgit v1.2.3