Example #1
0
def test_get_cli_root_directory_returns_path_to_directory_containing_config_file(
) -> None:
    create_fake_lean_cli_directory()

    manager = LeanConfigManager(mock.Mock(), ProjectConfigManager())

    assert manager.get_cli_root_directory() == Path.cwd()
Example #2
0
def test_get_data_directory_returns_path_to_data_directory_as_configured_in_config(
) -> None:
    with (Path.cwd() / "lean.json").open("w+", encoding="utf-8") as file:
        file.write('{ "data-folder": "sub1/sub2/sub3/data" }')

    manager = LeanConfigManager(mock.Mock(), ProjectConfigManager())

    assert manager.get_data_directory(
    ) == Path.cwd() / "sub1" / "sub2" / "sub3" / "data"
Example #3
0
def test_get_complete_lean_config_sets_iqfeed_host() -> None:
    create_fake_lean_cli_directory()

    manager = LeanConfigManager(mock.Mock(), ProjectConfigManager())
    config = manager.get_complete_lean_config(
        "my-environment",
        Path.cwd() / "Python Project" / "main.py", None)

    assert config["iqfeed-host"] == "host.docker.internal"
Example #4
0
def test_get_complete_lean_config_sets_close_automatically() -> None:
    create_fake_lean_cli_directory()

    manager = LeanConfigManager(mock.Mock(), ProjectConfigManager())
    config = manager.get_complete_lean_config(
        "my-environment",
        Path.cwd() / "Python Project" / "main.py", None)

    assert config["close-automatically"]
Example #5
0
def test_get_complete_lean_config_disables_debugging_when_no_method_given(
) -> None:
    create_fake_lean_cli_directory()

    manager = LeanConfigManager(mock.Mock(), ProjectConfigManager())
    config = manager.get_complete_lean_config(
        "my-environment",
        Path.cwd() / "Python Project" / "main.py", None)

    assert not config["debugging"]
Example #6
0
def test_get_complete_lean_config_sets_python_algorithm_details() -> None:
    create_fake_lean_cli_directory()

    manager = LeanConfigManager(mock.Mock(), ProjectConfigManager())
    config = manager.get_complete_lean_config(
        "my-environment",
        Path.cwd() / "Python Project" / "main.py", None)

    assert config["algorithm-type-name"] == "main"
    assert config["algorithm-language"] == "Python"
    assert config["algorithm-location"] == "/LeanCLI/main.py"
Example #7
0
def test_get_complete_lean_config_parses_debugging_method_correctly(
        method: DebuggingMethod, value: str) -> None:
    create_fake_lean_cli_directory()

    manager = LeanConfigManager(mock.Mock(), ProjectConfigManager())
    config = manager.get_complete_lean_config(
        "my-environment",
        Path.cwd() / "Python Project" / "main.py", method)

    assert config["debugging"]
    assert config["debugging-method"] == value
Example #8
0
def test_get_lean_config_path_returns_closest_config_file() -> None:
    lean_config_path = Path.cwd() / "lean.json"
    cwd_path = Path.cwd() / "sub1" / "sub2" / "sub3"

    lean_config_path.touch()
    cwd_path.mkdir(parents=True)
    os.chdir(cwd_path)

    manager = LeanConfigManager(mock.Mock(), ProjectConfigManager())

    assert manager.get_lean_config_path() == lean_config_path
Example #9
0
def test_get_complete_lean_config_sets_interactive_brokers_config() -> None:
    create_fake_lean_cli_directory()

    manager = LeanConfigManager(mock.Mock(), ProjectConfigManager())
    config = manager.get_complete_lean_config(
        "my-environment",
        Path.cwd() / "Python Project" / "main.py", None)

    assert config["ib-host"] == "127.0.0.1"
    assert config["ib-port"] == "4002"
    assert config["ib-tws-dir"] == "/root/Jts"
    assert config["ib-version"] == "978"
Example #10
0
def test_get_data_directory_returns_path_to_data_directory_when_config_contains_comments(
) -> None:
    with (Path.cwd() / "lean.json").open("w+", encoding="utf-8") as file:
        file.write("""
{
    // some comment about the data-folder
    "data-folder": "sub1/sub2/sub3/data"
}
        """)

    manager = LeanConfigManager(mock.Mock(), ProjectConfigManager())

    assert manager.get_data_directory(
    ) == Path.cwd() / "sub1" / "sub2" / "sub3" / "data"
