]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - test.py
Merge branch 'dev'
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / test.py
diff --git a/test.py b/test.py
index a9a80c5714cda8e68312194e4e88a5dc50dd2f19..854e27b1089bf6f985778242a550eca9340a0c25 100644 (file)
--- 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()
@@ -71,7 +72,8 @@ class poloniexETest(unittest.TestCase):
         super().tearDown()
 
     def test__init(self):
-        with mock.patch("market.ccxt.poloniexE.session") as session:
+        with self.subTest("Nominal case"), \
+                mock.patch("market.ccxt.poloniexE.session") as session:
             session.request.return_value = "response"
             ccxt = market.ccxt.poloniexE()
             ccxt._market = mock.Mock
@@ -82,6 +84,21 @@ class poloniexETest(unittest.TestCase):
             ccxt._market.report.log_http_request.assert_called_with('GET', 'URL', 'data',
                     'headers', 'response')
 
+        with self.subTest("Raising"),\
+                mock.patch("market.ccxt.poloniexE.session") as session:
+            session.request.side_effect = market.ccxt.RequestException("Boo")
+
+            ccxt = market.ccxt.poloniexE()
+            ccxt._market = mock.Mock
+            ccxt._market.report = mock.Mock()
+
+            with self.assertRaises(market.ccxt.RequestException, msg="Boo") as cm:
+                ccxt.session.request("GET", "URL", data="data",
+                        headers="headers")
+            ccxt._market.report.log_http_request.assert_called_with('GET', 'URL', 'data',
+                    'headers', cm.exception)
+
+
     def test_nanoseconds(self):
         with mock.patch.object(market.ccxt.time, "time") as time:
             time.return_value = 123456.7890123456
@@ -1181,9 +1198,12 @@ class MarketTest(WebMockTestCase):
             with self.subTest(quiet=False):
                 m = market.Market(self.ccxt, self.market_args(quiet=False))
                 report_store.assert_called_with(m, verbose_print=True)
+                report_store().log_market.assert_called_once()
+            report_store.reset_mock()
             with self.subTest(quiet=True):
                 m = market.Market(self.ccxt, self.market_args(quiet=True))
                 report_store.assert_called_with(m, verbose_print=False)
+                report_store().log_market.assert_called_once()
 
     @mock.patch("market.ccxt")
     def test_from_config(self, ccxt):
@@ -1613,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,\
@@ -1630,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,\
@@ -1678,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,\
@@ -1690,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,\
@@ -1706,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,\
@@ -1722,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,\
@@ -4346,6 +4383,34 @@ class ReportStoreTest(WebMockTestCase):
             'response': 'Hey'
             })
 
+        add_log.reset_mock()
+        report_store.log_http_request("method", "url", "body",
+                "headers", ValueError("Foo"))
+        add_log.assert_called_once_with({
+            'type': 'http_request',
+            'method': 'method',
+            'url': 'url',
+            'body': 'body',
+            'headers': 'headers',
+            'status': -1,
+            'response': None,
+            'error': 'ValueError',
+            'error_message': 'Foo',
+            })
+
+    @mock.patch.object(market.ReportStore, "add_log")
+    def test_log_market(self, add_log):
+        report_store = market.ReportStore(self.m)
+
+        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": { "report_path": None, "debug": True, "quiet": False },
+            "user_id": 4,
+            "market_id": 1,
+            })
+
     @mock.patch.object(market.ReportStore, "print_log")
     @mock.patch.object(market.ReportStore, "add_log")
     def test_log_error(self, add_log, print_log):
@@ -4550,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):
@@ -4571,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,\
@@ -4599,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)
@@ -4684,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):