示例#1
0
def test_cloud_optimize_runs_optimization_by_project_id() -> None:
    create_fake_lean_cli_directory()

    project = create_api_project(1, "My Project")
    optimization = create_api_optimization()

    api_client = mock.Mock()
    api_client.projects.get_all.return_value = [project]
    api_client.optimizations.estimate.return_value = QCOptimizationEstimate(
        estimateId="x", time=10, balance=1000)
    api_client.organizations.get.return_value = create_api_organization()
    container.api_client.override(providers.Object(api_client))

    cloud_runner = mock.Mock()
    cloud_runner.run_optimization.return_value = optimization
    container.cloud_runner.override(providers.Object(cloud_runner))

    result = CliRunner().invoke(lean, ["cloud", "optimize", "1"])

    assert result.exit_code == 0

    cloud_runner.run_optimization.assert_called_once()
    args, kwargs = cloud_runner.run_optimization.call_args

    assert args[0] == project
示例#2
0
def test_cloud_optimize_passes_given_config_to_cloud_runner() -> None:
    create_fake_lean_cli_directory()

    project = create_api_project(1, "My Project")
    optimization = create_api_optimization()

    api_client = mock.Mock()
    api_client.projects.get_all.return_value = [project]
    api_client.optimizations.estimate.return_value = QCOptimizationEstimate(
        estimateId="x", time=10, balance=1000)
    api_client.organizations.get.return_value = create_api_organization()
    container.api_client.override(providers.Object(api_client))

    cloud_runner = mock.Mock()
    cloud_runner.run_optimization.return_value = optimization
    container.cloud_runner.override(providers.Object(cloud_runner))

    result = CliRunner().invoke(
        lean, ["cloud", "optimize", "My Project", "--name", "My Name"])

    assert result.exit_code == 0

    optimizer_config_manager = container.optimizer_config_manager()
    cloud_runner.run_optimization.assert_called_once_with(
        project, mock.ANY, "My Name",
        optimizer_config_manager.configure_strategy(cloud=True),
        optimizer_config_manager.configure_target(),
        optimizer_config_manager.configure_parameters([]),
        optimizer_config_manager.configure_constraints(),
        optimizer_config_manager.configure_node()[0].name,
        optimizer_config_manager.configure_node()[1])
示例#3
0
def test_cloud_optimize_does_not_display_backtest_results_when_none_meet_constraints(
) -> None:
    create_fake_lean_cli_directory()

    project = create_api_project(1, "My Project")
    optimization = create_api_optimization()
    optimization.backtests["1"] = create_api_optimization_backtest(
        1, True, False, True)
    optimization.backtests["2"] = create_api_optimization_backtest(
        2, True, False, False)

    api_client = mock.Mock()
    api_client.projects.get_all.return_value = [project]
    api_client.optimizations.estimate.return_value = QCOptimizationEstimate(
        estimateId="x", time=10, balance=1000)
    api_client.organizations.get.return_value = create_api_organization()
    container.api_client.override(providers.Object(api_client))

    cloud_runner = mock.Mock()
    cloud_runner.run_optimization.return_value = optimization
    container.cloud_runner.override(providers.Object(cloud_runner))

    result = CliRunner().invoke(lean, ["cloud", "optimize", "My Project"])

    assert result.exit_code == 0

    assert "id: 1" not in result.output
    assert "id: 2" not in result.output