Example #11
0
def test_get_complete_lean_config_sets_credentials_from_cli_config_manager(
) -> None:
    create_fake_lean_cli_directory()

    cli_config_manager = mock.Mock()
    cli_config_manager.user_id.get_value.return_value = "123"
    cli_config_manager.api_token.get_value.return_value = "456"

    manager = LeanConfigManager(cli_config_manager, ProjectConfigManager())
    config = manager.get_complete_lean_config(
        "my-environment",
        Path.cwd() / "Python Project" / "main.py", None)

    assert config["job-user-id"] == "123"
    assert config["api-access-token"] == "456"
Example #12
0
def test_get_complete_lean_config_returns_dict_with_all_keys_removed_in_clean_lean_config(
) -> None:
    create_fake_lean_cli_directory()

    manager = LeanConfigManager(mock.Mock(), ProjectConfigManager())
    config = manager.get_complete_lean_config(
        "backtesting",
        Path.cwd() / "Python Project" / "main.py", None)

    for key in [
            "environment", "algorithm-type-name", "algorithm-language",
            "algorithm-location", "composer-dll-directory", "debugging",
            "debugging-method", "parameters"
    ]:
        assert key in config
Example #13
0
def test_get_complete_lean_config_sets_csharp_algorithm_details(
        csharp_code: str, class_name: str) -> None:
    create_fake_lean_cli_directory()

    csharp_path = Path.cwd() / "CSharp Project" / "Main.cs"
    with csharp_path.open("w+", encoding="utf-8") as file:
        file.write(csharp_code.strip() + "\n")

    manager = LeanConfigManager(mock.Mock(), ProjectConfigManager())
    config = manager.get_complete_lean_config("my-environment", csharp_path,
                                              None)

    assert config["algorithm-type-name"] == class_name
    assert config["algorithm-language"] == "CSharp"
    assert config["algorithm-location"] == "CSharp Project.dll"
Example #14
0
def create_lean_runner(docker_manager: mock.Mock) -> LeanRunner:
    logger = mock.Mock()
    logger.debug_logging_enabled = False

    cli_config_manager = mock.Mock()
    cli_config_manager.user_id.get_value.return_value = "123"
    cli_config_manager.api_token.get_value.return_value = "456"

    project_config_manager = ProjectConfigManager(XMLManager())

    cache_storage = Storage(str(Path("~/.lean/cache").expanduser()))
    lean_config_manager = LeanConfigManager(logger, cli_config_manager,
                                            project_config_manager,
                                            mock.Mock(), cache_storage)
    output_config_manager = OutputConfigManager(lean_config_manager)

    module_manager = mock.Mock()
    module_manager.get_installed_packages.return_value = [
        NuGetPackage(name="QuantConnect.Brokerages", version="1.0.0")
    ]

    xml_manager = XMLManager()
    project_manager = ProjectManager(project_config_manager,
                                     lean_config_manager, xml_manager,
                                     PlatformManager())

    return LeanRunner(logger, project_config_manager, lean_config_manager,
                      output_config_manager, docker_manager, module_manager,
                      project_manager, TempManager(), xml_manager)
Example #15
0
def create_lean_runner(docker_manager: mock.Mock, csharp_compiler: mock.Mock = create_csharp_compiler()) -> LeanRunner:
    cli_config_manager = mock.Mock()
    cli_config_manager.user_id.get_value.return_value = "123"
    cli_config_manager.api_token.get_value.return_value = "456"

    return LeanRunner(mock.Mock(),
                      csharp_compiler,
                      LeanConfigManager(cli_config_manager, ProjectConfigManager()),
                      docker_manager,
                      TempManager())
def _create_project_manager() -> ProjectManager:
    xml_manager = XMLManager()
    project_config_manager = ProjectConfigManager(xml_manager)
    cache_storage = Storage(str(Path("~/.lean/cache").expanduser()))

    return ProjectManager(
        project_config_manager,
        LeanConfigManager(mock.Mock(), mock.Mock(), project_config_manager,
                          mock.Mock(), cache_storage), xml_manager,
        PlatformManager())
Example #17
0
def test_get_complete_lean_config_sets_parameters() -> None:
    create_fake_lean_cli_directory()

    Storage(str(Path.cwd() / "Python Project" / "config.json")).set(
        "parameters", {
            "key1": "value1",
            "key2": "value2",
            "key3": "value3"
        })

    manager = LeanConfigManager(mock.Mock(), ProjectConfigManager())
    config = manager.get_complete_lean_config(
        "my-environment",
        Path.cwd() / "Python Project" / "main.py", None)

    assert config["parameters"] == {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3"
    }
