Skip to content

Commit

Permalink
Run a few tests with both json and orjson encoders
Browse files Browse the repository at this point in the history
  • Loading branch information
jonmmease committed Aug 16, 2021
1 parent e531a58 commit a167542
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 43 deletions.
1 change: 1 addition & 0 deletions requires-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ black==21.6b0
fire==0.4.0
coloredlogs==15.0.1
flask-talisman==0.8.1
orjson>=3.6
91 changes: 48 additions & 43 deletions tests/integration/callbacks/test_basic_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
from dash.testing import wait
from tests.integration.utils import json_engine


def test_cbsc001_simple_callback(dash_duo):
Expand Down Expand Up @@ -248,57 +249,61 @@ def update_out2(n_clicks, data):
assert dash_duo.get_logs() == []


def test_cbsc005_children_types(dash_duo):
app = dash.Dash()
app.layout = html.Div([html.Button(id="btn"), html.Div("init", id="out")])

outputs = [
[None, ""],
["a string", "a string"],
[123, "123"],
[123.45, "123.45"],
[[6, 7, 8], "678"],
[["a", "list", "of", "strings"], "alistofstrings"],
[["strings", 2, "numbers"], "strings2numbers"],
[["a string", html.Div("and a div")], "a string\nand a div"],
]

@app.callback(Output("out", "children"), [Input("btn", "n_clicks")])
def set_children(n):
if n is None or n > len(outputs):
return dash.no_update
return outputs[n - 1][0]
@pytest.mark.parametrize("engine", ["json", "orjson"])
def test_cbsc005_children_types(dash_duo, engine):
with json_engine(engine):
app = dash.Dash()
app.layout = html.Div([html.Button(id="btn"), html.Div("init", id="out")])

outputs = [
[None, ""],
["a string", "a string"],
[123, "123"],
[123.45, "123.45"],
[[6, 7, 8], "678"],
[["a", "list", "of", "strings"], "alistofstrings"],
[["strings", 2, "numbers"], "strings2numbers"],
[["a string", html.Div("and a div")], "a string\nand a div"],
]

dash_duo.start_server(app)
dash_duo.wait_for_text_to_equal("#out", "init")
@app.callback(Output("out", "children"), [Input("btn", "n_clicks")])
def set_children(n):
if n is None or n > len(outputs):
return dash.no_update
return outputs[n - 1][0]

for children, text in outputs:
dash_duo.find_element("#btn").click()
dash_duo.wait_for_text_to_equal("#out", text)
dash_duo.start_server(app)
dash_duo.wait_for_text_to_equal("#out", "init")

for children, text in outputs:
dash_duo.find_element("#btn").click()
dash_duo.wait_for_text_to_equal("#out", text)

def test_cbsc006_array_of_objects(dash_duo):
app = dash.Dash()
app.layout = html.Div(
[html.Button(id="btn"), dcc.Dropdown(id="dd"), html.Div(id="out")]
)

@app.callback(Output("dd", "options"), [Input("btn", "n_clicks")])
def set_options(n):
return [{"label": "opt{}".format(i), "value": i} for i in range(n or 0)]
@pytest.mark.parametrize("engine", ["json", "orjson"])
def test_cbsc006_array_of_objects(dash_duo, engine):
with json_engine(engine):
app = dash.Dash()
app.layout = html.Div(
[html.Button(id="btn"), dcc.Dropdown(id="dd"), html.Div(id="out")]
)

@app.callback(Output("out", "children"), [Input("dd", "options")])
def set_out(opts):
print(repr(opts))
return len(opts)
@app.callback(Output("dd", "options"), [Input("btn", "n_clicks")])
def set_options(n):
return [{"label": "opt{}".format(i), "value": i} for i in range(n or 0)]

dash_duo.start_server(app)
@app.callback(Output("out", "children"), [Input("dd", "options")])
def set_out(opts):
print(repr(opts))
return len(opts)

dash_duo.wait_for_text_to_equal("#out", "0")
for i in range(5):
dash_duo.find_element("#btn").click()
dash_duo.wait_for_text_to_equal("#out", str(i + 1))
dash_duo.select_dcc_dropdown("#dd", "opt{}".format(i))
dash_duo.start_server(app)

dash_duo.wait_for_text_to_equal("#out", "0")
for i in range(5):
dash_duo.find_element("#btn").click()
dash_duo.wait_for_text_to_equal("#out", str(i + 1))
dash_duo.select_dcc_dropdown("#dd", "opt{}".format(i))


@pytest.mark.parametrize("refresh", [False, True])
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
import contextlib


TIMEOUT = 5 # Seconds
Expand Down Expand Up @@ -61,3 +62,15 @@ def wrapped_condition_function():
time.sleep(0.5)

raise WaitForTimeout(get_message())


@contextlib.contextmanager
def json_engine(engine):
import plotly.io as pio

original_engine = pio.json.config.default_engine
try:
pio.json.config.default_engine = engine
yield
finally:
pio.json.config.default_engine = original_engine

0 comments on commit a167542

Please sign in to comment.