Skip to content

Commit

Permalink
Fix tests with dynamic port.
Browse files Browse the repository at this point in the history
  • Loading branch information
T4rk1n committed Apr 11, 2022
1 parent 77984d1 commit 8b9b9cb
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def extras(t):
dcc.Link(
children="Absolute Path",
id="link1",
href=dash_dcc.server.url + "/extra/eseehc",
href="/extra/eseehc",
refresh=True,
),
dcc.Location(id="url", refresh=False),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def display_page(pathname):
dash_dcc.wait_for_text_to_equal("#page-content", "You are on page /test-link")

wait.until(
lambda: test_link.get_attribute("href") == "http://localhost:8050/test-link", 3
lambda: test_link.get_attribute("href")
== "http://localhost:{}/test-link".format(dash_dcc.server.port),
3,
)
wait.until(lambda: call_count.value == 2, 3)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def update_pathname(n_clicks, current_pathname):
# Check that link updates pathname
dash_dcc.find_element("#test-link").click()
until(
lambda: dash_dcc.driver.current_url.replace("http://localhost:8050", "")
lambda: dash_dcc.driver.current_url.replace(
"http://localhost:{}".format(dash_dcc.server.port), ""
)
== "/test/pathname",
3,
)
Expand Down
35 changes: 25 additions & 10 deletions tests/integration/callbacks/test_basic_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,18 @@ def test_cbsc008_wildcard_prop_callbacks(dash_duo):
)

input_call_count = Value("i", 0)
percy_enabled = Value("b", False)

def snapshot(name):
percy_enabled.value = os.getenv("PERCY_ENABLED", "") != ""
dash_duo.percy_snapshot(name=name)
percy_enabled.value = False

@app.callback(Output("output-1", "data-cb"), [Input("input", "value")])
def update_data(value):
with lock:
input_call_count.value += 1
if not percy_enabled.value:
input_call_count.value += 1
return value

@app.callback(Output("output-1", "children"), [Input("output-1", "data-cb")])
Expand All @@ -413,7 +420,7 @@ def update_text(data):

dash_duo.start_server(app)
dash_duo.wait_for_text_to_equal("#output-1", "initial value")
dash_duo.percy_snapshot(name="wildcard-callback-1")
snapshot("wildcard-callback-1")

input1 = dash_duo.find_element("#input")
dash_duo.clear_input(input1)
Expand All @@ -423,7 +430,7 @@ def update_text(data):
input1.send_keys(key)

dash_duo.wait_for_text_to_equal("#output-1", "hello world")
dash_duo.percy_snapshot(name="wildcard-callback-2")
snapshot("wildcard-callback-2")

