X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=test.py;h=854e27b1089bf6f985778242a550eca9340a0c25;hb=882d55e99489d9131b5171f23e505b0dfd1c8738;hp=bf679bfc8c2879f507bdfee64b57c09705619845;hpb=7d5da019e51adea12a1ad52e07fc71c1ee1de6ab;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/test.py b/test.py index bf679bf..854e27b 100644 --- a/test.py +++ b/test.py @@ -23,8 +23,9 @@ for test_type in limits: class WebMockTestCase(unittest.TestCase): import time - def market_args(self, debug=False, quiet=False): - return type('Args', (object,), { "debug": debug, "quiet": quiet })() + def market_args(self, debug=False, quiet=False, report_path=None, **kwargs): + return main.configargparse.Namespace(report_path=report_path, + debug=debug, quiet=quiet, **kwargs) def setUp(self): super().setUp() @@ -1632,7 +1633,8 @@ class MarketTest(WebMockTestCase): def test_store_file_report(self): file_open = mock.mock_open() - m = market.Market(self.ccxt, self.market_args(), report_path="present", user_id=1) + m = market.Market(self.ccxt, + self.market_args(report_path="present"), user_id=1) with self.subTest(file="present"),\ mock.patch("market.open", file_open),\ mock.patch.object(m, "report") as report,\ @@ -1649,7 +1651,7 @@ class MarketTest(WebMockTestCase): file_open().write.assert_any_call("Foo\nBar") m.report.to_json.assert_called_once_with() - m = market.Market(self.ccxt, self.market_args(), report_path="error", user_id=1) + m = market.Market(self.ccxt, self.market_args(report_path="error"), user_id=1) with self.subTest(file="error"),\ mock.patch("market.open") as file_open,\ mock.patch.object(m, "report") as report,\ @@ -1697,7 +1699,7 @@ class MarketTest(WebMockTestCase): self.assertEqual(stdout_mock.getvalue(), "impossible to store report to database: Exception; Bouh\n") def test_store_report(self): - m = market.Market(self.ccxt, self.market_args(), user_id=1) + m = market.Market(self.ccxt, self.market_args(report_db=False), user_id=1) with self.subTest(file=None, pg_config=None),\ mock.patch.object(m, "report") as report,\ mock.patch.object(m, "store_database_report") as db_report,\ @@ -1709,7 +1711,7 @@ class MarketTest(WebMockTestCase): db_report.assert_not_called() report.reset_mock() - m = market.Market(self.ccxt, self.market_args(), report_path="present", user_id=1) + m = market.Market(self.ccxt, self.market_args(report_db=False, report_path="present"), user_id=1) with self.subTest(file="present", pg_config=None),\ mock.patch.object(m, "report") as report,\ mock.patch.object(m, "store_file_report") as file_report,\ @@ -1725,7 +1727,23 @@ class MarketTest(WebMockTestCase): db_report.assert_not_called() report.reset_mock() - m = market.Market(self.ccxt, self.market_args(), pg_config="present", user_id=1) + m = market.Market(self.ccxt, self.market_args(report_db=True, report_path="present"), user_id=1) + with self.subTest(file="present", pg_config=None, report_db=True),\ + mock.patch.object(m, "report") as report,\ + mock.patch.object(m, "store_file_report") as file_report,\ + mock.patch.object(m, "store_database_report") as db_report,\ + mock.patch.object(market, "datetime") as time_mock: + + time_mock.now.return_value = datetime.datetime(2018, 2, 25) + + m.store_report() + + report.merge.assert_called_with(store.Portfolio.report) + file_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) + db_report.assert_not_called() + + report.reset_mock() + m = market.Market(self.ccxt, self.market_args(report_db=True), pg_config="present", user_id=1) with self.subTest(file=None, pg_config="present"),\ mock.patch.object(m, "report") as report,\ mock.patch.object(m, "store_file_report") as file_report,\ @@ -1741,8 +1759,8 @@ class MarketTest(WebMockTestCase): db_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) report.reset_mock() - m = market.Market(self.ccxt, self.market_args(), - pg_config="pg_config", report_path="present", user_id=1) + m = market.Market(self.ccxt, self.market_args(report_db=True, report_path="present"), + pg_config="pg_config", user_id=1) with self.subTest(file="present", pg_config="present"),\ mock.patch.object(m, "report") as report,\ mock.patch.object(m, "store_file_report") as file_report,\ @@ -4383,20 +4401,14 @@ class ReportStoreTest(WebMockTestCase): @mock.patch.object(market.ReportStore, "add_log") def test_log_market(self, add_log): report_store = market.ReportStore(self.m) - class Args: - def __init__(self): - self.debug = True - self.quiet = False - report_store.log_market(Args(), 4, 1, "report", True) + report_store.log_market(self.market_args(debug=True, quiet=False), 4, 1) add_log.assert_called_once_with({ "type": "market", "commit": "$Format:%H$", - "args": { "debug": True, "quiet": False }, + "args": { "report_path": None, "debug": True, "quiet": False }, "user_id": 4, "market_id": 1, - "report_path": "report", - "debug": True }) @mock.patch.object(market.ReportStore, "print_log") @@ -4603,16 +4615,16 @@ class MainTest(WebMockTestCase): args_mock.after = "after" self.assertEqual("", stdout_mock.getvalue()) - main.process("config", 3, 1, args_mock, "report_path", "pg_config") + main.process("config", 3, 1, args_mock, "pg_config") market_mock.from_config.assert_has_calls([ - mock.call("config", args_mock, pg_config="pg_config", market_id=3, user_id=1, report_path="report_path"), + mock.call("config", args_mock, pg_config="pg_config", market_id=3, user_id=1), mock.call().process("action", before="before", after="after"), ]) with self.subTest(exception=True): market_mock.from_config.side_effect = Exception("boo") - main.process(3, "config", 1, "report_path", args_mock, "pg_config") + main.process(3, "config", 1, args_mock, "pg_config") self.assertEqual("Exception: boo\n", stdout_mock.getvalue()) def test_main(self): @@ -4624,24 +4636,23 @@ class MainTest(WebMockTestCase): args_mock = mock.Mock() args_mock.parallel = False - args_mock.config = "config" args_mock.user = "user" parse_args.return_value = args_mock - parse_config.return_value = ["pg_config", "report_path"] + parse_config.return_value = "pg_config" fetch_markets.return_value = [[3, "config1", 1], [1, "config2", 2]] main.main(["Foo", "Bar"]) parse_args.assert_called_with(["Foo", "Bar"]) - parse_config.assert_called_with("config") + parse_config.assert_called_with(args_mock) fetch_markets.assert_called_with("pg_config", "user") self.assertEqual(2, process.call_count) process.assert_has_calls([ - mock.call("config1", 3, 1, args_mock, "report_path", "pg_config"), - mock.call("config2", 1, 2, args_mock, "report_path", "pg_config"), + mock.call("config1", 3, 1, args_mock, "pg_config"), + mock.call("config2", 1, 2, args_mock, "pg_config"), ]) with self.subTest(parallel=True): with mock.patch("main.parse_args") as parse_args,\ @@ -4652,79 +4663,66 @@ class MainTest(WebMockTestCase): args_mock = mock.Mock() args_mock.parallel = True - args_mock.config = "config" args_mock.user = "user" parse_args.return_value = args_mock - parse_config.return_value = ["pg_config", "report_path"] + parse_config.return_value = "pg_config" fetch_markets.return_value = [[3, "config1", 1], [1, "config2", 2]] main.main(["Foo", "Bar"]) parse_args.assert_called_with(["Foo", "Bar"]) - parse_config.assert_called_with("config") + parse_config.assert_called_with(args_mock) fetch_markets.assert_called_with("pg_config", "user") start.assert_called_once_with() self.assertEqual(2, process.call_count) process.assert_has_calls([ mock.call.__bool__(), - mock.call("config1", 3, 1, args_mock, "report_path", "pg_config"), + mock.call("config1", 3, 1, args_mock, "pg_config"), mock.call.__bool__(), - mock.call("config2", 1, 2, args_mock, "report_path", "pg_config"), + mock.call("config2", 1, 2, args_mock, "pg_config"), ]) @mock.patch.object(main.sys, "exit") - @mock.patch("main.configparser") @mock.patch("main.os") - def test_parse_config(self, os, configparser, exit): - with self.subTest(pg_config=True, report_path=None): - config_mock = mock.MagicMock() - configparser.ConfigParser.return_value = config_mock - def config(element): - return element == "postgresql" - - config_mock.__contains__.side_effect = config - config_mock.__getitem__.return_value = "pg_config" - - result = main.parse_config("configfile") - - config_mock.read.assert_called_with("configfile") - - self.assertEqual(["pg_config", None], result) - - with self.subTest(pg_config=True, report_path="present"): - config_mock = mock.MagicMock() - configparser.ConfigParser.return_value = config_mock + def test_parse_config(self, os, exit): + with self.subTest(report_path=None): + args = main.configargparse.Namespace(**{ + "db_host": "host", + "db_port": "port", + "db_user": "user", + "db_password": "password", + "db_database": "database", + "report_path": None, + }) - config_mock.__contains__.return_value = True - config_mock.__getitem__.side_effect = [ - {"report_path": "report_path"}, - {"report_path": "report_path"}, - "pg_config", - ] + result = main.parse_config(args) + self.assertEqual({ "host": "host", "port": "port", "user": + "user", "password": "password", "database": "database" + }, result) + with self.assertRaises(AttributeError): + args.db_password + + with self.subTest(report_path="present"): + args = main.configargparse.Namespace(**{ + "db_host": "host", + "db_port": "port", + "db_user": "user", + "db_password": "password", + "db_database": "database", + "report_path": "report_path", + }) os.path.exists.return_value = False - result = main.parse_config("configfile") - config_mock.read.assert_called_with("configfile") - self.assertEqual(["pg_config", "report_path"], result) + result = main.parse_config(args) + os.path.exists.assert_called_once_with("report_path") os.makedirs.assert_called_once_with("report_path") - with self.subTest(pg_config=False),\ - mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: - config_mock = mock.MagicMock() - configparser.ConfigParser.return_value = config_mock - result = main.parse_config("configfile") - - config_mock.read.assert_called_with("configfile") - exit.assert_called_once_with(1) - self.assertEqual("no configuration for postgresql in config file\n", stdout_mock.getvalue()) - - @mock.patch.object(main.sys, "exit") - def test_parse_args(self, exit): + def test_parse_args(self): with self.subTest(config="config.ini"): args = main.parse_args([]) self.assertEqual("config.ini", args.config) @@ -4737,13 +4735,10 @@ class MainTest(WebMockTestCase): self.assertTrue(args.after) self.assertTrue(args.debug) - exit.assert_not_called() - - with self.subTest(config="inexistant"),\ - mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: + with self.subTest(config="inexistant"), \ + self.assertRaises(SystemExit), \ + mock.patch('sys.stderr', new_callable=StringIO) as stdout_mock: args = main.parse_args(["--config", "foo.bar"]) - exit.assert_called_once_with(1) - self.assertEqual("no config file found, exiting\n", stdout_mock.getvalue()) @mock.patch.object(main, "psycopg2") def test_fetch_markets(self, psycopg2):