Repository navigation
fix: keep the modules cache when the downloaded JSON is malformed or has no modules #708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Martin-Molinero
merged 7 commits into
QuantConnect:master
from
pcbeingused333:fix/modules-json-keep-cache
Nov 4, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cc5ef51
fix: keep the modules cache when the downloaded JSON is malformed
pcbeingused333 4932d67
fix: replace the modules cache through os functions
pcbeingused333 7b6aa27
Merge remote-tracking branch 'origin/master' into fix/modules-json-ke…
quantconnect-kronos[bot] 0468910
Use a per-process temporary modules file and add the test license header
quantconnect-kronos[bot] 2df8f3d
Keep the modules cache's owner and mode when replacing it
quantconnect-kronos[bot] 2d85f0e
Change the modules cache's owner only when it differs
quantconnect-kronos[bot] fa52e2e
Write the modules file in place, once the response is decoded and has…
Martin-Molinero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. | ||
| # Lean CLI v1.0. Copyright 2021 QuantConnect Corporation. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import json | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
| from responses import RequestsMock | ||
|
|
||
| import lean.models as models | ||
|
|
||
|
|
||
| def _write_cache(path: Path) -> str: | ||
| content = json.dumps({"modules": {"SavedModule": {"version": "1.0"}}}) | ||
| path.write_text(content, encoding="utf-8") | ||
| return content | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("body", ["not json", "", "{}", "[]", "null"]) | ||
| def test_download_modules_json_keeps_cache_when_response_has_no_modules(requests_mock: RequestsMock, | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| body: str) -> None: | ||
| cache = Path.cwd() / "modules.json" | ||
| content = _write_cache(cache) | ||
| monkeypatch.setattr(models, "file_path", cache) | ||
| requests_mock.add(requests_mock.GET, models.url, body=body, status=200) | ||
|
|
||
| with pytest.raises(ValueError): | ||
| models._download_modules_json() | ||
|
|
||
| assert cache.read_text(encoding="utf-8") == content | ||
|
|
||
|
|
||
| def test_download_modules_json_replaces_cache_with_valid_response(requests_mock: RequestsMock, | ||
| monkeypatch: pytest.MonkeyPatch) -> None: | ||
| cache = Path.cwd() / "modules.json" | ||
| _write_cache(cache) | ||
| monkeypatch.setattr(models, "file_path", cache) | ||
| new_modules = {"modules": {"NewModule": {"version": "2.0"}}} | ||
| requests_mock.add(requests_mock.GET, models.url, json=new_modules, status=200) | ||
|
|
||
| models._download_modules_json() | ||
|
|
||
| assert json.loads(cache.read_text(encoding="utf-8")) == new_modules | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: add the license header the other test files start with (see
tests/models/test_modules.py).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in 0468910.