Skip to content

Commit 90697b1

Browse files
author
Marc-André Rivet
committed
no global variables
1 parent f4086c6 commit 90697b1

File tree

1 file changed

+68
-77
lines changed

1 file changed

+68
-77
lines changed

tests/integration/callbacks/test_callback_context.py

Lines changed: 68 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import operator
33
import pytest
4+
45
import dash_html_components as html
56
import dash_core_components as dcc
67

@@ -101,13 +102,13 @@ def report_triggered(n):
101102
)
102103

103104

104-
calls = 0
105-
callback_contexts = []
106-
clicks = dict()
107-
108-
109105
@pytest.mark.DASH1350
110106
def test_cbcx005_grouped_clicks(dash_duo):
107+
class context:
108+
calls = 0
109+
callback_contexts = []
110+
clicks = dict()
111+
111112
app = Dash(__name__)
112113
app.layout = html.Div(
113114
[
@@ -141,17 +142,13 @@ def test_cbcx005_grouped_clicks(dash_duo):
141142
prevent_initial_call=True,
142143
)
143144
def update(div1, div2, btn0, btn1, btn2):
144-
global calls
145-
global callback_contexts
146-
global clicks
147-
148-
calls = calls + 1
149-
callback_contexts.append(callback_context.triggered)
150-
clicks["div1"] = div1
151-
clicks["div2"] = div2
152-
clicks["btn0"] = btn0
153-
clicks["btn1"] = btn1
154-
clicks["btn2"] = btn2
145+
context.calls = context.calls + 1
146+
context.callback_contexts.append(callback_context.triggered)
147+
context.clicks["div1"] = div1
148+
context.clicks["div2"] = div2
149+
context.clicks["btn0"] = btn0
150+
context.clicks["btn1"] = btn1
151+
context.clicks["btn2"] = btn2
155152

156153
def click(target):
157154
ActionChains(dash_duo.driver).move_to_element_with_offset(
@@ -160,76 +157,76 @@ def click(target):
160157

161158
dash_duo.start_server(app)
162159
click(dash_duo.find_element("#btn0"))
163-
assert calls == 1
164-
keys = list(map(operator.itemgetter("prop_id"), callback_contexts[-1:][0]))
160+
assert context.calls == 1
161+
keys = list(map(operator.itemgetter("prop_id"), context.callback_contexts[-1:][0]))
165162
assert len(keys) == 1
166163
assert "btn0.n_clicks" in keys
167164

168-
assert clicks.get("btn0") == 1
169-
assert clicks.get("btn1") is None
170-
assert clicks.get("btn2") is None
171-
assert clicks.get("div1") is None
172-
assert clicks.get("div2") is None
165+
assert context.clicks.get("btn0") == 1
166+
assert context.clicks.get("btn1") is None
167+
assert context.clicks.get("btn2") is None
168+
assert context.clicks.get("div1") is None
169+
assert context.clicks.get("div2") is None
173170

174171
click(dash_duo.find_element("#div1"))
175-
assert calls == 2
176-
keys = list(map(operator.itemgetter("prop_id"), callback_contexts[-1:][0]))
172+
assert context.calls == 2
173+
keys = list(map(operator.itemgetter("prop_id"), context.callback_contexts[-1:][0]))
177174
assert len(keys) == 1
178175
assert "div1.n_clicks" in keys
179176

180-
assert clicks.get("btn0") == 1
181-
assert clicks.get("btn1") is None
182-
assert clicks.get("btn2") is None
183-
assert clicks.get("div1") == 1
184-
assert clicks.get("div2") is None
177+
assert context.clicks.get("btn0") == 1
178+
assert context.clicks.get("btn1") is None
179+
assert context.clicks.get("btn2") is None
180+
assert context.clicks.get("div1") == 1
181+
assert context.clicks.get("div2") is None
185182

186183
click(dash_duo.find_element("#btn1"))
187-
assert calls == 3
188-
keys = list(map(operator.itemgetter("prop_id"), callback_contexts[-1:][0]))
184+
assert context.calls == 3
185+
keys = list(map(operator.itemgetter("prop_id"), context.callback_contexts[-1:][0]))
189186
assert len(keys) == 2
190187
assert "btn1.n_clicks" in keys
191188
assert "div1.n_clicks" in keys
192189

193-
assert clicks.get("btn0") == 1
194-
assert clicks.get("btn1") == 1
195-
assert clicks.get("btn2") is None
196-
assert clicks.get("div1") == 2
197-
assert clicks.get("div2") is None
190+
assert context.clicks.get("btn0") == 1
191+
assert context.clicks.get("btn1") == 1
192+
assert context.clicks.get("btn2") is None
193+
assert context.clicks.get("div1") == 2
194+
assert context.clicks.get("div2") is None
198195

199196
click(dash_duo.find_element("#div2"))
200-
assert calls == 4
201-
keys = list(map(operator.itemgetter("prop_id"), callback_contexts[-1:][0]))
197+
assert context.calls == 4
198+
keys = list(map(operator.itemgetter("prop_id"), context.callback_contexts[-1:][0]))
202199
assert len(keys) == 2
203200
assert "div1.n_clicks" in keys
204201
assert "div2.n_clicks" in keys
205202

206-
assert clicks.get("btn0") == 1
207-
assert clicks.get("btn1") == 1
208-
assert clicks.get("btn2") is None
209-
assert clicks.get("div1") == 3
210-
assert clicks.get("div2") == 1
203+
assert context.clicks.get("btn0") == 1
204+
assert context.clicks.get("btn1") == 1
205+
assert context.clicks.get("btn2") is None
206+
assert context.clicks.get("div1") == 3
207+
assert context.clicks.get("div2") == 1
211208

212209
click(dash_duo.find_element("#btn2"))
213-
assert calls == 5
214-
keys = list(map(operator.itemgetter("prop_id"), callback_contexts[-1:][0]))
210+
assert context.calls == 5
211+
keys = list(map(operator.itemgetter("prop_id"), context.callback_contexts[-1:][0]))
215212
assert len(keys) == 3
216213
assert "btn2.n_clicks" in keys
217214
assert "div1.n_clicks" in keys
218215
assert "div2.n_clicks" in keys
219216

220-
assert clicks.get("btn0") == 1
221-
assert clicks.get("btn1") == 1
222-
assert clicks.get("btn2") == 1
223-
assert clicks.get("div1") == 4
224-
assert clicks.get("div2") == 2
225-
226-
227-
cbcx006_calls = 0
228-
cbcx006_contexts = []
217+
assert context.clicks.get("btn0") == 1
218+
assert context.clicks.get("btn1") == 1
219+
assert context.clicks.get("btn2") == 1
220+
assert context.clicks.get("div1") == 4
221+
assert context.clicks.get("div2") == 2
229222

230223

231224
@pytest.mark.DASH1350
232225
def test_cbcx006_initial_callback_predecessor(dash_duo):
226+
class context:
227+
calls = 0
228+
callback_contexts = []
229+
233230
app = Dash(__name__)
234231
app.layout = html.Div(
235232
[
@@ -267,11 +264,8 @@ def test_cbcx006_initial_callback_predecessor(dash_duo):
267264
[Input("input-number-1", "value"), Input("input-number-2", "value")],
268265
)
269266
def update_sum_number(n1, n2):
270-
global cbcx006_calls
271-
global cbcx006_contexts
272-
273-
cbcx006_calls = cbcx006_calls + 1
274-
cbcx006_contexts.append(callback_context.triggered)
267+
context.calls = context.calls + 1
268+
context.callback_contexts.append(callback_context.triggered)
275269

276270
return n1 + n2
277271

@@ -284,11 +278,8 @@ def update_sum_number(n1, n2):
284278
],
285279
)
286280
def update_results(n1, n2, nsum):
287-
global cbcx006_calls
288-
global cbcx006_contexts
289-
290-
cbcx006_calls = cbcx006_calls + 1
291-
cbcx006_contexts.append(callback_context.triggered)
281+
context.calls = context.calls + 1
282+
context.callback_contexts.append(callback_context.triggered)
292283

293284
return [
294285
"{} + {} = {}".format(n1, n2, nsum),
@@ -299,47 +290,47 @@ def update_results(n1, n2, nsum):
299290
dash_duo.start_server(app)
300291

301292
# Initial Callbacks
302-
wait.until(lambda: cbcx006_calls == 2, 2)
303-
wait.until(lambda: len(cbcx006_contexts) == 2, 2)
293+
wait.until(lambda: context.calls == 2, 2)
294+
wait.until(lambda: len(context.callback_contexts) == 2, 2)
304295

305-
keys0 = list(map(operator.itemgetter("prop_id"), cbcx006_contexts[0]))
296+
keys0 = list(map(operator.itemgetter("prop_id"), context.callback_contexts[0]))
306297
# Special case present for backward compatibility
307298
assert len(keys0) == 1
308299
assert "." in keys0
309300

310-
keys1 = list(map(operator.itemgetter("prop_id"), cbcx006_contexts[1]))
301+
keys1 = list(map(operator.itemgetter("prop_id"), context.callback_contexts[1]))
311302
assert len(keys1) == 1
312303
assert "sum-number.value" in keys1
313304

314305
# User action & followup callbacks
315306
dash_duo.find_element("#input-number-1").click()
316307
dash_duo.find_element("#input-number-1").send_keys("1")
317308

318-
wait.until(lambda: cbcx006_calls == 4, 2)
319-
wait.until(lambda: len(cbcx006_contexts) == 4, 2)
309+
wait.until(lambda: context.calls == 4, 2)
310+
wait.until(lambda: len(context.callback_contexts) == 4, 2)
320311

321-
keys0 = list(map(operator.itemgetter("prop_id"), cbcx006_contexts[2]))
312+
keys0 = list(map(operator.itemgetter("prop_id"), context.callback_contexts[2]))
322313
# Special case present for backward compatibility
323314
assert len(keys0) == 1
324315
assert "input-number-1.value" in keys0
325316

326-
keys1 = list(map(operator.itemgetter("prop_id"), cbcx006_contexts[3]))
317+
keys1 = list(map(operator.itemgetter("prop_id"), context.callback_contexts[3]))
327318
assert len(keys1) == 2
328319
assert "sum-number.value" in keys1
329320
assert "input-number-1.value" in keys1
330321

331322
dash_duo.find_element("#input-number-2").click()
332323
dash_duo.find_element("#input-number-2").send_keys("1")
333324

334-
wait.until(lambda: cbcx006_calls == 6, 2)
335-
wait.until(lambda: len(cbcx006_contexts) == 6, 2)
325+
wait.until(lambda: context.calls == 6, 2)
326+
wait.until(lambda: len(context.callback_contexts) == 6, 2)
336327

337-
keys0 = list(map(operator.itemgetter("prop_id"), cbcx006_contexts[4]))
328+
keys0 = list(map(operator.itemgetter("prop_id"), context.callback_contexts[4]))
338329
# Special case present for backward compatibility
339330
assert len(keys0) == 1
340331
assert "input-number-2.value" in keys0
341332

342-
keys1 = list(map(operator.itemgetter("prop_id"), cbcx006_contexts[5]))
333+
keys1 = list(map(operator.itemgetter("prop_id"), context.callback_contexts[5]))
343334
assert len(keys1) == 2
344335
assert "sum-number.value" in keys1
345336
assert "input-number-2.value" in keys1

0 commit comments

Comments
 (0)