Пример #1
0
def test_main_reload_conf(mocker, default_conf, caplog) -> None:
    """
    Test main() function
    In this test we are skipping the while True loop by throwing an exception.
    """
    patch_exchange(mocker)
    mocker.patch.multiple(
        'freqtrade.freqtradebot.FreqtradeBot',
        _init_modules=MagicMock(),
        worker=MagicMock(return_value=State.RELOAD_CONF),
        cleanup=MagicMock(),
    )
    mocker.patch('freqtrade.configuration.Configuration._load_config_file',
                 lambda *args, **kwargs: default_conf)
    mocker.patch('freqtrade.freqtradebot.CryptoToFiatConverter', MagicMock())
    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())

    # Raise exception as side effect to avoid endless loop
    reconfigure_mock = mocker.patch('freqtrade.main.reconfigure',
                                    MagicMock(side_effect=Exception))

    with pytest.raises(SystemExit):
        main(['-c', 'config.json.example'])

    assert reconfigure_mock.call_count == 1
    assert log_has('Using config: config.json.example ...',
                   caplog.record_tuples)
Пример #2
0
def test_main_keyboard_interrupt(mocker, default_conf, caplog) -> None:
    """
    Test main() function
    In this test we are skipping the while True loop by throwing an exception.
    """
    patch_exchange(mocker)
    mocker.patch.multiple(
        'freqtrade.freqtradebot.FreqtradeBot',
        _init_modules=MagicMock(),
        worker=MagicMock(side_effect=KeyboardInterrupt),
        cleanup=MagicMock(),
    )
    mocker.patch('freqtrade.configuration.Configuration._load_config_file',
                 lambda *args, **kwargs: default_conf)
    mocker.patch('freqtrade.freqtradebot.CryptoToFiatConverter', MagicMock())
    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())

    args = ['-c', 'config.json.example']

    # Test Main + the KeyboardInterrupt exception
    with pytest.raises(SystemExit):
        main(args)
    assert log_has('Using config: config.json.example ...',
                   caplog.record_tuples)
    assert log_has('SIGINT received, aborting ...', caplog.record_tuples)
Пример #3
0
def test_main_reload_conf(mocker, default_conf, caplog) -> None:
    patch_exchange(mocker)
    mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
    # Simulate Running, reload, running workflow
    worker_mock = MagicMock(side_effect=[
        State.RUNNING, State.RELOAD_CONF, State.RUNNING,
        OperationalException("Oh snap!")
    ])
    mocker.patch('freqtrade.worker.Worker._worker', worker_mock)
    mocker.patch('freqtrade.configuration.Configuration._load_config_file',
                 lambda *args, **kwargs: default_conf)
    reconfigure_mock = mocker.patch('freqtrade.main.Worker._reconfigure',
                                    MagicMock())

    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
    mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())

    args = Arguments(['-c', 'config.json.example'], '').get_parsed_arg()
    worker = Worker(args=args, config=default_conf)
    with pytest.raises(SystemExit):
        main(['-c', 'config.json.example'])

    assert log_has('Using config: config.json.example ...',
                   caplog.record_tuples)
    assert worker_mock.call_count == 4
    assert reconfigure_mock.call_count == 1
    assert isinstance(worker.freqtrade, FreqtradeBot)
Пример #4
0
def test_main(mocker, caplog) -> None:
    """
    Test main() function
    In this test we are skipping the while True loop by throwing an exception.
    """
    mocker.patch.multiple(
        'freqtrade.freqtradebot.FreqtradeBot',
        _init_modules=MagicMock(),
        worker=MagicMock(side_effect=KeyboardInterrupt),
        clean=MagicMock(),
    )
    args = ['-c', 'config.json.example']

    # Test Main + the KeyboardInterrupt exception
    with pytest.raises(SystemExit) as pytest_wrapped_e:
        main(args)
        log_has('Starting freqtrade', caplog.record_tuples)
        log_has('Got SIGINT, aborting ...', caplog.record_tuples)
        assert pytest_wrapped_e.type == SystemExit
        assert pytest_wrapped_e.value.code == 42

    # Test the BaseException case
    mocker.patch('freqtrade.freqtradebot.FreqtradeBot.worker',
                 MagicMock(side_effect=BaseException))
    with pytest.raises(SystemExit):
        main(args)
        log_has('Got fatal exception!', caplog.record_tuples)
