|
| 1 | +import json |
| 2 | +import time |
| 3 | +import itertools |
| 4 | +from pytest import approx |
| 5 | +import dash |
| 6 | +from dash.dependencies import Input, Output, State |
| 7 | +from dash.exceptions import PreventUpdate |
| 8 | +import dash_core_components as dcc |
| 9 | +import dash_html_components as html |
| 10 | + |
| 11 | + |
| 12 | +def test_stco001_storage_component_smoke(store_app, dash_duo): |
| 13 | + |
| 14 | + app = dash.Dash(__name__) |
| 15 | + app.layout = html.Div( |
| 16 | + [ |
| 17 | + dcc.Store(id="memory", storage_type="memory"), |
| 18 | + dcc.Store(id="storage", storage_type="local"), |
| 19 | + dcc.Store(id="session", storage_type="session", data=dummy_data), |
| 20 | + dcc.Store(id="initial-storage", storage_type="session"), |
| 21 | + html.Button("click me", id="btn"), |
| 22 | + html.Button("clear", id="clear-btn"), |
| 23 | + html.Button("set-init-storage", id="set-init-storage"), |
| 24 | + html.Div(id="memory-output"), |
| 25 | + html.Div(id="init-output"), |
| 26 | + ] |
| 27 | + ) |
| 28 | + |
| 29 | + @app.callback( |
| 30 | + Output("storage", "data"), |
| 31 | + [Input("btn", "n_clicks")], |
| 32 | + [State("storage", "data")], |
| 33 | + ) |
| 34 | + def on_click(n_clicks, storage): |
| 35 | + if n_clicks is None: |
| 36 | + return |
| 37 | + storage = storage or {} |
| 38 | + return {"clicked": storage.get("clicked", 0) + 1} |
| 39 | + |
| 40 | + @app.callback( |
| 41 | + Output("storage", "clear_data"), [Input("clear-btn", "n_clicks")] |
| 42 | + ) |
| 43 | + def on_clear(n_clicks): |
| 44 | + if n_clicks is None: |
| 45 | + return |
| 46 | + return True |
| 47 | + |
| 48 | + @app.callback(Output("memory", "data"), [Input("storage", "data")]) |
| 49 | + def on_memory(data): |
| 50 | + return data |
| 51 | + |
| 52 | + @app.callback( |
| 53 | + Output("memory-output", "children"), [Input("memory", "data")] |
| 54 | + ) |
| 55 | + def on_memory_json(data): |
| 56 | + if data is None: |
| 57 | + return "" |
| 58 | + return json.dumps(data) |
| 59 | + |
| 60 | + @app.callback( |
| 61 | + Output("initial-storage", "data"), |
| 62 | + [Input("set-init-storage", "n_clicks")], |
| 63 | + ) |
| 64 | + def on_init(n_clicks): |
| 65 | + if n_clicks is None: |
| 66 | + raise PreventUpdate |
| 67 | + |
| 68 | + return "initialized" |
| 69 | + |
| 70 | + @app.callback( |
| 71 | + Output("init-output", "children"), |
| 72 | + [Input("initial-storage", "modified_timestamp")], |
| 73 | + [State("initial-storage", "data")], |
| 74 | + ) |
| 75 | + def init_output(ts, data): |
| 76 | + return json.dumps({"data": data, "ts": ts}) |
| 77 | + |
| 78 | + dash_duo.start_server(app) |
| 79 | + |
| 80 | + getter = 'return JSON.parse(window.{}.getItem("{}"));' |
| 81 | + clicked_getter = getter.format("localStorage", "storage") |
| 82 | + |
| 83 | + session = dash_duo.driver.execute_script( |
| 84 | + getter.format("sessionStorage", "session") |
| 85 | + ) |
| 86 | + assert dummy_data == session |
| 87 | + |
| 88 | + for i in range(1, 11): |
| 89 | + dash_duo.find_element("#btn").click() |
| 90 | + click_data = dash_duo.driver.execute_script(clicked_getter) |
| 91 | + assert i == click_data.get("clicked") |
| 92 | + mem = dash_duo.wait_for_element("#memory-output") |
| 93 | + assert i == int(json.loads(mem.text).get("clicked")) |
| 94 | + |
| 95 | + |
| 96 | + # Test initial timestamp output |
| 97 | + dash_duo.find_element("#set-init-storage").click() |
| 98 | + # the python ts ends at seconds while javascript one ends at ms |
| 99 | + ts = float(time.time() * 1000) |
| 100 | + dash_duo.driver.refresh() |
| 101 | + init = json.loads(dash_duo.wait_for_element("#init-output").text) |
| 102 | + |
| 103 | + assert ts == approx(init.get("ts"), abs=10) |
| 104 | + assert init.get("data") == "initialized" |
| 105 | + |
| 106 | + |
| 107 | +def test_store_nested_data(dash_duo): |
| 108 | + app = dash.Dash(__name__) |
| 109 | + |
| 110 | + nested = {"nested": {"nest": "much"}} |
| 111 | + nested_list = dict(my_list=[1, 2, 3]) |
| 112 | + |
| 113 | + app.layout = html.Div( |
| 114 | + [ |
| 115 | + dcc.Store(id="store", storage_type="local"), |
| 116 | + html.Button("set object as key", id="obj-btn"), |
| 117 | + html.Button("set list as key", id="list-btn"), |
| 118 | + html.Output(id="output"), |
| 119 | + ] |
| 120 | + ) |
| 121 | + |
| 122 | + @app.callback( |
| 123 | + Output("store", "data"), |
| 124 | + [ |
| 125 | + Input("obj-btn", "n_clicks_timestamp"), |
| 126 | + Input("list-btn", "n_clicks_timestamp"), |
| 127 | + ], |
| 128 | + ) |
| 129 | + def on_obj_click(obj_ts, list_ts): |
| 130 | + if obj_ts is None and list_ts is None: |
| 131 | + raise PreventUpdate |
| 132 | + |
| 133 | + # python 3 got the default props bug. plotly/dash#396 |
| 134 | + if (obj_ts and not list_ts) or obj_ts > list_ts: |
| 135 | + return nested |
| 136 | + else: |
| 137 | + return nested_list |
| 138 | + |
| 139 | + @app.callback( |
| 140 | + Output("output", "children"), |
| 141 | + [Input("store", "modified_timestamp")], |
| 142 | + [State("store", "data")], |
| 143 | + ) |
| 144 | + def on_ts(ts, data): |
| 145 | + if ts is None: |
| 146 | + raise PreventUpdate |
| 147 | + return json.dumps(data) |
| 148 | + |
| 149 | + dash_duo.start_server(app) |
| 150 | + |
| 151 | + obj_btn = dash_duo.wait_for_element_by_css_selector("#obj-btn") |
| 152 | + list_btn = dash_duo.wait_for_element_by_css_selector("#list-btn") |
| 153 | + |
| 154 | + obj_btn.click() |
| 155 | + time.sleep(1) |
| 156 | + dash_duo.wait_for_text_to_equal("#output", json.dumps(nested)) |
| 157 | + # it would of crashed the app before adding the recursive check. |
| 158 | + |
| 159 | + list_btn.click() |
| 160 | + time.sleep(1) |
| 161 | + dash_duo.wait_for_text_to_equal("#output", json.dumps(nested_list)) |
| 162 | + |
| 163 | + |
| 164 | +def test_stco003_data_type_updates(dash_duo): |
| 165 | + app = dash.Dash(__name__) |
| 166 | + |
| 167 | + types = [ |
| 168 | + ("str", "hello"), |
| 169 | + ("number", 1), |
| 170 | + ("dict", {"data": [2, 3, None]}), |
| 171 | + ("list", [5, -6, 700000, 1e-12]), |
| 172 | + ("null", None), |
| 173 | + ("bool", True), |
| 174 | + ("bool", False), |
| 175 | + ("empty-dict", {}), |
| 176 | + ] |
| 177 | + types_changes = list( |
| 178 | + itertools.chain(*itertools.combinations(types, 2)) |
| 179 | + ) + [ # No combinations as it add much test time. |
| 180 | + ("list-dict-1", [1, 2, {"data": [55, 66, 77], "dummy": "dum"}]), |
| 181 | + ("list-dict-2", [1, 2, {"data": [111, 99, 88]}]), |
| 182 | + ("dict-3", {"a": 1, "c": 1}), |
| 183 | + ("dict-2", {"a": 1, "b": None}), |
| 184 | + ] |
| 185 | + |
| 186 | + app.layout = html.Div( |
| 187 | + [ |
| 188 | + html.Div(id="output"), |
| 189 | + html.Button("click", id="click"), |
| 190 | + dcc.Store(id="store"), |
| 191 | + ] |
| 192 | + ) |
| 193 | + |
| 194 | + @app.callback( |
| 195 | + Output("output", "children"), |
| 196 | + [Input("store", "modified_timestamp")], |
| 197 | + [State("store", "data")], |
| 198 | + ) |
| 199 | + def on_data(ts, data): |
| 200 | + if ts is None: |
| 201 | + raise PreventUpdate |
| 202 | + return json.dumps(data) |
| 203 | + |
| 204 | + @app.callback(Output("store", "data"), [Input("click", "n_clicks")]) |
| 205 | + def on_click(n_clicks): |
| 206 | + if n_clicks is None: |
| 207 | + raise PreventUpdate |
| 208 | + return types_changes[n_clicks - 1][1] |
| 209 | + |
| 210 | + dash_duo.start_server(app) |
| 211 | + |
| 212 | + button = dash_duo.wait_for_element("#click") |
| 213 | + for type_change in types_changes: |
| 214 | + button.click() |
| 215 | + dash_duo.wait_for_text_to_equal("#output", json.dumps(type_change[1])) |
0 commit comments