--- /dev/null
+import psycopg2
+import redis as _redis
+
+redis = None
+psql = None
+
+def redis_connected():
+ global redis
+ if redis is None:
+ return False
+ else:
+ try:
+ return redis.ping()
+ except Exception:
+ return False
+
+def psql_connected():
+ global psql
+ return psql is not None and psql.closed == 0
+
+def connect_redis(args):
+ global redis
+
+ redis_config = {
+ "host": args.redis_host,
+ "port": args.redis_port,
+ "db": args.redis_database,
+ }
+ if redis_config["host"].startswith("/"):
+ redis_config["unix_socket_path"] = redis_config.pop("host")
+ del(redis_config["port"])
+ del(args.redis_host)
+ del(args.redis_port)
+ del(args.redis_database)
+
+ if redis is None:
+ redis = _redis.Redis(**redis_config)
+
+def connect_psql(args):
+ global psql
+ pg_config = {
+ "host": args.db_host,
+ "port": args.db_port,
+ "user": args.db_user,
+ "password": args.db_password,
+ "database": args.db_database,
+ }
+ del(args.db_host)
+ del(args.db_port)
+ del(args.db_user)
+ del(args.db_password)
+ del(args.db_database)
+
+ if psql is None:
+ psql = psycopg2.connect(**pg_config)
import configargparse
-import psycopg2
+import dbs
import os
import sys
if debug:
args.append("--debug")
args = parse_args(args)
- pg_config, redis_config = parse_config(args)
- market_id, market_config, user_id = list(fetch_markets(pg_config, str(user_id)))[0]
- return market.Market.from_config(market_config, args,
- pg_config=pg_config, market_id=market_id,
- user_id=user_id)
+ 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)
-def fetch_markets(pg_config, user):
- connection = psycopg2.connect(**pg_config)
- cursor = connection.cursor()
+def fetch_markets(user):
+ cursor = dbs.psql.cursor()
if user is None:
cursor.execute("SELECT id,config,user_id FROM market_configs")
yield row
def parse_config(args):
- pg_config = {
- "host": args.db_host,
- "port": args.db_port,
- "user": args.db_user,
- "password": args.db_password,
- "database": args.db_database,
- }
- del(args.db_host)
- del(args.db_port)
- del(args.db_user)
- del(args.db_password)
- del(args.db_database)
-
- redis_config = {
- "host": args.redis_host,
- "port": args.redis_port,
- "db": args.redis_database,
- }
- if redis_config["host"].startswith("/"):
- redis_config["unix_socket_path"] = redis_config.pop("host")
- del(redis_config["port"])
- del(args.redis_host)
- del(args.redis_port)
- del(args.redis_database)
+ if args.db_host is not None:
+ dbs.connect_psql(args)
+
+ if args.redis_host is not None:
+ dbs.connect_redis(args)
report_path = args.report_path
os.path.exists(report_path):
os.makedirs(report_path)
- return pg_config, redis_config
-
def parse_args(argv):
parser = configargparse.ArgumentParser(
description="Run the trade bot.")
parsed.action = ["sell_all"]
return parsed
-def process(market_config, market_id, user_id, args, pg_config, redis_config):
+def process(market_config, market_id, user_id, args):
try:
market.Market\
.from_config(market_config, args, market_id=market_id,
- pg_config=pg_config, redis_config=redis_config,
user_id=user_id)\
.process(args.action, before=args.before, after=args.after)
except Exception as e:
def main(argv):
args = parse_args(argv)
- pg_config, redis_config = parse_config(args)
+ parse_config(args)
market.Portfolio.report.set_verbose(not args.quiet)
else:
process_ = process
- for market_id, market_config, user_id in fetch_markets(pg_config, args.user):
- process_(market_config, market_id, user_id, args, pg_config, redis_config)
+ for market_id, market_config, user_id in fetch_markets(args.user):
+ process_(market_config, market_id, user_id, args)
if args.parallel:
for thread in threads:
from ccxt import ExchangeError, NotSupported, RequestTimeout, InvalidNonce
import ccxt_wrapper as ccxt
import time
-import psycopg2
-import redis
+import dbs
from store import *
from cachetools.func import ttl_cache
from datetime import datetime
self.balances = BalanceStore(self)
self.processor = Processor(self)
- for key in ["user_id", "market_id", "pg_config", "redis_config"]:
+ for key in ["user_id", "market_id"]:
setattr(self, key, kwargs.get(key, None))
self.report.log_market(self.args)
date = datetime.datetime.now()
if self.args.report_path is not None:
self.store_file_report(date)
- if self.pg_config is not None and self.args.report_db:
+ if dbs.psql_connected() and self.args.report_db:
self.store_database_report(date)
- if self.redis_config is not None and self.args.report_redis:
+ if dbs.redis_connected() and self.args.report_redis:
self.store_redis_report(date)
def store_file_report(self, date):
try:
report_query = 'INSERT INTO reports("date", "market_config_id", "debug") VALUES (%s, %s, %s) RETURNING id;'
line_query = 'INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);'
- connection = psycopg2.connect(**self.pg_config)
- cursor = connection.cursor()
+ cursor = dbs.psql.cursor()
cursor.execute(report_query, (date, self.market_id, self.debug))
report_id = cursor.fetchone()[0]
for date, type_, payload in self.report.to_json_array():
cursor.execute(line_query, (date, report_id, type_, payload))
- connection.commit()
+ dbs.psql.commit()
cursor.close()
- connection.close()
except Exception as e:
print("impossible to store report to database: {}; {}".format(e.__class__.__name__, e))
def store_redis_report(self, date):
try:
- conn = redis.Redis(**self.redis_config)
for type_, log in self.report.to_json_redis():
key = "/cryptoportfolio/{}/{}/{}".format(self.market_id, date.isoformat(), type_)
- conn.set(key, log, ex=31*24*60*60)
+ dbs.redis.set(key, log, ex=31*24*60*60)
key = "/cryptoportfolio/{}/latest/{}".format(self.market_id, type_)
- conn.set(key, log)
+ dbs.redis.set(key, log)
key = "/cryptoportfolio/{}/latest/date".format(self.market_id)
- conn.set(key, date.isoformat())
+ dbs.redis.set(key, date.isoformat())
except Exception as e:
print("impossible to store report to redis: {}; {}".format(e.__class__.__name__, e))
from tests.test_market import *
from tests.test_store import *
from tests.test_portfolio import *
+ from tests.test_dbs import *
if "acceptance" in limits:
from tests.test_acceptance import *
from unittest import mock
import requests_mock
from io import StringIO
-import portfolio, market, main, store
+import portfolio, market, main, store, dbs
__all__ = ["limits", "unittest", "WebMockTestCase", "mock", "D",
"StringIO"]
callback=None),
mock.patch.multiple(portfolio.Computation,
computations=portfolio.Computation.computations),
+ mock.patch.multiple(dbs,
+ redis=None, psql=None)
]
for patcher in self.patchers:
patcher.start()
--- /dev/null
+from .helper import *
+import dbs, main
+
+@unittest.skipUnless("unit" in limits, "Unit skipped")
+class DbsTest(WebMockTestCase):
+ @mock.patch.object(dbs, "psycopg2")
+ def test_connect_psql(self, psycopg2):
+ args = main.configargparse.Namespace(**{
+ "db_host": "host",
+ "db_port": "port",
+ "db_user": "user",
+ "db_password": "password",
+ "db_database": "database",
+ })
+ psycopg2.connect.return_value = "pg_connection"
+ dbs.connect_psql(args)
+
+ psycopg2.connect.assert_called_once_with(host="host",
+ port="port", user="user", password="password",
+ database="database")
+ self.assertEqual("pg_connection", dbs.psql)
+ with self.assertRaises(AttributeError):
+ args.db_password
+
+ psycopg2.connect.reset_mock()
+ args = main.configargparse.Namespace(**{
+ "db_host": "host",
+ "db_port": "port",
+ "db_user": "user",
+ "db_password": "password",
+ "db_database": "database",
+ })
+ dbs.connect_psql(args)
+ psycopg2.connect.assert_not_called()
+
+ @mock.patch.object(dbs, "_redis")
+ def test_connect_redis(self, redis):
+ with self.subTest(redis_host="tcp"):
+ args = main.configargparse.Namespace(**{
+ "redis_host": "host",
+ "redis_port": "port",
+ "redis_database": "database",
+ })
+ redis.Redis.return_value = "redis_connection"
+ dbs.connect_redis(args)
+
+ redis.Redis.assert_called_once_with(host="host",
+ port="port", db="database")
+ self.assertEqual("redis_connection", dbs.redis)
+ with self.assertRaises(AttributeError):
+ args.redis_database
+
+ redis.Redis.reset_mock()
+ args = main.configargparse.Namespace(**{
+ "redis_host": "host",
+ "redis_port": "port",
+ "redis_database": "database",
+ })
+ dbs.connect_redis(args)
+ redis.Redis.assert_not_called()
+
+ dbs.redis = None
+ with self.subTest(redis_host="socket"):
+ args = main.configargparse.Namespace(**{
+ "redis_host": "/run/foo",
+ "redis_port": "port",
+ "redis_database": "database",
+ })
+ redis.Redis.return_value = "redis_socket"
+ dbs.connect_redis(args)
+
+ redis.Redis.assert_called_once_with(unix_socket_path="/run/foo", db="database")
+ self.assertEqual("redis_socket", dbs.redis)
+
+ def test_redis_connected(self):
+ with self.subTest(redis=None):
+ dbs.redis = None
+ self.assertFalse(dbs.redis_connected())
+
+ with self.subTest(redis="mocked_true"):
+ dbs.redis = mock.Mock()
+ dbs.redis.ping.return_value = True
+ self.assertTrue(dbs.redis_connected())
+
+ with self.subTest(redis="mocked_false"):
+ dbs.redis = mock.Mock()
+ dbs.redis.ping.return_value = False
+ self.assertFalse(dbs.redis_connected())
+
+ with self.subTest(redis="mocked_raise"):
+ dbs.redis = mock.Mock()
+ dbs.redis.ping.side_effect = Exception("bouh")
+ self.assertFalse(dbs.redis_connected())
+
+ def test_psql_connected(self):
+ with self.subTest(psql=None):
+ dbs.psql = None
+ self.assertFalse(dbs.psql_connected())
+
+ with self.subTest(psql="connected"):
+ dbs.psql = mock.Mock()
+ dbs.psql.closed = 0
+ self.assertTrue(dbs.psql_connected())
+
+ with self.subTest(psql="not connected"):
+ dbs.psql = mock.Mock()
+ dbs.psql.closed = 3
+ self.assertFalse(dbs.psql_connected())
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", "redis_config"]
main_fetch_markets.return_value = [(1, {"key": "market_config"}, 3)]
m = main.get_user_market("config_path.ini", 1)
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", "redis_config"]
main_fetch_markets.return_value = [(1, {"key": "market_config"}, 3)]
m = main.get_user_market("config_path.ini", 1, debug=True)
args_mock.after = "after"
self.assertEqual("", stdout_mock.getvalue())
- main.process("config", 3, 1, args_mock, "pg_config", "redis_config")
+ main.process("config", 3, 1, args_mock)
market_mock.from_config.assert_has_calls([
- mock.call("config", args_mock, pg_config="pg_config", redis_config="redis_config", market_id=3, user_id=1),
+ mock.call("config", args_mock, 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, args_mock, "pg_config", "redis_config")
+ main.process(3, "config", 1, args_mock)
self.assertEqual("Exception: boo\n", stdout_mock.getvalue())
def test_main(self):
args_mock.user = "user"
parse_args.return_value = args_mock
- parse_config.return_value = ["pg_config", "redis_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(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", "redis_config"),
- mock.call("config2", 1, 2, args_mock, "pg_config", "redis_config"),
+ mock.call("config1", 3, 1, args_mock),
+ mock.call("config2", 1, 2, args_mock),
])
with self.subTest(parallel=True):
with mock.patch("main.parse_args") as parse_args,\
args_mock.user = "user"
parse_args.return_value = args_mock
- parse_config.return_value = ["pg_config", "redis_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(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", "redis_config"),
+ mock.call("config1", 3, 1, args_mock),
mock.call.__bool__(),
- mock.call("config2", 1, 2, args_mock, "pg_config", "redis_config"),
+ mock.call("config2", 1, 2, args_mock),
])
with self.subTest(quiet=True):
with mock.patch("main.parse_args") as parse_args,\
args_mock.user = "user"
parse_args.return_value = args_mock
- parse_config.return_value = ["pg_config", "redis_config"]
-
fetch_markets.return_value = [[3, "config1", 1], [1, "config2", 2]]
main.main(["Foo", "Bar"])
args_mock.user = "user"
parse_args.return_value = args_mock
- parse_config.return_value = ["pg_config", "redis_config"]
-
fetch_markets.return_value = [[3, "config1", 1], [1, "config2", 2]]
main.main(["Foo", "Bar"])
@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",
- "db_port": "port",
- "db_user": "user",
- "db_password": "password",
- "db_database": "database",
"redis_host": "rhost",
- "redis_port": "rport",
- "redis_database": "rdb",
"report_path": None,
})
- db_config, redis_config = main.parse_config(args)
- self.assertEqual({ "host": "host", "port": "port", "user":
- "user", "password": "password", "database": "database"
- }, db_config)
- self.assertEqual({ "host": "rhost", "port": "rport", "db":
- "rdb"}, redis_config)
+ 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,
+ })
- with self.assertRaises(AttributeError):
- args.db_password
- with self.assertRaises(AttributeError):
- args.redis_host
+ main.parse_config(args)
+ psql.assert_not_called()
+ redis.assert_called_once_with(args)
- with self.subTest(redis_host="socket"):
+ 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": "/run/foo",
- "redis_port": "rport",
- "redis_database": "rdb",
+ "redis_host": None,
"report_path": None,
})
- db_config, redis_config = main.parse_config(args)
- self.assertEqual({ "unix_socket_path": "/run/foo", "db": "rdb"}, redis_config)
+ 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",
- "redis_port": "rport",
- "redis_database": "rdb",
"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")
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"]
- 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")
self.assertEqual(["row_1", "row_2"], 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)
self.assertEqual(["row_1", "row_2"], rows)
from .helper import *
-import market, store, portfolio
+import market, store, portfolio, dbs
import datetime
@unittest.skipUnless("unit" in limits, "Unit skipped")
self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;")
- @mock.patch.object(market, "psycopg2")
- def test_store_database_report(self, psycopg2):
- connect_mock = mock.Mock()
+ @mock.patch.object(dbs, "psql")
+ def test_store_database_report(self, psql):
cursor_mock = mock.MagicMock()
- connect_mock.cursor.return_value = cursor_mock
- psycopg2.connect.return_value = connect_mock
+ psql.cursor.return_value = cursor_mock
m = market.Market(self.ccxt, self.market_args(),
pg_config={"config": "pg_config"}, user_id=1)
cursor_mock.fetchone.return_value = [42]
("date2", "type2", "payload2"),
]
m.store_database_report(datetime.datetime(2018, 3, 24))
- connect_mock.assert_has_calls([
+ psql.assert_has_calls([
mock.call.cursor(),
mock.call.cursor().execute('INSERT INTO reports("date", "market_config_id", "debug") VALUES (%s, %s, %s) RETURNING id;', (datetime.datetime(2018, 3, 24), None, False)),
mock.call.cursor().fetchone(),
mock.call.cursor().execute('INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);', ('date2', 42, 'type2', 'payload2')),
mock.call.commit(),
mock.call.cursor().close(),
- mock.call.close()
])
- connect_mock.reset_mock()
with self.subTest(error=True),\
mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
- psycopg2.connect.side_effect = Exception("Bouh")
+ psql.cursor.side_effect = Exception("Bouh")
m.store_database_report(datetime.datetime(2018, 3, 24))
self.assertEqual(stdout_mock.getvalue(), "impossible to store report to database: Exception; Bouh\n")
- @mock.patch.object(market, "redis")
+ @mock.patch.object(dbs, "redis")
def test_store_redis_report(self, redis):
- connect_mock = mock.Mock()
- redis.Redis.return_value = connect_mock
-
m = market.Market(self.ccxt, self.market_args(),
redis_config={"config": "redis_config"}, market_id=1)
("type2", "payload2"),
]
m.store_redis_report(datetime.datetime(2018, 3, 24))
- connect_mock.assert_has_calls([
+ redis.assert_has_calls([
mock.call.set("/cryptoportfolio/1/2018-03-24T00:00:00/type1", "payload1", ex=31*24*60*60),
mock.call.set("/cryptoportfolio/1/latest/type1", "payload1"),
mock.call.set("/cryptoportfolio/1/2018-03-24T00:00:00/type2", "payload2", ex=31*24*60*60),
mock.call.set("/cryptoportfolio/1/latest/date", "2018-03-24T00:00:00"),
])
- connect_mock.reset_mock()
+ redis.reset_mock()
with self.subTest(error=True),\
mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
- redis.Redis.side_effect = Exception("Bouh")
+ redis.set.side_effect = Exception("Bouh")
m.store_redis_report(datetime.datetime(2018, 3, 24))
self.assertEqual(stdout_mock.getvalue(), "impossible to store report to redis: Exception; Bouh\n")
def test_store_report(self):
m = market.Market(self.ccxt, self.market_args(report_db=False), user_id=1)
- with self.subTest(file=None, pg_config=None),\
+ with self.subTest(file=None, pg_connected=None),\
+ mock.patch.object(dbs, "psql_connected") as psql,\
+ mock.patch.object(dbs, "redis_connected") as redis,\
mock.patch.object(m, "report") as report,\
mock.patch.object(m, "store_database_report") as db_report,\
mock.patch.object(m, "store_redis_report") as redis_report,\
mock.patch.object(m, "store_file_report") as file_report:
+ psql.return_value = False
+ redis.return_value = False
m.store_report()
report.merge.assert_called_with(store.Portfolio.report)
report.reset_mock()
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),\
+ with self.subTest(file="present", pg_connected=None),\
+ mock.patch.object(dbs, "psql_connected") as psql,\
+ mock.patch.object(dbs, "redis_connected") as redis,\
mock.patch.object(m, "report") as report,\
mock.patch.object(m, "store_file_report") as file_report,\
mock.patch.object(m, "store_redis_report") as redis_report,\
mock.patch.object(m, "store_database_report") as db_report,\
mock.patch.object(market.datetime, "datetime") as time_mock:
-
+ psql.return_value = False
+ redis.return_value = False
time_mock.now.return_value = datetime.datetime(2018, 2, 25)
m.store_report()
report.reset_mock()
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),\
+ with self.subTest(file="present", pg_connected=None, report_db=True),\
+ mock.patch.object(dbs, "psql_connected") as psql,\
+ mock.patch.object(dbs, "redis_connected") as redis,\
mock.patch.object(m, "report") as report,\
mock.patch.object(m, "store_file_report") as file_report,\
mock.patch.object(m, "store_redis_report") as redis_report,\
mock.patch.object(m, "store_database_report") as db_report,\
mock.patch.object(market.datetime, "datetime") as time_mock:
-
+ psql.return_value = False
+ redis.return_value = False
time_mock.now.return_value = datetime.datetime(2018, 2, 25)
m.store_report()
redis_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"),\
+ m = market.Market(self.ccxt, self.market_args(report_db=True), user_id=1)
+ with self.subTest(file=None, pg_connected=True),\
+ mock.patch.object(dbs, "psql_connected") as psql,\
+ mock.patch.object(dbs, "redis_connected") as redis,\
mock.patch.object(m, "report") as report,\
mock.patch.object(m, "store_file_report") as file_report,\
mock.patch.object(m, "store_redis_report") as redis_report,\
mock.patch.object(m, "store_database_report") as db_report,\
mock.patch.object(market.datetime, "datetime") as time_mock:
-
+ psql.return_value = True
+ redis.return_value = False
time_mock.now.return_value = datetime.datetime(2018, 2, 25)
m.store_report()
report.reset_mock()
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"),\
+ user_id=1)
+ with self.subTest(file="present", pg_connected=True),\
+ mock.patch.object(dbs, "psql_connected") as psql,\
+ mock.patch.object(dbs, "redis_connected") as redis,\
mock.patch.object(m, "report") as report,\
mock.patch.object(m, "store_file_report") as file_report,\
mock.patch.object(m, "store_redis_report") as redis_report,\
mock.patch.object(m, "store_database_report") as db_report,\
mock.patch.object(market.datetime, "datetime") as time_mock:
-
+ psql.return_value = True
+ redis.return_value = False
time_mock.now.return_value = datetime.datetime(2018, 2, 25)
m.store_report()
report.reset_mock()
m = market.Market(self.ccxt, self.market_args(report_redis=False),
- redis_config="redis_config", user_id=1)
- with self.subTest(redis_config="present", report_redis=False),\
+ user_id=1)
+ with self.subTest(redis_connected=True, report_redis=False),\
+ mock.patch.object(dbs, "psql_connected") as psql,\
+ mock.patch.object(dbs, "redis_connected") as redis,\
mock.patch.object(m, "report") as report,\
mock.patch.object(m, "store_file_report") as file_report,\
mock.patch.object(m, "store_redis_report") as redis_report,\
mock.patch.object(m, "store_database_report") as db_report,\
mock.patch.object(market.datetime, "datetime") as time_mock:
-
+ psql.return_value = False
+ redis.return_value = True
time_mock.now.return_value = datetime.datetime(2018, 2, 25)
m.store_report()
report.reset_mock()
m = market.Market(self.ccxt, self.market_args(report_redis=True),
user_id=1)
- with self.subTest(redis_config="absent", report_redis=True),\
+ with self.subTest(redis_connected=False, report_redis=True),\
+ mock.patch.object(dbs, "psql_connected") as psql,\
+ mock.patch.object(dbs, "redis_connected") as redis,\
mock.patch.object(m, "report") as report,\
mock.patch.object(m, "store_file_report") as file_report,\
mock.patch.object(m, "store_redis_report") as redis_report,\
mock.patch.object(m, "store_database_report") as db_report,\
mock.patch.object(market.datetime, "datetime") as time_mock:
-
+ psql.return_value = False
+ redis.return_value = False
time_mock.now.return_value = datetime.datetime(2018, 2, 25)
m.store_report()
report.reset_mock()
m = market.Market(self.ccxt, self.market_args(report_redis=True),
- redis_config="redis_config", user_id=1)
- with self.subTest(redis_config="present", report_redis=True),\
+ user_id=1)
+ with self.subTest(redis_connected=True, report_redis=True),\
+ mock.patch.object(dbs, "psql_connected") as psql,\
+ mock.patch.object(dbs, "redis_connected") as redis,\
mock.patch.object(m, "report") as report,\
mock.patch.object(m, "store_file_report") as file_report,\
mock.patch.object(m, "store_redis_report") as redis_report,\
mock.patch.object(m, "store_database_report") as db_report,\
mock.patch.object(market.datetime, "datetime") as time_mock:
-
+ psql.return_value = False
+ redis.return_value = True
time_mock.now.return_value = datetime.datetime(2018, 2, 25)
m.store_report()