Пример #5
0
def test_main_reload_config(mocker, default_conf, caplog) -> None:
    patch_exchange(mocker)
    mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
    # Simulate Running, reload, running workflow
    worker_mock = MagicMock(side_effect=[
        State.RUNNING, State.RELOAD_CONFIG, State.RUNNING,
        OperationalException("Oh snap!")
    ])
    mocker.patch('freqtrade.worker.Worker._worker', worker_mock)
    patched_configuration_load_config_file(mocker, default_conf)
    mocker.patch('freqtrade.wallets.Wallets.update', MagicMock())
    reconfigure_mock = mocker.patch('freqtrade.worker.Worker._reconfigure',
                                    MagicMock())

    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
    mocker.patch('freqtrade.freqtradebot.init_db', MagicMock())

    args = Arguments(['trade', '-c',
                      'config_bittrex.json.example']).get_parsed_arg()
    worker = Worker(args=args, config=default_conf)
    with pytest.raises(SystemExit):
        main(['trade', '-c', 'config_bittrex.json.example'])

    assert log_has('Using config: config_bittrex.json.example ...', caplog)
    assert worker_mock.call_count == 4
    assert reconfigure_mock.call_count == 1
    assert isinstance(worker.freqtrade, FreqtradeBot)
Пример #6
0
def test_main_start_hyperopt(mocker) -> None:
    hyperopt_mock = mocker.patch('freqtrade.optimize.hyperopt.start', MagicMock())
    main(['hyperopt'])
    assert hyperopt_mock.call_count == 1
    call_args = hyperopt_mock.call_args[0][0]
    assert call_args.config == ['config.json']
    assert call_args.loglevel == 0
    assert call_args.subparser == 'hyperopt'
    assert call_args.func is not None
Пример #7
0
def test_main_start_hyperopt(mocker) -> None:
    hyperopt_mock = mocker.patch('freqtrade.optimize.start_hyperopt',
                                 MagicMock())
    # it's sys.exit(0) at the end of hyperopt
    with pytest.raises(SystemExit):
        main(['hyperopt'])
    assert hyperopt_mock.call_count == 1
    call_args = hyperopt_mock.call_args[0][0]
    assert call_args.config == ['config.json']
    assert call_args.loglevel == 0
    assert call_args.subparser == 'hyperopt'
    assert call_args.func is not None
Пример #8
0
def test_main_start_hyperopt(mocker) -> None:
    hyperopt_mock = mocker.patch('freqtrade.optimize.start_hyperopt', MagicMock())
    hyperopt_mock.__name__ = PropertyMock("start_hyperopt")
    # it's sys.exit(0) at the end of hyperopt
    with pytest.raises(SystemExit):
        main(['hyperopt'])
    assert hyperopt_mock.call_count == 1
    call_args = hyperopt_mock.call_args[0][0]
    assert call_args["config"] == ['config.json']
    assert call_args["verbosity"] == 0
    assert call_args["command"] == 'hyperopt'
    assert call_args["func"] is not None
    assert callable(call_args["func"])
Пример #9
0
def test_main_start_hyperopt(mocker) -> None:
    mocker.patch.object(Path, 'is_file', MagicMock(side_effect=[False, True]))
    hyperopt_mock = mocker.patch('freqtrade.commands.start_hyperopt', MagicMock())
    hyperopt_mock.__name__ = PropertyMock('start_hyperopt')
    # it's sys.exit(0) at the end of hyperopt
    with pytest.raises(SystemExit):
        main(['hyperopt'])
    assert hyperopt_mock.call_count == 1
    call_args = hyperopt_mock.call_args[0][0]
    assert call_args['config'] == ['config.json']
    assert call_args['verbosity'] == 0
    assert call_args['command'] == 'hyperopt'
    assert call_args['func'] is not None
    assert callable(call_args['func'])
