persistence.init does not need the config dict

This commit is contained in:
Matthias 2019-05-29 19:54:47 +02:00
parent 6451feee0e
commit d7bebc4385
4 changed files with 16 additions and 17 deletions

View File

@ -73,7 +73,7 @@ class FreqtradeBot(object):
self.active_pair_whitelist: List[str] = self.config['exchange']['pair_whitelist'] self.active_pair_whitelist: List[str] = self.config['exchange']['pair_whitelist']
persistence.init(self.config) persistence.init(self.config.get('db_url', None), self.config.get('dry_run', False))
# Set initial bot state from config # Set initial bot state from config
initial_state = self.config.get('initial_state') initial_state = self.config.get('initial_state')

View File

@ -25,7 +25,7 @@ _DECL_BASE: Any = declarative_base()
_SQL_DOCS_URL = 'http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls' _SQL_DOCS_URL = 'http://docs.sqlalchemy.org/en/latest/core/engines.html#database-urls'
def init(config: Dict) -> None: def init(db_url: str, dry_run: bool = False) -> None:
""" """
Initializes this module with the given config, Initializes this module with the given config,
registers all known command handlers registers all known command handlers
@ -33,7 +33,6 @@ def init(config: Dict) -> None:
:param config: config to use :param config: config to use
:return: None :return: None
""" """
db_url = config.get('db_url', None)
kwargs = {} kwargs = {}
# Take care of thread ownership if in-memory db # Take care of thread ownership if in-memory db
@ -57,7 +56,7 @@ def init(config: Dict) -> None:
check_migrate(engine) check_migrate(engine)
# Clean dry_run DB if the db is not in-memory # Clean dry_run DB if the db is not in-memory
if config.get('dry_run', False) and db_url != 'sqlite://': if dry_run and db_url != 'sqlite://':
clean_dry_run_db() clean_dry_run_db()

View File

