]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/blobdiff - tests/test_main.py
Don’t raise when some market is disabled
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git] / tests / test_main.py
index d2f80297b37f6b499289944d723020e889e01be9..3735a3be58bbe81a7cd948829b77eee27e0249be 100644 (file)
@@ -103,25 +103,29 @@ class MainTest(WebMockTestCase):
                 mock.patch("main.parse_config") as main_parse_config:
             with self.subTest(debug=False):
                 main_parse_args.return_value = self.market_args()
-                main_parse_config.return_value = "pg_config"
-                main_fetch_markets.return_value = [(1, {"key": "market_config"}, 3)]
+                main_fetch_markets.return_value = [(1, {"key": "market_config"}, 3, { "foo": "bar" })]
                 m = main.get_user_market("config_path.ini", 1)
 
                 self.assertIsInstance(m, market.Market)
                 self.assertFalse(m.debug)
+                self.assertEqual("bar", m.options["foo"])
                 main_parse_args.assert_called_once_with(["--config", "config_path.ini"])
 
             main_parse_args.reset_mock()
             with self.subTest(debug=True):
                 main_parse_args.return_value = self.market_args(debug=True)
-                main_parse_config.return_value = "pg_config"
-                main_fetch_markets.return_value = [(1, {"key": "market_config"}, 3)]
+                main_fetch_markets.return_value = [(1, {"key": "market_config"}, 3, { "foo": "bar" })]
                 m = main.get_user_market("config_path.ini", 1, debug=True)
 
                 self.assertIsInstance(m, market.Market)
                 self.assertTrue(m.debug)
                 main_parse_args.assert_called_once_with(["--config", "config_path.ini", "--debug"])
 
+    def test_parse_liquidity(self):
+        self.assertEqual("high", main.parse_liquidity("high-liquidity"))
+        self.assertEqual("medium", main.parse_liquidity("medium-liquidity"))
+        self.assertIsNone(main.parse_liquidity("foo"))
+
     def test_process(self):
         with mock.patch("market.Market") as market_mock,\
                 mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
@@ -135,16 +139,16 @@ class MainTest(WebMockTestCase):
             args_mock.after = "after"
             self.assertEqual("", stdout_mock.getvalue())
 
-            main.process("config", 3, 1, args_mock, "pg_config")
+            main.process("config", 3, 1, args_mock, "options")
 
             market_mock.from_config.assert_has_calls([
-                mock.call("config", args_mock, pg_config="pg_config", market_id=3, user_id=1),
+                mock.call("config", args_mock, market_id=3, user_id=1, options="options"),
                 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, args_mock, "pg_config")
+                main.process(3, "config", 1, args_mock, "options")
                 self.assertEqual("Exception: boo\n", stdout_mock.getvalue())
 
     def test_main(self):
@@ -159,21 +163,23 @@ class MainTest(WebMockTestCase):
                 args_mock.user = "user"
                 parse_args.return_value = args_mock
 
-                parse_config.return_value = "pg_config"
-
-                fetch_markets.return_value = [[3, "config1", 1], [1, "config2", 2]]
+                fetch_markets.return_value = [
+                        [3, "config1", 1, "options"],
+                        [1, "config2", 2, "options"]
+                ]
 
                 main.main(["Foo", "Bar"])
 
                 parse_args.assert_called_with(["Foo", "Bar"])
                 parse_config.assert_called_with(args_mock)
-                fetch_markets.assert_called_with("pg_config", "user")
+                fetch_markets.assert_called_with("user")
 
                 self.assertEqual(2, process.call_count)
                 process.assert_has_calls([
-                    mock.call("config1", 3, 1, args_mock, "pg_config"),
-                    mock.call("config2", 1, 2, args_mock, "pg_config"),
+                    mock.call("config1", 3, 1, args_mock, "options"),
+                    mock.call("config2", 1, 2, args_mock, "options"),
                     ])
+
         with self.subTest(parallel=True):
             with mock.patch("main.parse_args") as parse_args,\
                     mock.patch("main.parse_config") as parse_config,\
@@ -187,25 +193,27 @@ class MainTest(WebMockTestCase):
                 args_mock.user = "user"
                 parse_args.return_value = args_mock
 
-                parse_config.return_value = "pg_config"
-
-                fetch_markets.return_value = [[3, "config1", 1], [1, "config2", 2]]
+                fetch_markets.return_value = [
+                        [3, "config1", 1, "options"],
+                        [1, "config2", 2, "options"]
+                ]
 
                 main.main(["Foo", "Bar"])
 
                 parse_args.assert_called_with(["Foo", "Bar"])
                 parse_config.assert_called_with(args_mock)
-                fetch_markets.assert_called_with("pg_config", "user")
+                fetch_markets.assert_called_with("user")
 
                 stop.assert_called_once_with()
                 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, "pg_config"),
+                    mock.call("config1", 3, 1, args_mock, "options"),
                     mock.call.__bool__(),
-                    mock.call("config2", 1, 2, args_mock, "pg_config"),
+                    mock.call("config2", 1, 2, args_mock, "options"),
                     ])
+
         with self.subTest(quiet=True):
             with mock.patch("main.parse_args") as parse_args,\
                     mock.patch("main.parse_config") as parse_config,\
@@ -219,9 +227,10 @@ class MainTest(WebMockTestCase):
                 args_mock.user = "user"
                 parse_args.return_value = args_mock
 
-                parse_config.return_value = "pg_config"
-
-                fetch_markets.return_value = [[3, "config1", 1], [1, "config2", 2]]
+                fetch_markets.return_value = [
+                        [3, "config1", 1, "options"],
+                        [1, "config2", 2, "options"]
+                ]
 
                 main.main(["Foo", "Bar"])
 
