Repository navigation
Fix Patch.update shared mapping ownership #4144
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -884,3 +884,54 @@ def get_output(): | |
|
|
||
| dash_duo.find_element("#clear-btn").click() | ||
| until(lambda: get_output()["array"] == [], 2) | ||
|
|
||
|
|
||
| def test_pch008_patch_update_shared_mapping(dash_duo): | ||
| app = Dash(__name__) | ||
| app.layout = html.Div( | ||
| [ | ||
| html.Button("Apply", id="apply"), | ||
| dcc.Store( | ||
| id="records", | ||
| data={"jan": {"retained": 1}, "feb": {"retained": 2}}, | ||
| ), | ||
| html.Pre(id="records-json"), | ||
| html.Pre(id="common-json"), | ||
| ] | ||
| ) | ||
| app.clientside_callback( | ||
| "data => JSON.stringify(data)", | ||
| Output("records-json", "children"), | ||
| Input("records", "data"), | ||
| ) | ||
|
|
||
| @app.callback( | ||
| Output("records", "data"), | ||
| Output("common-json", "children"), | ||
| Input("apply", "n_clicks"), | ||
| prevent_initial_call=True, | ||
| ) | ||
| def apply_patch(_): | ||
| common = {"currency": "USD"} | ||
| p = Patch() | ||
| p["jan"].update(common, amount=100) | ||
| p["feb"].update(common, amount=200) | ||
| return p, json.dumps(common) | ||
|
|
||
| dash_duo.start_server(app) | ||
| dash_duo.find_element("#apply").click() | ||
|
|
||
| def data_received(): | ||
| raw_data = dash_duo.find_element("#records-json").text | ||
| if not raw_data or not dash_duo.find_element("#common-json").text: | ||
| return False | ||
| data = json.loads(raw_data) | ||
| return all("amount" in data.get(month, {}) for month in ("jan", "feb")) | ||
|
|
||
| until(data_received, timeout=3) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| assert json.loads(dash_duo.find_element("#records-json").text) == { | ||
| "jan": {"retained": 1, "currency": "USD", "amount": 100}, | ||
| "feb": {"retained": 2, "currency": "USD", "amount": 200}, | ||
| } | ||
| assert json.loads(dash_duo.find_element("#common-json").text) == {"currency": "USD"} | ||
| assert dash_duo.get_logs() == [] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -263,3 +263,68 @@ def test_pat021_patch_iter(): | |
| with pytest.raises(TypeError): | ||
| for _ in p["invalid"]: | ||
| print("invalid iteration") | ||
|
|
||
|
|
||
| def test_pat022_patch_update_shared_mapping(): | ||
| common = {"currency": "USD"} | ||
| p = Patch() | ||
| p["jan"].update(common, amount=100) | ||
| p["feb"].update(common, amount=200) | ||
|
|
||
| assert patch_to_dict(p)["operations"] == [ | ||
| { | ||
| "operation": "Merge", | ||
| "location": ["jan"], | ||
| "params": {"value": {"currency": "USD", "amount": 100}}, | ||
| }, | ||
| { | ||
| "operation": "Merge", | ||
| "location": ["feb"], | ||
| "params": {"value": {"currency": "USD", "amount": 200}}, | ||
| }, | ||
| ] | ||
| assert common == {"currency": "USD"} | ||
|
|
||
|
|
||
| def test_pat023_patch_update_keywords_and_shallow_values(): | ||
| nested = {"retained": 1} | ||
| common = {"amount": 0, "nested": nested} | ||
| p = Patch() | ||
| p.update(common, amount=100) | ||
|
|
||
| value = p.to_plotly_json()["operations"][0]["params"]["value"] | ||
| assert value == {"amount": 100, "nested": {"retained": 1}} | ||
| assert value["nested"] is nested | ||
| assert common == {"amount": 0, "nested": {"retained": 1}} | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("args", [(), (None,), ({},)]) | ||
| @pytest.mark.parametrize("kwargs", [{}, {"currency": "USD"}]) | ||
| def test_pat024_patch_update_empty(args, kwargs): | ||
| p = Patch() | ||
| p.update(*args, **kwargs) | ||
|
|
||
| assert patch_to_dict(p)["operations"] == [ | ||
| {"operation": "Merge", "location": [], "params": {"value": kwargs}} | ||
| ] | ||
|
|
||
|
|
||
| def test_pat025_patch_update_union(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| common = {"currency": "USD"} | ||
| p = Patch() | ||
| p |= common | ||
| p["jan"] |= common | ||
|
|
||
| assert patch_to_dict(p)["operations"] == [ | ||
| { | ||
| "operation": "Merge", | ||
| "location": [], | ||
| "params": {"value": {"currency": "USD"}}, | ||
| }, | ||
| { | ||
| "operation": "Merge", | ||
| "location": ["jan"], | ||
| "params": {"value": {"currency": "USD"}}, | ||
| }, | ||
| ] | ||
| assert common == {"currency": "USD"} | ||
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.
The
common-jsonoutput only checks that the input dict wasn't changed, andtest_pat022already covers that without a browser. Can you drop it so this test only checks the Store result?