Skip to content

Commit

Permalink
Merge branch 'tidy/replace_selected_components' of https://github.com…
Browse files Browse the repository at this point in the history
…/mckinsey/vizro into tidy/replace_selected_components
  • Loading branch information
huong-li-nguyen committed Jan 4, 2024
2 parents f52b3d5 + a5c84de commit c0f5e74
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @Joseph-Perkins @antonymilne @huong-li-nguyen @maxschulz-COL @lingyielia
* @Joseph-Perkins @antonymilne @huong-li-nguyen @maxschulz-COL
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!--
A new scriv changelog fragment.
Uncomment the section that is right (remove the HTML comment wrapper).
-->

<!--
### Highlights ✨
- A bullet item for the Highlights ✨ category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Removed
- A bullet item for the Removed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Added
- A bullet item for the Added category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Changed
- A bullet item for the Changed category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
<!--
### Deprecated
- A bullet item for the Deprecated category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->

### Fixed

- Fix a bug that prevented the update of nested graph properties through parameters when the graph property was not previously defined. ([#273](https://github.com/mckinsey/vizro/pull/237))

<!--
### Security
- A bullet item for the Security category with a link to the relevant PR at the end of your entry, e.g. Enable feature XXX ([#1](https://github.com/mckinsey/vizro/pull/1))
-->
1 change: 1 addition & 0 deletions vizro-core/docs/pages/development/authors.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

## Previous team members and code contributors

[Ann Marie Ward](https://github.com/AnnMarieW),
[Ned Letcher](https://github.com/ned2),
Natalia Kurakina,
[Leon Nallamuthu](https://github.com/leonnallamuthu),
Expand Down
8 changes: 6 additions & 2 deletions vizro-core/src/vizro/actions/_actions_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,15 @@ def _create_target_arg_mapping(dot_separated_strings: List[str]) -> Dict[str, Li
return results


def _update_nested_graph_properties(graph_config: Dict[str, Any], dot_separated_string: str, value: Any):
def _update_nested_graph_properties(
graph_config: Dict[str, Any], dot_separated_string: str, value: Any
) -> Dict[str, Any]:
keys = dot_separated_string.split(".")
current_property = graph_config

for key in keys[:-1]:
current_property = current_property[key]
current_property = current_property.setdefault(key, {})

current_property[keys[-1]] = value
return graph_config

Expand Down
59 changes: 41 additions & 18 deletions vizro-core/tests/unit/vizro/actions/test_actions_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,61 @@
from vizro.actions._actions_utils import _create_target_arg_mapping, _update_nested_graph_properties


@pytest.fixture
def fake_graph_property_dict():
return {"nodes": {"A": {"color": "blue"}, "B": {"color": "green"}}}


class TestUpdateNestedGraphProperties:
def test_update_nested_graph_properties_single_level(self):
graph = {"color": "blue"}
result = _update_nested_graph_properties(graph, "color", "red")
expected = {"color": "red"}
assert result == expected

def test_update_nested_graph_properties_multiple_levels(self):
graph = {"node": {"label": "A", "color": "blue"}}
result = _update_nested_graph_properties(graph, "node.color", 2)
expected = {"node": {"label": "A", "color": 2}}
@pytest.mark.parametrize(
"graph, dot_separated_strings, value, expected",
[
({"node": {"label": "A", "color": "blue"}}, "node.color", "red", {"node": {"label": "A", "color": "red"}}),
(
{"nodes": {"A": {"color": "blue"}, "B": {"color": "green"}}},
"nodes.A.color",
"red",
{"nodes": {"A": {"color": "red"}, "B": {"color": "green"}}},
),
],
)
def test_update_nested_graph_properties_multiple_levels(self, graph, dot_separated_strings, value, expected):
result = _update_nested_graph_properties(graph, dot_separated_strings, value)
assert result == expected

def test_update_nested_graph_properties_nested_dict(self):
graph = {"nodes": {"A": {"color": "blue"}, "B": {"color": "green"}}}
result = _update_nested_graph_properties(graph, "nodes.A.color", "red")
expected = {"nodes": {"A": {"color": "red"}, "B": {"color": "green"}}}
@pytest.mark.parametrize(
"graph, dot_separated_strings, value, expected",
[
(
{"nodes": {"A": {"color": "blue"}, "B": {"color": "green"}}},
"nodes.C.color",
"red",
{"nodes": {"A": {"color": "blue"}, "B": {"color": "green"}, "C": {"color": "red"}}},
),
(
{"nodes": {"A": {"color": "blue"}, "B": {"color": "red"}}},
"nodes.B.value",
"red",
{"nodes": {"A": {"color": "blue"}, "B": {"color": "red", "value": "red"}}},
),
(
{"nodes": {"A": {"color": "blue"}, "B": {"color": "green"}}},
"nodes.B",
"red",
{"nodes": {"A": {"color": "blue"}, "B": "red"}},
),
({}, "color", "red", {"color": "red"}),
],
)
def test_update_nested_graph_properties_add_or_overwrite_keys(self, graph, dot_separated_strings, value, expected):
result = _update_nested_graph_properties(graph, dot_separated_strings, value)
assert result == expected

def test_update_nested_graph_properties_invalid_key(self, fake_graph_property_dict):
with pytest.raises(KeyError, match="C"):
_update_nested_graph_properties(fake_graph_property_dict, "nodes.C.color", "red")

def test_update_nested_graph_properties_invalid_type(self):
graph = {"color": "blue"}
with pytest.raises(TypeError, match="'str' object does not support item assignment"):
_update_nested_graph_properties(graph, "color.value", 42)
_update_nested_graph_properties(graph, "color.value", "42")


class TestCreateTargetArgMapping:
Expand Down

0 comments on commit c0f5e74

Please sign in to comment.