@@ -240,9 +249,10 @@ class MainTest(WebMockTestCase):
                 args_mock.user = "user"
                 parse_args.return_value = args_mock
 
-                parse_config.return_value = "pg_config"
-
-                fetch_markets.return_value = [[3, "config1", 1], [1, "config2", 2]]
+                fetch_markets.return_value = [
+                        [3, "config1", 1, "options"],
+                        [1, "config2", 2, "options"]
+                ]
 
                 main.main(["Foo", "Bar"])
 
@@ -252,36 +262,57 @@ class MainTest(WebMockTestCase):
     @mock.patch.object(main.sys, "exit")
     @mock.patch("main.os")
     def test_parse_config(self, os, exit):
-        with self.subTest(report_path=None):
+        with self.subTest(report_path=None),\
+                mock.patch.object(main.dbs, "connect_psql") as psql,\
+                mock.patch.object(main.dbs, "connect_redis") as redis:
+            args = main.configargparse.Namespace(**{
+                "db_host": "host",
+                "redis_host": "rhost",
+                "report_path": None,
+                })
+
+            main.parse_config(args)
+            psql.assert_called_once_with(args)
+            redis.assert_called_once_with(args)
+
+        with self.subTest(report_path=None, db=None),\
+                mock.patch.object(main.dbs, "connect_psql") as psql,\
+                mock.patch.object(main.dbs, "connect_redis") as redis:
+            args = main.configargparse.Namespace(**{
+                "db_host": None,
+                "redis_host": "rhost",
+                "report_path": None,
+                })
+
+            main.parse_config(args)
+            psql.assert_not_called()
+            redis.assert_called_once_with(args)
+
+        with self.subTest(report_path=None, redis=None),\
+                mock.patch.object(main.dbs, "connect_psql") as psql,\
+                mock.patch.object(main.dbs, "connect_redis") as redis:
             args = main.configargparse.Namespace(**{
                 "db_host": "host",
-                "db_port": "port",
-                "db_user": "user",
-                "db_password": "password",
-                "db_database": "database",
+                "redis_host": None,
                 "report_path": None,
                 })
 
-            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
+            main.parse_config(args)
+            redis.assert_not_called()
+            psql.assert_called_once_with(args)
 
-        with self.subTest(report_path="present"):
+        with self.subTest(report_path="present"),\
+                mock.patch.object(main.dbs, "connect_psql") as psql,\
+                mock.patch.object(main.dbs, "connect_redis") as redis:
             args = main.configargparse.Namespace(**{
                 "db_host": "host",
-                "db_port": "port",
-                "db_user": "user",
-                "db_password": "password",
-                "db_database": "database",
+                "redis_host": "rhost",
                 "report_path": "report_path",
                 })
 
             os.path.exists.return_value = False
 
-            result = main.parse_config(args)
+            main.parse_config(args)
 
             os.path.exists.assert_called_once_with("report_path")
             os.makedirs.assert_called_once_with("report_path")
@@ -304,31 +335,35 @@ class MainTest(WebMockTestCase):
                 mock.patch('sys.stderr', new_callable=StringIO) as stdout_mock:
             args = main.parse_args(["--config", "foo.bar"])
 
-    @mock.patch.object(main, "psycopg2")
-    def test_fetch_markets(self, psycopg2):
-        connect_mock = mock.Mock()
+    @mock.patch.object(main.dbs, "psql")
+    def test_fetch_markets(self, psql):
         cursor_mock = mock.MagicMock()
-        cursor_mock.__iter__.return_value = ["row_1", "row_2"]
+        cursor_mock.__iter__.return_value = [
+                (1, "cfg", 1, "high-liquidity"),
+                (2, "cfg2", 3, "medium-liquidity")
+                ]
 
-        connect_mock.cursor.return_value = cursor_mock
-        psycopg2.connect.return_value = connect_mock
+        psql.cursor.return_value = cursor_mock
 
         with self.subTest(user=None):
-            rows = list(main.fetch_markets({"foo": "bar"}, None))
+            rows = list(main.fetch_markets(None))
 
-            psycopg2.connect.assert_called_once_with(foo="bar")
-            cursor_mock.execute.assert_called_once_with("SELECT id,config,user_id FROM market_configs")
+            cursor_mock.execute.assert_called_once_with("SELECT id,config,user_id,portfolio_profile FROM market_configs_augmented WHERE status='enabled'")
 
-            self.assertEqual(["row_1", "row_2"], rows)
+            self.assertEqual([
+                (1, 'cfg', 1, {'liquidity': 'high'}),
+                (2, 'cfg2', 3, {'liquidity': 'medium'})
+                ], rows)
 
-        psycopg2.connect.reset_mock()
         cursor_mock.execute.reset_mock()
         with self.subTest(user=1):
-            rows = list(main.fetch_markets({"foo": "bar"}, 1))
+            rows = list(main.fetch_markets(1))
 
-            psycopg2.connect.assert_called_once_with(foo="bar")
-            cursor_mock.execute.assert_called_once_with("SELECT id,config,user_id FROM market_configs WHERE user_id = %s", 1)
+            cursor_mock.execute.assert_called_once_with("SELECT id,config,user_id,portfolio_profile FROM market_configs_augmented WHERE status='enabled' AND user_id = %s", [1])
 
-            self.assertEqual(["row_1", "row_2"], rows)
+            self.assertEqual([
+                (1, 'cfg', 1, {'liquidity': 'high'}),
+                (2, 'cfg2', 3, {'liquidity': 'medium'})
+                ], rows)