Skip to content

Commit 6a830eb

Browse files
committed
Add unit tests
1 parent ca659d2 commit 6a830eb

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

lean/components/cloud/push_manager.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,15 @@ def _push_metadata(self, project: Path, cloud_project: QCProject, encryption_act
254254
encryption_key_id = get_project_key_hash(encryption_key)
255255
update_args["encryption_key"] = encryption_key_id
256256

257+
if not force:
258+
update_args["code_source_id"] = "cli"
259+
257260
if update_args != {}:
258261
self._api_client.projects.update(cloud_project.projectId, **update_args)
259262

260263
if "encryption_key" in update_args:
261264
del update_args["encryption_key"]
262-
if not force:
263-
update_args["code_source_id"] = "cli"
265+
264266
updated_keys = list(update_args)
265267
if len(updated_keys) == 1:
266268
updated_keys_str = updated_keys[0]

tests/commands/cloud/test_push.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from unittest import mock
1616
from datetime import datetime
1717

18+
import pytest
1819
from click.testing import CliRunner
1920

2021
from lean.commands import lean
@@ -470,7 +471,11 @@ def _get_expected_encrypted_files_content() -> dict:
470471
bFWAAkX56MyDHwJefC1nkA=="""
471472
}
472473

473-
def test_cloud_push_sets_code_source_id_to_cli() -> None:
474+
@pytest.mark.parametrize("force_flag, expected_code_source_id", [
475+
(False, "cli"),
476+
(True, None),
477+
])
478+
def test_cloud_push_code_source_id_behavior(force_flag: bool, expected_code_source_id: str | None) -> None:
474479
create_fake_lean_cli_directory()
475480
project_path = Path.cwd() / "Python Project"
476481

@@ -483,15 +488,22 @@ def test_cloud_push_sets_code_source_id_to_cli() -> None:
483488

484489
init_container(api_client_to_use=api_client)
485490

486-
result = CliRunner().invoke(lean, ["cloud", "push", "--project", project_path])
491+
command = ["cloud", "push", "--project", project_path]
492+
if force_flag:
493+
command.append("--force")
487494

495+
result = CliRunner().invoke(lean, command)
488496
assert result.exit_code == 0
497+
489498
expected_arguments = {
490499
"name": "Python Project",
491500
"description": "",
492-
"files": mock.ANY,
501+
"files": [{'name': 'main.py', 'content': '# region imports\nfrom AlgorithmImports import *\n# endregion\n\nclass PythonProject(QCAlgorithm):\n\n def initialize(self):\n # Locally Lean installs free sample data, to download more data please visit https://www.quantconnect.com/docs/v2/lean-cli/datasets/downloading-data\n self.set_start_date(2013, 10, 7) # Set Start Date\n self.set_end_date(2013, 10, 11) # Set End Date\n self.set_cash(100000) # Set Strategy Cash\n self.add_equity("SPY", Resolution.MINUTE)\n\n def on_data(self, data: Slice):\n """on_data event is the primary entry point for your algorithm. Each new data point will be pumped in here.\n Arguments:\n data: Slice object keyed by symbol containing the stock data\n """\n if not self.portfolio.invested:\n self.set_holdings("SPY", 1)\n self.debug("Purchased Stock")\n'}, {'name': 'research.ipynb', 'content': '{\n "cells": [\n {\n "cell_type": "markdown",\n "metadata": {},\n "source": [\n "![QuantConnect Logo](https://cdn.quantconnect.com/web/i/icon.png)\\n",\n "<hr>"\n ]\n },\n {\n "cell_type": "code",\n "execution_count": null,\n "metadata": {},\n "outputs": [],\n "source": [\n "# QuantBook Analysis Tool \\n",\n "# For more information see [https://www.quantconnect.com/docs/v2/our-platform/research/getting-started]\\n",\n "qb = QuantBook()\\n",\n "spy = qb.add_equity(\\"SPY\\")\\n",\n "# Locally Lean installs free sample data, to download more data please visit https://www.quantconnect.com/docs/v2/lean-cli/datasets/downloading-data \\n",\n "qb.set_start_date(2013, 10, 11)\\n",\n "history = qb.history(qb.securities.keys(), 360, Resolution.DAILY)\\n",\n "\\n",\n "# Indicator Analysis\\n",\n "bbdf = qb.indicator(BollingerBands(30, 2), spy.symbol, 360, Resolution.DAILY)\\n",\n "bbdf.drop(\'standarddeviation\', axis=1).plot()"\n ]\n }\n ],\n "metadata": {\n "kernelspec": {\n "display_name": "Python 3",\n "language": "python",\n "name": "python3"\n }\n },\n "nbformat": 4,\n "nbformat_minor": 2\n}\n'}],
493502
"libraries": [],
494503
"encryption_key": "",
495-
"codeSourceId": "cli"
496504
}
505+
506+
if expected_code_source_id is not None:
507+
expected_arguments["code_source_id"] = expected_code_source_id
508+
497509
api_client.projects.update.assert_called_once_with(1, **expected_arguments)

0 commit comments

Comments
 (0)