-
Notifications
You must be signed in to change notification settings - Fork 0
Fix recursive_update_dict to add new keys from project config #26
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
Open
nulinspiratie
wants to merge
5
commits into
main
Choose a base branch
from
fix/recursive-update-dict-new-keys
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a7c8ab3
Fix recursive_update_dict to add new keys from project config
nulinspiratie 298c824
Add unit tests for recursive_update_dict
nulinspiratie 2c7ad08
fix lint issues
nulinspiratie d4d5900
remove reference to bug
nulinspiratie 9977aeb
Added docstring to recursive_update_dict
nulinspiratie 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,131 @@ | ||
| from qualibrate_config.core.utils import recursive_update_dict | ||
|
|
||
|
|
||
| def test_recursive_update_dict_add_new_key(): | ||
| """Test that new keys from updates are added to the base dict.""" | ||
| base = {"section": {"existing": "value"}} | ||
| updates = {"section": {"new_key": "new_value"}} | ||
|
|
||
| result = recursive_update_dict(base, updates) | ||
|
|
||
| assert result["section"]["existing"] == "value" | ||
| assert result["section"]["new_key"] == "new_value" | ||
|
|
||
|
|
||
| def test_recursive_update_dict_update_existing_key(): | ||
| """Test that existing keys are properly updated.""" | ||
| base = {"section": {"key": "old_value"}} | ||
| updates = {"section": {"key": "new_value"}} | ||
|
|
||
| result = recursive_update_dict(base, updates) | ||
|
|
||
| assert result["section"]["key"] == "new_value" | ||
|
|
||
|
|
||
| def test_recursive_update_dict_add_new_top_level_section(): | ||
| """Test that completely new sections are added.""" | ||
| base = {"existing_section": {"key": "value"}} | ||
| updates = {"new_section": {"key": "value"}} | ||
|
|
||
| result = recursive_update_dict(base, updates) | ||
|
|
||
| assert "existing_section" in result | ||
| assert "new_section" in result | ||
| assert result["new_section"]["key"] == "value" | ||
|
|
||
|
|
||
| def test_recursive_update_dict_nested_merge_with_new_keys(): | ||
| """Test recursive merge preserves existing values while adding new ones.""" | ||
| base = {"quam": {"version": 3, "serialization": {"include_defaults": True}}} | ||
| updates = { | ||
| "quam": { | ||
| "state_path": "/new/path", | ||
| "serialization": {"other_option": False}, | ||
| } | ||
| } | ||
|
|
||
| result = recursive_update_dict(base, updates) | ||
|
|
||
| # Existing values preserved | ||
| assert result["quam"]["version"] == 3 | ||
| assert result["quam"]["serialization"]["include_defaults"] is True | ||
|
|
||
| # New values added | ||
| assert result["quam"]["state_path"] == "/new/path" | ||
| assert result["quam"]["serialization"]["other_option"] is False | ||
|
|
||
|
|
||
| def test_recursive_update_dict_project_config_use_case(): | ||
| """ | ||
|
nulinspiratie marked this conversation as resolved.
|
||
| Test adding new keys to project config when base config doesn't have them. | ||
|
|
||
| This test verifies that when merging configurations: | ||
| - Base config: [quam] has version=3, no state_path | ||
| - Project config: [quam] adds state_path="/project/path" | ||
| - Expected: state_path should be merged in while preserving existing values | ||
| """ | ||
| # Base config from ~/.qualibrate/config.toml | ||
| base = { | ||
| "quam": { | ||
| "raise_error_missing_reference": False, | ||
| "version": 3, | ||
| "serialization": {"include_defaults": True}, | ||
| }, | ||
| "qualibrate": {"version": 5, "project": "CS_1"}, | ||
| } | ||
|
|
||
| # Project config from ~/.qualibrate/projects/CS_1/config.toml | ||
| updates = { | ||
| "quam": {"state_path": "/path/to/project/quam_state"}, | ||
| "qualibrate": {"storage": {"location": "/project/storage"}}, | ||
| } | ||
|
|
||
| result = recursive_update_dict(base, updates) | ||
|
|
||
| # Verify all base values preserved | ||
| assert result["quam"]["version"] == 3 | ||
| assert result["quam"]["raise_error_missing_reference"] is False | ||
| assert result["quam"]["serialization"]["include_defaults"] is True | ||
| assert result["qualibrate"]["version"] == 5 | ||
| assert result["qualibrate"]["project"] == "CS_1" | ||
|
|
||
| # Verify new project-specific values added | ||
| assert result["quam"]["state_path"] == "/path/to/project/quam_state" | ||
| assert result["qualibrate"]["storage"]["location"] == "/project/storage" | ||
|
|
||
|
|
||
| def test_recursive_update_dict_empty_updates(): | ||
| """Test that empty updates dict doesn't break anything.""" | ||
| base = {"section": {"key": "value"}} | ||
| updates = {} | ||
|
|
||
| result = recursive_update_dict(base, updates) | ||
|
|
||
| assert result == base | ||
|
|
||
|
|
||
| def test_recursive_update_dict_non_dict_value_override(): | ||
| """Test that non-dict values properly override nested dicts.""" | ||
| base = {"section": {"key": {"nested": "value"}}} | ||
| updates = {"section": {"key": "simple_value"}} | ||
|
|
||
| result = recursive_update_dict(base, updates) | ||
|
|
||
| assert result["section"]["key"] == "simple_value" | ||
|
|
||
|
|
||
| def test_recursive_update_dict_multiple_new_keys_at_different_levels(): | ||
| """Test adding new keys at multiple nesting levels.""" | ||
| base = {"level1": {"level2": {"existing": "value"}}} | ||
| updates = { | ||
| "level1": { | ||
| "new_at_level2": "value2", | ||
| "level2": {"new_at_level3": "value3"}, | ||
| } | ||
| } | ||
|
|
||
| result = recursive_update_dict(base, updates) | ||
|
|
||
| assert result["level1"]["level2"]["existing"] == "value" | ||
| assert result["level1"]["new_at_level2"] == "value2" | ||
| assert result["level1"]["level2"]["new_at_level3"] == "value3" | ||
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.
can you explain what this function is doing?
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 docstring, does this clarify it?