示例#4
0
def test_cloud_optimize_pushes_nothing_when_project_does_not_exist_locally(
) -> None:
    create_fake_lean_cli_directory()

    project = create_api_project(1, "My Project")
    optimization = create_api_optimization()

    api_client = mock.Mock()
    api_client.projects.get_all.return_value = [project]
    api_client.optimizations.estimate.return_value = QCOptimizationEstimate(
        estimateId="x", time=10, balance=1000)
    api_client.organizations.get.return_value = create_api_organization()
    container.api_client.override(providers.Object(api_client))

    cloud_runner = mock.Mock()
    cloud_runner.run_optimization.return_value = optimization
    container.cloud_runner.override(providers.Object(cloud_runner))

    push_manager = mock.Mock()
    container.push_manager.override(providers.Object(push_manager))

    result = CliRunner().invoke(lean,
                                ["cloud", "optimize", "My Project", "--push"])

    assert result.exit_code == 0

    push_manager.push_projects.assert_not_called()
    def estimate(self, project_id: int, compile_id: str, name: str,
                 strategy: str, target: OptimizationTarget,
                 parameters: List[OptimizationParameter],
                 constraints: List[OptimizationConstraint], node_type: str,
                 parallel_nodes: int) -> QCOptimizationEstimate:
        """Estimates how long a backtest will take.

        :param project_id: the id of the project to optimize
        :param compile_id: the id of the compile to optimize
        :param name: the name of the optimization
        :param strategy: the strategy to optimize with
        :param target: the target of the optimization
        :param parameters: the parameters to optimize
        :param constraints: the constraints of the optimization
        :param node_type: the type of the node to run the optimization on
        :param parallel_nodes: the number of parallel nodes to run the optimization on
        :return: the resulting estimation
        """
        request_parameters = self._build_request_parameters(
            project_id, compile_id, name, strategy, target, parameters,
            constraints, node_type, parallel_nodes)

        data = self._api.post("optimizations/estimate",
                              request_parameters,
                              data_as_json=False)
        return QCOptimizationEstimate(**data["estimate"])
示例#6
0
def test_cloud_optimize_displays_optimal_backtest_results(
        optimizer_config_manager_mock: mock.Mock, target: str,
        extremum: OptimizationExtremum) -> None:
    create_fake_lean_cli_directory()

    project = create_api_project(1, "My Project")
    optimization = create_api_optimization()
    optimization.backtests["1"] = create_api_optimization_backtest(
        1, True, True, True)
    optimization.backtests["2"] = create_api_optimization_backtest(
        2, True, True, False)

    api_client = mock.Mock()
    api_client.projects.get_all.return_value = [project]
    api_client.optimizations.estimate.return_value = QCOptimizationEstimate(
        estimateId="x", time=10, balance=1000)
    container.api_client.override(providers.Object(api_client))

    cloud_runner = mock.Mock()
    cloud_runner.run_optimization.return_value = optimization
    container.cloud_runner.override(providers.Object(cloud_runner))

    optimizer_config_manager_mock.configure_target.return_value = OptimizationTarget(
        target=f"TotalPerformance.PortfolioStatistics.{target}",
        extremum=extremum,
    )

    result = CliRunner().invoke(lean, ["cloud", "optimize", "My Project"])

    assert result.exit_code == 0

    if extremum == OptimizationExtremum.Maximum:
        assert "id: 1" in result.output
    else:
        assert "id: 2" in result.output
示例#7
0
def test_cloud_optimize_aborts_when_input_matches_no_cloud_project() -> None:
    create_fake_lean_cli_directory()

    project = create_api_project(1, "My Project")
    optimization = create_api_optimization()

    api_client = mock.Mock()
    api_client.projects.get_all.return_value = [project]
    api_client.optimizations.estimate.return_value = QCOptimizationEstimate(
        estimateId="x", time=10, balance=1000)
    container.api_client.override(providers.Object(api_client))

    cloud_runner = mock.Mock()
    cloud_runner.run_optimization.return_value = optimization
    container.cloud_runner.override(providers.Object(cloud_runner))

    result = CliRunner().invoke(lean, ["cloud", "optimize", "Fake Project"])

    assert result.exit_code != 0

    cloud_runner.run_optimization.assert_not_called()
示例#8
0
def test_cloud_optimize_aborts_when_optimization_fails() -> None:
    create_fake_lean_cli_directory()

    project = create_api_project(1, "My Project")

    def run_optimization(*args, **kwargs):
        raise RuntimeError("Oops")

    api_client = mock.Mock()
    api_client.projects.get_all.return_value = [project]
    api_client.optimizations.estimate.return_value = QCOptimizationEstimate(
        estimateId="x", time=10, balance=1000)
    container.api_client.override(providers.Object(api_client))

    cloud_runner = mock.Mock()
    cloud_runner.run_optimization.side_effect = run_optimization
    container.cloud_runner.override(providers.Object(cloud_runner))

    result = CliRunner().invoke(lean, ["cloud", "optimize", "My Project"])

    assert result.exit_code != 0

    cloud_runner.run_optimization.assert_called_once()