Skip to content

Commit a167542

Browse files
committed
Run a few tests with both json and orjson encoders
1 parent e531a58 commit a167542

File tree

3 files changed

+62
-43
lines changed

3 files changed

+62
-43
lines changed

Diff for: requires-dev.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ black==21.6b0
1010
fire==0.4.0
1111
coloredlogs==15.0.1
1212
flask-talisman==0.8.1
13+
orjson>=3.6

Diff for: tests/integration/callbacks/test_basic_callback.py

+48-43
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from dash.dependencies import Input, Output, State
1717
from dash.exceptions import PreventUpdate
1818
from dash.testing import wait
19+
from tests.integration.utils import json_engine
1920

2021

2122
def test_cbsc001_simple_callback(dash_duo):
@@ -248,57 +249,61 @@ def update_out2(n_clicks, data):
248249
assert dash_duo.get_logs() == []
249250

250251

251-
def test_cbsc005_children_types(dash_duo):
252-
app = dash.Dash()
253-
app.layout = html.Div([html.Button(id="btn"), html.Div("init", id="out")])
254-
255-
outputs = [
256-
[None, ""],
257-
["a string", "a string"],
258-
[123, "123"],
259-
[123.45, "123.45"],
260-
[[6, 7, 8], "678"],
261-
[["a", "list", "of", "strings"], "alistofstrings"],
262-
[["strings", 2, "numbers"], "strings2numbers"],
263-
[["a string", html.Div("and a div")], "a string\nand a div"],
264-
]
265-
266-
@app.callback(Output("out", "children"), [Input("btn", "n_clicks")])
267-
def set_children(n):
268-
if n is None or n > len(outputs):
269-
return dash.no_update
270-
return outputs[n - 1][0]
252+
@pytest.mark.parametrize("engine", ["json", "orjson"])
253+
def test_cbsc005_children_types(dash_duo, engine):
254+
with json_engine(engine):
255+
app = dash.Dash()
256+
app.layout = html.Div([html.Button(id="btn"), html.Div("init", id="out")])
257+
258+
outputs = [
259+
[None, ""],
260+
["a string", "a string"],
261+
[123, "123"],
262+
[123.45, "123.45"],
263+
[[6, 7, 8], "678"],
264+
[["a", "list", "of", "strings"], "alistofstrings"],
265+
[["strings", 2, "numbers"], "strings2numbers"],
266+
[["a string", html.Div("and a div")], "a string\nand a div"],
267+
]
271268

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

275-
for children, text in outputs:
276-
dash_duo.find_element("#btn").click()
277-
dash_duo.wait_for_text_to_equal("#out", text)
275+
dash_duo.start_server(app)
276+
dash_duo.wait_for_text_to_equal("#out", "init")
278277

278+
for children, text in outputs:
279+
dash_duo.find_element("#btn").click()
280+
dash_duo.wait_for_text_to_equal("#out", text)
279281

280-
def test_cbsc006_array_of_objects(dash_duo):
281-
app = dash.Dash()
282-
app.layout = html.Div(
283-
[html.Button(id="btn"), dcc.Dropdown(id="dd"), html.Div(id="out")]
284-
)
285282

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

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

295-
dash_duo.start_server(app)
295+
@app.callback(Output("out", "children"), [Input("dd", "options")])
296+
def set_out(opts):
297+
print(repr(opts))
298+
return len(opts)
296299

297-
dash_duo.wait_for_text_to_equal("#out", "0")
298-
for i in range(5):
299-
dash_duo.find_element("#btn").click()
300-
dash_duo.wait_for_text_to_equal("#out", str(i + 1))
301-
dash_duo.select_dcc_dropdown("#dd", "opt{}".format(i))
300+
dash_duo.start_server(app)
301+
302+
dash_duo.wait_for_text_to_equal("#out", "0")
303+
for i in range(5):
304+
dash_duo.find_element("#btn").click()
305+
dash_duo.wait_for_text_to_equal("#out", str(i + 1))
306+
dash_duo.select_dcc_dropdown("#dd", "opt{}".format(i))
302307

303308

304309
@pytest.mark.parametrize("refresh", [False, True])

Diff for: tests/integration/utils.py

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
import contextlib
23

34

45
TIMEOUT = 5 # Seconds
@@ -61,3 +62,15 @@ def wrapped_condition_function():
6162
time.sleep(0.5)
6263

6364
raise WaitForTimeout(get_message())
65+
66+
67+
@contextlib.contextmanager
68+
def json_engine(engine):
69+
import plotly.io as pio
70+
71+
original_engine = pio.json.config.default_engine
72+
try:
73+
pio.json.config.default_engine = engine
74+
yield
75+
finally:
76+
pio.json.config.default_engine = original_engine

0 commit comments

Comments
 (0)