Example #18
0
def test_clean_lean_config_removes_auto_configurable_keys_from_original_config(
) -> None:
    original_config = """
{
    // this configuration file works by first loading all top-level
    // configuration items and then will load the specified environment
    // on top, this provides a layering affect.environment names can be
    // anything, and just require definition in this file.There's
    // two predefined environments, 'backtesting' and 'live', feel free
    // to add more!

    "environment": "backtesting", // "live-paper", "backtesting", "live-interactive", "live-interactive-iqfeed"

    // algorithm class selector
    "algorithm-type-name": "BasicTemplateFrameworkAlgorithm",

    // Algorithm language selector - options CSharp, Python
    "algorithm-language": "CSharp",

    //Physical DLL location
    "algorithm-location": "QuantConnect.Algorithm.CSharp.dll",
    //"algorithm-location": "../../../Algorithm.Python/BasicTemplateFrameworkAlgorithm.py",

    //Research notebook
    //"composer-dll-directory": ".",

    // engine
    "data-folder": "../../../Data/",

    // debugging configuration - options for debugging-method LocalCmdLine, VisualStudio, PTVSD, PyCharm
    "debugging": false,
    "debugging-method": "LocalCmdline",

    // parameters to set in the algorithm (the below are just samples)
    "parameters": {
        // Intrinio account user and password
        "intrinio-username": "",
        "intrinio-password": "",

        "ema-fast": 10,
        "ema-slow": 20
    },

    // handlers
    "log-handler": "QuantConnect.Logging.CompositeLogHandler",
    "messaging-handler": "QuantConnect.Messaging.Messaging",
    "job-queue-handler": "QuantConnect.Queues.JobQueue",
    
    // interactive brokers configuration
    "ib-account": "",
    "ib-user-name": "",
    "ib-password": "",
    "ib-host": "127.0.0.1",
    "ib-port": "4002",
    "ib-agent-description": "Individual",
    "ib-tws-dir": "C:\\Jts",
    "ib-trading-mode": "paper",
    "ib-enable-delayed-streaming-data": false,
    "ib-version": "974",

    // iqfeed configuration
    "iqfeed-host": "127.0.0.1",
    "iqfeed-username": "",
    "iqfeed-password": "",
    "iqfeed-productName": "",
    "iqfeed-version": "1.0"
}
    """

    manager = LeanConfigManager(mock.Mock(), ProjectConfigManager())
    clean_config = manager.clean_lean_config(original_config)

    for key in [
            "environment", "algorithm-type-name", "algorithm-language",
            "algorithm-location", "composer-dll-directory", "debugging",
            "debugging-method", "parameters", "intrinio-username",
            "intrinio-password", "ema-fast", "ema-slow", "ib-host", "ib-port",
            "ib-tws-dir", "ib-version", "iqfeed-host"
    ]:
        assert f'"{key}"' not in clean_config

    for key in [
            "data-folder", "log-handler", "messaging-handler",
            "job-queue-handler", "ib-account", "ib-user-name", "ib-password",
            "ib-agent-description", "ib-trading-mode",
            "ib-enable-delayed-streaming-data", "iqfeed-iqconnect",
            "iqfeed-username", "iqfeed-password", "iqfeed-productName",
            "iqfeed-version"
    ]:
        assert f'"{key}"' in clean_config
def _create_output_config_manager() -> OutputConfigManager:
    cache_storage = Storage(str(Path("~/.lean/cache").expanduser()))
    return OutputConfigManager(
        LeanConfigManager(mock.Mock(), mock.Mock(),
                          ProjectConfigManager(XMLManager()), mock.Mock(),
                          cache_storage))
Example #20
0
def _create_lean_config_manager(
    cli_config_manager: Optional[CLIConfigManager] = None
) -> LeanConfigManager:
    return LeanConfigManager(mock.Mock(), cli_config_manager or mock.Mock(),
                             ProjectConfigManager(XMLManager()), mock.Mock(),
                             Storage(str(Path("~/.lean/cache").expanduser())))
Example #21
0
def test_get_lean_config_path_raises_error_when_no_config_file_exists(
) -> None:
    manager = LeanConfigManager(mock.Mock(), ProjectConfigManager())

    with pytest.raises(Exception):
        manager.get_lean_config_path()
Example #22
0
def test_get_lean_config_path_returns_default_path_when_set() -> None:
    manager = LeanConfigManager(mock.Mock(), ProjectConfigManager())
    manager.set_default_lean_config_path(Path.cwd() / "custom-lean.json")

    assert manager.get_lean_config_path() == Path.cwd() / "custom-lean.json"