# an initial call, one for clearing the input
# and one for each hello world character
Expand Down Expand Up @@ -612,6 +619,13 @@ def test_cbsc014_multiple_properties_update_at_same_time_on_same_component(dash_
timestamp_1 = Value("d", -5)
timestamp_2 = Value("d", -5)

percy_enabled = Value("b")

def snapshot(name):
percy_enabled.value = os.getenv("PERCY_ENABLED", "") != ""
dash_duo.percy_snapshot(name=name)
percy_enabled.value = False

app = Dash(__name__)
app.layout = html.Div(
[
Expand All @@ -629,9 +643,10 @@ def test_cbsc014_multiple_properties_update_at_same_time_on_same_component(dash_
Input("button-2", "n_clicks_timestamp"),
)
def update_output(n1, t1, n2, t2):
call_count.value += 1
timestamp_1.value = t1
timestamp_2.value = t2
if not percy_enabled.value:
call_count.value += 1
timestamp_1.value = t1
timestamp_2.value = t2
return "{}, {}".format(n1, n2)

dash_duo.start_server(app)
Expand All @@ -640,22 +655,22 @@ def update_output(n1, t1, n2, t2):
assert timestamp_1.value == -1
assert timestamp_2.value == -1
assert call_count.value == 1
dash_duo.percy_snapshot("Dash button-1 initialization 1")
snapshot("Dash button-1 initialization 1")

dash_duo.find_element("#button-1").click()
dash_duo.wait_for_text_to_equal("#container", "1, 0")
assert timestamp_1.value > ((time.time() - (24 * 60 * 60)) * 1000)
assert timestamp_2.value == -1
assert call_count.value == 2
dash_duo.percy_snapshot("Dash button-1 click")
snapshot("Dash button-1 click")
prev_timestamp_1 = timestamp_1.value

dash_duo.find_element("#button-2").click()
dash_duo.wait_for_text_to_equal("#container", "1, 1")
assert timestamp_1.value == prev_timestamp_1
assert timestamp_2.value > ((time.time() - 24 * 60 * 60) * 1000)
assert call_count.value == 3
dash_duo.percy_snapshot("Dash button-2 click")
snapshot("Dash button-2 click")
prev_timestamp_2 = timestamp_2.value

dash_duo.find_element("#button-2").click()
Expand All @@ -664,7 +679,7 @@ def update_output(n1, t1, n2, t2):
assert timestamp_2.value > prev_timestamp_2
assert timestamp_2.value > timestamp_1.value
assert call_count.value == 4
dash_duo.percy_snapshot("Dash button-2 click again")
snapshot("Dash button-2 click again")


def test_cbsc015_input_output_callback(dash_duo):
Expand Down
26 changes: 18 additions & 8 deletions tests/integration/callbacks/test_layout_paths_with_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ def test_cblp001_radio_buttons_callbacks_generating_children(dash_duo):
with open(os.path.join(os.path.dirname(__file__), "state_path.json")) as fp:
EXPECTED_PATHS = json.load(fp)

percy_enabled = Value("b")

def snapshot(name):
percy_enabled.value = os.getenv("PERCY_ENABLED", "") != ""
dash_duo.percy_snapshot(name=name)
percy_enabled.value = False

app = Dash(__name__)
app.layout = html.Div(
[
Expand Down Expand Up @@ -96,14 +103,16 @@ def test_cblp001_radio_buttons_callbacks_generating_children(dash_duo):

@app.callback(Output("body", "children"), [Input("toc", "value")])
def display_chapter(toc_value):
call_counts["body"].value += 1
if not percy_enabled.value:
call_counts["body"].value += 1
return chapters[toc_value]

app.config.suppress_callback_exceptions = True

def generate_graph_callback(counterId):
def callback(value):
call_counts[counterId].value += 1
if not percy_enabled.value:
call_counts[counterId].value += 1
return {
"data": [
{
Expand All @@ -124,7 +133,8 @@ def callback(value):

def generate_label_callback(id_):
def update_label(value):
call_counts[id_].value += 1
if not percy_enabled.value:
call_counts[id_].value += 1
return value

return update_label
Expand Down Expand Up @@ -187,7 +197,7 @@ def check_call_counts(chapters, count):

assert dash_duo.redux_state_paths == EXPECTED_PATHS["chapter1"]
check_chapter("chapter1")
dash_duo.percy_snapshot(name="chapter-1")
snapshot(name="chapter-1")

dash_duo.find_elements('input[type="radio"]')[1].click() # switch chapters

Expand All @@ -198,7 +208,7 @@ def check_call_counts(chapters, count):

assert dash_duo.redux_state_paths == EXPECTED_PATHS["chapter2"]
check_chapter("chapter2")
dash_duo.percy_snapshot(name="chapter-2")
snapshot(name="chapter-2")

# switch to 3
dash_duo.find_elements('input[type="radio"]')[2].click()
Expand All @@ -210,11 +220,11 @@ def check_call_counts(chapters, count):

assert dash_duo.redux_state_paths == EXPECTED_PATHS["chapter3"]
check_chapter("chapter3")
dash_duo.percy_snapshot(name="chapter-3")
snapshot(name="chapter-3")

dash_duo.find_elements('input[type="radio"]')[3].click() # switch to 4
dash_duo.wait_for_text_to_equal("#body", "Just a string")
dash_duo.percy_snapshot(name="chapter-4")
snapshot(name="chapter-4")

paths = dash_duo.redux_state_paths
assert paths["objs"] == {}
Expand All @@ -234,4 +244,4 @@ def check_call_counts(chapters, count):
lambda: dash_duo.redux_state_paths == EXPECTED_PATHS["chapter1"], TIMEOUT
)
check_chapter("chapter1")
dash_duo.percy_snapshot(name="chapter-1-again")
snapshot(name="chapter-1-again")
15 changes: 12 additions & 3 deletions tests/integration/renderer/test_dependencies.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from multiprocessing import Value

from dash import Dash, html, dcc, Input, Output
Expand All @@ -10,10 +11,17 @@ def test_rddp001_dependencies_on_components_that_dont_exist(dash_duo):
)

output_1_call_count = Value("i", 0)
percy_enabled = Value("b")

def snapshot(name):
percy_enabled.value = os.getenv("PERCY_ENABLED", "") != ""
dash_duo.percy_snapshot(name=name)
percy_enabled.value = False

@app.callback(Output("output-1", "children"), [Input("input", "value")])
def update_output(value):
output_1_call_count.value += 1
if not percy_enabled.value:
output_1_call_count.value += 1
return value

# callback for component that doesn't yet exist in the dom
Expand All @@ -23,14 +31,15 @@ def update_output(value):

@app.callback(Output("output-2", "children"), [Input("input", "value")])
def update_output_2(value):
output_2_call_count.value += 1
if not percy_enabled.value:
output_2_call_count.value += 1
return value

dash_duo.start_server(app)

assert dash_duo.find_element("#output-1").text == "initial value"
assert output_1_call_count.value == 1 and output_2_call_count.value == 0
dash_duo.percy_snapshot(name="dependencies")
snapshot("dependencies")

dash_duo.find_element("#input").send_keys("a")
assert dash_duo.find_element("#output-1").text == "initial valuea"
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,10 @@ def test_inin025_url_base_pathname(dash_br, dash_thread_server):

dash_thread_server(app)

dash_br.server_url = "http://localhost:8050/app1/"
dash_br.server_url = "http://localhost:{}/app1/".format(dash_thread_server.port)
dash_br.wait_for_text_to_equal("#out", "The first")

dash_br.server_url = "http://localhost:8050/app2/"
dash_br.server_url = "http://localhost:{}/app2/".format(dash_thread_server.port)
dash_br.wait_for_text_to_equal("#out", "The second")


Expand Down

0 comments on commit 8b9b9cb

Please sign in to comment.