Ejemplo n.º 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
Ejemplo n.º 2
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
Ejemplo n.º 3
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])
Ejemplo n.º 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()
Ejemplo n.º 5
0
def test_interactive_bulk_select():
    pytest.skip("This test is interactive")

    organization = create_api_organization()
    datasource = json.loads(bulk_datasource)
    testSets = [
        Dataset(name="testSet",
                vendor="testVendor",
                categories=["testData"],
                options=datasource["options"],
                paths=datasource["paths"],
                requires_security_master=datasource["requiresSecurityMaster"])
    ]

    products = _select_products_interactive(organization, testSets)
Ejemplo n.º 6
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)
    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", "Fake Project"])

    assert result.exit_code != 0

    cloud_runner.run_optimization.assert_not_called()
Ejemplo n.º 7
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)
    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.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()
Ejemplo n.º 8
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)
    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))

    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