Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 06a27fc

Browse files
byronbyronz
authored andcommitted
✅ add new test for data lifecycle
1 parent 9c0a889 commit 06a27fc

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

tests/integration/store/conftest.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import json
2+
import pytest
3+
import uuid
4+
import dash
5+
from dash.dependencies import Input, Output, State
6+
from dash.exceptions import PreventUpdate
7+
import dash_core_components as dcc
8+
import dash_html_components as html
9+
10+
UUID = "store-test-{}".format(uuid.uuid4().hex)
11+
12+
@pytest.fixture(scope="module")
13+
def store_app():
14+
app = dash.Dash(__name__)
15+
app.uuid = UUID
16+
app.layout = html.Div(
17+
[
18+
dcc.Store(id="memory", storage_type="memory", data=app.uuid),
19+
dcc.Store(id="local", storage_type="local"),
20+
dcc.Store(id="session", storage_type="session"),
21+
html.Button("click me", id="btn"),
22+
html.Button("clear data", id="clear-btn"),
23+
html.Div(id="output"),
24+
]
25+
)
26+
27+
@app.callback(
28+
Output("output", "children"),
29+
[Input("memory", "modified_timestamp")],
30+
[State("memory", "data")],
31+
)
32+
def write_memory(modified_ts, data):
33+
return json.dumps(data)
34+
35+
@app.callback(
36+
[
37+
Output("local", "clear_data"),
38+
Output("memory", "clear_data"),
39+
Output("session", "clear_data"),
40+
],
41+
[Input("clear-btn", "n_clicks")],
42+
)
43+
def on_clear(n_clicks):
44+
if n_clicks is None:
45+
raise PreventUpdate
46+
return True
47+
48+
@app.callback(
49+
[
50+
Output("memory", "data"),
51+
Output("local", "data"),
52+
Output("session", "data"),
53+
],
54+
[Input("btn", "n_clicks")],
55+
)
56+
def on_click(n_clicks):
57+
if n_clicks is None:
58+
raise PreventUpdate
59+
return ({"n_clicks": n_clicks},) * 3
60+
61+
yield app
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
def test_stdl001_data_lifecycle_with_different_condition(store_app, dash_duo):
2+
dash_duo.start_server(store_app)
3+
4+
nclicks = 10
5+
dash_duo.multiple_click("#btn", nclicks)
6+
7+
dash_duo.wait_for_text_to_equal(
8+
"#output", '{{"n_clicks": {}}}'.format(nclicks)
9+
)
10+
assert dash_duo.local_storage == {
11+
"n_clicks": nclicks
12+
}, "local storage should contain the same click nums"
13+
assert dash_duo.session_storage == {
14+
"n_clicks": nclicks
15+
}, "session storage should contain the same click nums"
16+
17+
dash_duo.driver.refresh()
18+
assert dash_duo.find_element("#output").text == '"{}"'.format(
19+
store_app.uuid
20+
), "a browser refresh will clear the memory type data to initial data"
21+
assert dash_duo.local_storage == {"n_clicks": nclicks}
22+
assert dash_duo.session_storage == {"n_clicks": nclicks}
23+
24+
dash_duo.open_new_tab()
25+
dash_duo.toggle_window() # switch to the new tab
26+
assert dash_duo.local_storage == {
27+
"n_clicks": nclicks
28+
}, "local storage should be persistent"
29+
assert dash_duo.find_element("#output").text == '"{}"'.format(
30+
store_app.uuid
31+
), "memory storage should contain the initial data in new tab"
32+
33+
dash_duo.multiple_click("#btn", 2)
34+
assert dash_duo.session_storage == {"n_clicks": 2}
35+
assert (
36+
'"n_clicks": 2' in dash_duo.wait_for_element("#output").text
37+
), "memory storage should reflect to the new clicks"
38+
39+
dash_duo.driver.close()
40+
dash_duo.switch_window()
41+
assert dash_duo.local_storage == {"n_clicks": 2}
42+
assert dash_duo.find_element("#output").text == '"{}"'.format(
43+
store_app.uuid
44+
), "memory output should be the same as after previous refresh"
45+
assert dash_duo.session_storage == {
46+
"n_clicks": nclicks
47+
}, "session storage should be specific per browser tab window"

0 commit comments

Comments
 (0)