args.append("--debug")
args = parse_args(args)
parse_config(args)
- market_id, market_config, user_id = list(fetch_markets(str(user_id)))[0]
- return market.Market.from_config(market_config, args, user_id=user_id)
+ market_id, market_config, user_id, options = list(fetch_markets(str(user_id)))[0]
+ return market.Market.from_config(market_config, args, user_id=user_id, options=options)
def fetch_markets(user):
cursor = dbs.psql.cursor()
if user is None:
- cursor.execute("SELECT id,config,user_id FROM market_configs WHERE status='enabled'")
+ cursor.execute("SELECT id,config,user_id,portfolio_profile FROM market_configs_augmented WHERE status='enabled'")
else:
- cursor.execute("SELECT id,config,user_id FROM market_configs WHERE status='enabled' AND user_id = %s", [user])
+ cursor.execute("SELECT id,config,user_id,portfolio_profile FROM market_configs_augmented WHERE status='enabled' AND user_id = %s", [user])
for row in cursor:
- yield row
+ options = {
+ "liquidity": parse_liquidity(row[3])
+ }
+ yield row[0:3] + (options,)
+
+def parse_liquidity(value):
+ if value == "high-liquidity":
+ return "high"
+ elif value == "medium-liquidity":
+ return "medium"
+ else:
+ return None
def parse_config(args):
if args.db_host is not None:
parsed.action = ["sell_all"]
return parsed
-def process(market_config, market_id, user_id, args):
+def process(market_config, market_id, user_id, args, options):
try:
market.Market\
.from_config(market_config, args, market_id=market_id,
- user_id=user_id)\
+ user_id=user_id, options=options)\
.process(args.action, before=args.before, after=args.after)
except Exception as e:
print("{}: {}".format(e.__class__.__name__, e))
else:
process_ = process
- for market_id, market_config, user_id in fetch_markets(args.user):
- process_(market_config, market_id, user_id, args)
+ for market_id, market_config, user_id, options in fetch_markets(args.user):
+ process_(market_config, market_id, user_id, args, options)
if args.parallel:
for thread in threads:
mock.patch("main.parse_config") as main_parse_config:
with self.subTest(debug=False):
main_parse_args.return_value = self.market_args()
- 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_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:
args_mock.after = "after"
self.assertEqual("", stdout_mock.getvalue())
- main.process("config", 3, 1, args_mock)
+ main.process("config", 3, 1, args_mock, "options")
market_mock.from_config.assert_has_calls([
- mock.call("config", args_mock, 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)
+ main.process(3, "config", 1, args_mock, "options")
self.assertEqual("Exception: boo\n", stdout_mock.getvalue())
def test_main(self):
args_mock.user = "user"
parse_args.return_value = args_mock
- 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"])
self.assertEqual(2, process.call_count)
process.assert_has_calls([
- mock.call("config1", 3, 1, args_mock),
- mock.call("config2", 1, 2, args_mock),
+ 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,\
args_mock.user = "user"
parse_args.return_value = args_mock
- 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"])
self.assertEqual(2, process.call_count)
process.assert_has_calls([
mock.call.__bool__(),
- mock.call("config1", 3, 1, args_mock),
+ mock.call("config1", 3, 1, args_mock, "options"),
mock.call.__bool__(),
- mock.call("config2", 1, 2, args_mock),
+ 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,\
args_mock.user = "user"
parse_args.return_value = args_mock
- 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"])
args_mock.user = "user"
parse_args.return_value = args_mock
- 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"])
@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")
+ ]
psql.cursor.return_value = cursor_mock
with self.subTest(user=None):
rows = list(main.fetch_markets(None))
- cursor_mock.execute.assert_called_once_with("SELECT id,config,user_id FROM market_configs WHERE status='enabled'")
+ 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)
cursor_mock.execute.reset_mock()
with self.subTest(user=1):
rows = list(main.fetch_markets(1))
- cursor_mock.execute.assert_called_once_with("SELECT id,config,user_id FROM market_configs WHERE status='enabled' AND 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)