@ -13,12 +13,12 @@ from freqtrade.tests.conftest import log_has
@pytest.fixture(scope='function') @pytest.fixture(scope='function')
def init_persistence(default_conf): def init_persistence(default_conf):
init(default_conf) init(default_conf['db_url'], default_conf['dry_run'])
def test_init_create_session(default_conf): def test_init_create_session(default_conf):
# Check if init create a session # Check if init create a session
init(default_conf) init(default_conf['db_url'], default_conf['dry_run'])
assert hasattr(Trade, 'session') assert hasattr(Trade, 'session')
assert 'Session' in type(Trade.session).__name__ assert 'Session' in type(Trade.session).__name__
@ -28,7 +28,7 @@ def test_init_custom_db_url(default_conf, mocker):
default_conf.update({'db_url': 'sqlite:///tmp/freqtrade2_test.sqlite'}) default_conf.update({'db_url': 'sqlite:///tmp/freqtrade2_test.sqlite'})
create_engine_mock = mocker.patch('freqtrade.persistence.create_engine', MagicMock()) create_engine_mock = mocker.patch('freqtrade.persistence.create_engine', MagicMock())
init(default_conf) init(default_conf['db_url'], default_conf['dry_run'])
assert create_engine_mock.call_count == 1 assert create_engine_mock.call_count == 1
assert create_engine_mock.mock_calls[0][1][0] == 'sqlite:///tmp/freqtrade2_test.sqlite' assert create_engine_mock.mock_calls[0][1][0] == 'sqlite:///tmp/freqtrade2_test.sqlite'
@ -37,7 +37,7 @@ def test_init_invalid_db_url(default_conf):
# Update path to a value other than default, but still in-memory # Update path to a value other than default, but still in-memory
default_conf.update({'db_url': 'unknown:///some.url'}) default_conf.update({'db_url': 'unknown:///some.url'})
with pytest.raises(OperationalException, match=r'.*no valid database URL*'): with pytest.raises(OperationalException, match=r'.*no valid database URL*'):
init(default_conf) init(default_conf['db_url'], default_conf['dry_run'])
def test_init_prod_db(default_conf, mocker): def test_init_prod_db(default_conf, mocker):
@ -46,7 +46,7 @@ def test_init_prod_db(default_conf, mocker):
create_engine_mock = mocker.patch('freqtrade.persistence.create_engine', MagicMock()) create_engine_mock = mocker.patch('freqtrade.persistence.create_engine', MagicMock())
init(default_conf) init(default_conf['db_url'], default_conf['dry_run'])
assert create_engine_mock.call_count == 1 assert create_engine_mock.call_count == 1
assert create_engine_mock.mock_calls[0][1][0] == 'sqlite:///tradesv3.sqlite' assert create_engine_mock.mock_calls[0][1][0] == 'sqlite:///tradesv3.sqlite'
@ -57,7 +57,7 @@ def test_init_dryrun_db(default_conf, mocker):
create_engine_mock = mocker.patch('freqtrade.persistence.create_engine', MagicMock()) create_engine_mock = mocker.patch('freqtrade.persistence.create_engine', MagicMock())
init(default_conf) init(default_conf['db_url'], default_conf['dry_run'])
assert create_engine_mock.call_count == 1 assert create_engine_mock.call_count == 1
assert create_engine_mock.mock_calls[0][1][0] == 'sqlite://' assert create_engine_mock.mock_calls[0][1][0] == 'sqlite://'
@ -336,8 +336,8 @@ def test_calc_profit_percent(limit_buy_order, limit_sell_order, fee):
assert trade.calc_profit_percent(fee=0.003) == 0.06147824 assert trade.calc_profit_percent(fee=0.003) == 0.06147824
@pytest.mark.usefixtures("init_persistence")
def test_clean_dry_run_db(default_conf, fee): def test_clean_dry_run_db(default_conf, fee):
init(default_conf)
# Simulate dry_run entries # Simulate dry_run entries
trade = Trade( trade = Trade(
@ -424,7 +424,7 @@ def test_migrate_old(mocker, default_conf, fee):
engine.execute(create_table_old) engine.execute(create_table_old)
engine.execute(insert_table_old) engine.execute(insert_table_old)
# Run init to test migration # Run init to test migration
init(default_conf) init(default_conf['db_url'], default_conf['dry_run'])
assert len(Trade.query.filter(Trade.id == 1).all()) == 1 assert len(Trade.query.filter(Trade.id == 1).all()) == 1
trade = Trade.query.filter(Trade.id == 1).first() trade = Trade.query.filter(Trade.id == 1).first()
@ -497,7 +497,7 @@ def test_migrate_new(mocker, default_conf, fee, caplog):
engine.execute("create table trades_bak1 as select * from trades") engine.execute("create table trades_bak1 as select * from trades")
# Run init to test migration # Run init to test migration
init(default_conf) init(default_conf['db_url'], default_conf['dry_run'])
assert len(Trade.query.filter(Trade.id == 1).all()) == 1 assert len(Trade.query.filter(Trade.id == 1).all()) == 1
trade = Trade.query.filter(Trade.id == 1).first() trade = Trade.query.filter(Trade.id == 1).first()
@ -566,7 +566,7 @@ def test_migrate_mid_state(mocker, default_conf, fee, caplog):
engine.execute(insert_table_old) engine.execute(insert_table_old)
# Run init to test migration # Run init to test migration
init(default_conf) init(default_conf['db_url'], default_conf['dry_run'])
assert len(Trade.query.filter(Trade.id == 1).all()) == 1 assert len(Trade.query.filter(Trade.id == 1).all()) == 1
trade = Trade.query.filter(Trade.id == 1).first() trade = Trade.query.filter(Trade.id == 1).first()
@ -668,8 +668,8 @@ def test_adjust_min_max_rates(fee):
assert trade.min_rate == 0.96 assert trade.min_rate == 0.96
@pytest.mark.usefixtures("init_persistence")
def test_get_open(default_conf, fee): def test_get_open(default_conf, fee):
init(default_conf)
# Simulate dry_run entries # Simulate dry_run entries
trade = Trade( trade = Trade(
@ -713,8 +713,8 @@ def test_get_open(default_conf, fee):
assert len(Trade.get_open_trades()) == 2 assert len(Trade.get_open_trades()) == 2
@pytest.mark.usefixtures("init_persistence")
def test_to_json(default_conf, fee): def test_to_json(default_conf, fee):
init(default_conf)
# Simulate dry_run entries # Simulate dry_run entries
trade = Trade( trade = Trade(

View File

@ -55,7 +55,7 @@ timeZone = pytz.UTC
def load_trades(args: Namespace, pair: str, timerange: TimeRange) -> pd.DataFrame: def load_trades(args: Namespace, pair: str, timerange: TimeRange) -> pd.DataFrame:
trades: pd.DataFrame = pd.DataFrame() trades: pd.DataFrame = pd.DataFrame()
if args.db_url: if args.db_url:
persistence.init(_CONF) persistence.init(args.db_url, True)
columns = ["pair", "profit", "open_time", "close_time", columns = ["pair", "profit", "open_time", "close_time",
"open_rate", "close_rate", "duration"] "open_rate", "close_rate", "duration"]