Пример #10
0
def test_main_keyboard_interrupt(mocker, default_conf, caplog) -> None:
    patch_exchange(mocker)
    mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
    mocker.patch('freqtrade.worker.Worker._worker', MagicMock(side_effect=KeyboardInterrupt))
    patched_configuration_load_config_file(mocker, default_conf)
    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
    mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())

    args = ['trade', '-c', 'config.json.example']

    # Test Main + the KeyboardInterrupt exception
    with pytest.raises(SystemExit):
        main(args)
    assert log_has('Using config: config.json.example ...', caplog)
    assert log_has('SIGINT received, aborting ...', caplog)
Пример #11
0
def test_main_fatal_exception(mocker, default_conf, caplog) -> None:
    patch_exchange(mocker)
    mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
    mocker.patch('freqtrade.worker.Worker._worker', MagicMock(side_effect=Exception))
    patched_configuration_load_config_file(mocker, default_conf)
    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
    mocker.patch('freqtrade.freqtradebot.init_db', MagicMock())

    args = ['trade', '-c', 'config_examples/config_bittrex.example.json']

    # Test Main + the KeyboardInterrupt exception
    with pytest.raises(SystemExit):
        main(args)
    assert log_has('Using config: config_examples/config_bittrex.example.json ...', caplog)
    assert log_has('Fatal exception!', caplog)
Пример #12
0
def test_parse_args_backtesting(mocker) -> None:
    """
    Test that main() can start backtesting and also ensure we can pass some specific arguments
    further argument parsing is done in test_arguments.py
    """
    backtesting_mock = mocker.patch('freqtrade.optimize.backtesting.start', MagicMock())
    main(['backtesting'])
    assert backtesting_mock.call_count == 1
    call_args = backtesting_mock.call_args[0][0]
    assert call_args.config == ['config.json']
    assert call_args.live is False
    assert call_args.loglevel == 0
    assert call_args.subparser == 'backtesting'
    assert call_args.func is not None
    assert call_args.ticker_interval is None
Пример #13
0
def test_main_operational_exception(mocker, default_conf, caplog) -> None:
    patch_exchange(mocker)
    mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
    mocker.patch('freqtrade.worker.Worker._worker',
                 MagicMock(side_effect=FreqtradeException('Oh snap!')))
    patched_configuration_load_config_file(mocker, default_conf)
    mocker.patch('freqtrade.wallets.Wallets.update', MagicMock())
    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
    mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())

    args = ['trade', '-c', 'config.json.example']

    # Test Main + the KeyboardInterrupt exception
    with pytest.raises(SystemExit):
        main(args)
    assert log_has('Using config: config.json.example ...', caplog)
    assert log_has('Oh snap!', caplog)
Пример #14
0
def test_main_fatal_exception(mocker, default_conf, caplog) -> None:
    patch_exchange(mocker)
    mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
    mocker.patch('freqtrade.worker.Worker._worker',
                 MagicMock(side_effect=Exception))
    mocker.patch('freqtrade.configuration.Configuration._load_config_file',
                 lambda *args, **kwargs: default_conf)
    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
    mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())

    args = ['-c', 'config.json.example']

    # Test Main + the KeyboardInterrupt exception
    with pytest.raises(SystemExit):
        main(args)
    assert log_has('Using config: config.json.example ...',
                   caplog.record_tuples)
    assert log_has('Fatal exception!', caplog.record_tuples)
def test_parse_args_backtesting(mocker) -> None:
    """
    Test that main() can start backtesting and also ensure we can pass some specific arguments
    further argument parsing is done in test_arguments.py
    """
    backtesting_mock = mocker.patch('freqtrade.optimize.start_backtesting',
                                    MagicMock())
    backtesting_mock.__name__ = PropertyMock("start_backtesting")
    # it's sys.exit(0) at the end of backtesting
    with pytest.raises(SystemExit):
        main(['backtesting'])
    assert backtesting_mock.call_count == 1
    call_args = backtesting_mock.call_args[0][0]
    assert call_args.config == ['config.json']
    assert call_args.verbosity == 0
    assert call_args.subparser == 'backtesting'
    assert call_args.func is not None
    assert call_args.ticker_interval is None
Пример #16
0
def test_parse_args_backtesting(mocker) -> None:
    """
    Test that main() can start backtesting and also ensure we can pass some specific arguments
    further argument parsing is done in test_arguments.py
    """
    mocker.patch.object(Path, "is_file", MagicMock(side_effect=[False, True]))
    backtesting_mock = mocker.patch('freqtrade.commands.start_backtesting')
    backtesting_mock.__name__ = PropertyMock("start_backtesting")
    # it's sys.exit(0) at the end of backtesting
    with pytest.raises(SystemExit):
        main(['backtesting'])
    assert backtesting_mock.call_count == 1
    call_args = backtesting_mock.call_args[0][0]
    assert call_args['config'] == ['config.json']
    assert call_args['verbosity'] == 0
    assert call_args['command'] == 'backtesting'
    assert call_args['func'] is not None
    assert callable(call_args['func'])
    assert call_args['timeframe'] is None
Пример #17
0
def test_main_operational_exception(mocker, default_conf, caplog) -> None:
    patch_exchange(mocker)
    mocker.patch.multiple(
        'freqtrade.freqtradebot.FreqtradeBot',
        _init_modules=MagicMock(),
        worker=MagicMock(side_effect=OperationalException('Oh snap!')),
        cleanup=MagicMock(),
    )
    mocker.patch('freqtrade.configuration.Configuration._load_config_file',
                 lambda *args, **kwargs: default_conf)
    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())

    args = ['-c', 'config.json.example']

    # Test Main + the KeyboardInterrupt exception
    with pytest.raises(SystemExit):
        main(args)
    assert log_has('Using config: config.json.example ...',
                   caplog.record_tuples)
    assert log_has('Oh snap!', caplog.record_tuples)
Пример #18
0
def test_main_operational_exception1(mocker, default_conf, caplog) -> None:
    patch_exchange(mocker)
    mocker.patch('freqtrade.commands.list_commands.validate_exchanges',
                 MagicMock(side_effect=ValueError('Oh snap!')))
    patched_configuration_load_config_file(mocker, default_conf)

    args = ['list-exchanges']

    # Test Main + the KeyboardInterrupt exception
    with pytest.raises(SystemExit):
        main(args)

    assert log_has('Fatal exception!', caplog)
    assert not log_has_re(r'SIGINT.*', caplog)
    mocker.patch('freqtrade.commands.list_commands.validate_exchanges',
                 MagicMock(side_effect=KeyboardInterrupt))
    with pytest.raises(SystemExit):
        main(args)

    assert log_has_re(r'SIGINT.*', caplog)
Пример #19
0
def test_main_reload_conf(mocker, default_conf, caplog) -> None:
    patch_exchange(mocker)
    mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
    mocker.patch('freqtrade.worker.Worker._worker', MagicMock(return_value=State.RELOAD_CONF))
    mocker.patch(
        'freqtrade.configuration.Configuration._load_config_file',
        lambda *args, **kwargs: default_conf
    )
    mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
    mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())

    # Raise exception as side effect to avoid endless loop
    reconfigure_mock = mocker.patch(
        'freqtrade.main.Worker._reconfigure', MagicMock(side_effect=Exception)
    )

    with pytest.raises(SystemExit):
        main(['-c', 'config.json.example'])

    assert reconfigure_mock.call_count == 1
    assert log_has('Using config: config.json.example ...', caplog.record_tuples)
Пример #20
0
def test_parse_args_None(caplog) -> None:
    with pytest.raises(SystemExit):
        main([])
    assert log_has_re(r"Usage of Freqtrade requires a subcommand.*", caplog)
Пример #21
0
#!/usr/bin/env python3
"""
__main__.py for Freqtrade
To launch Freqtrade as a module

> python -m freqtrade (with Python >= 3.6)
"""

from freqtrade import main

if __name__ == '__main__':
    main.main()
Пример #22
0
#!/usr/bin/env python3
"""
__main__.py for Freqtrade
To launch Freqtrade as a module

> python -m freqtrade (with Python >= 3.6)
"""

import sys
from freqtrade import main

if __name__ == '__main__':
    main.set_loggers()
    main.main(sys.argv[1:])