-
Notifications
You must be signed in to change notification settings - Fork 20
/
test_reactivity.py
414 lines (371 loc) · 14.2 KB
/
test_reactivity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# -*- coding: utf-8 -*-
import logging
import sys
from test.utils import make_flow_fixture
from typing import Optional, Set, Tuple
from ipyflow.config import ExecutionMode
from ipyflow.data_model.cell import cells
from ipyflow.singletons import flow
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.ERROR)
# Reset dependency graph before each test
# _flow_fixture, run_cell_ = make_flow_fixture(trace_messages_enabled=True)
_flow_fixture, run_cell_ = make_flow_fixture()
def run_cell(
cell_content: str, cell_id: Optional[int] = None, ready_are_reactive: bool = False
) -> Tuple[int, Set[int]]:
orig_mode = flow().mut_settings.exec_mode
try:
if ready_are_reactive:
flow().mut_settings.exec_mode = ExecutionMode.REACTIVE
executed_cells = set()
reactive_cells = set()
next_content_to_run = cell_content
next_cell_to_run_id = cell_id
while next_content_to_run is not None:
executed_cells.add(
run_cell_(next_content_to_run, cell_id=next_cell_to_run_id)
)
if len(executed_cells) == 1:
cell_id = next(iter(executed_cells))
next_content_to_run = None
checker_result = flow().check_and_link_multiple_cells()
if ready_are_reactive:
reactive_cells |= checker_result.new_ready_cells
else:
reactive_cells |= checker_result.forced_reactive_cells
for reactive_cell_id in sorted(reactive_cells - executed_cells):
next_content_to_run = cells().from_id(reactive_cell_id).executed_content
next_cell_to_run_id = reactive_cell_id
break
return cell_id, executed_cells
finally:
flow().mut_settings.exec_mode = orig_mode
flow().handle_reactivity_cleanup()
def run_reactively(
cell_content: str, cell_id: Optional[int] = None
) -> Tuple[int, Set[int]]:
return run_cell(cell_content, cell_id=cell_id, ready_are_reactive=True)
# this is working with DAG semantics; need to update tests for that
def test_mutate_one_list_entry():
assert run_reactively("lst = [1, 2, 3]")[1] == {1}
assert run_reactively("logging.info(lst[0])")[1] == {2}
assert run_reactively("logging.info(lst[1])")[1] == {3}
assert run_reactively("logging.info(lst[2])")[1] == {4}
for i in range(3):
cell_id, cells_run = run_reactively(f"lst[{i}] += 1")
assert cells_run - {cell_id} == {i + 2}, "got %s" % cells_run
cell_id, cells_run = run_reactively("lst.append(3)")
assert cells_run - {cell_id} == set(), "got %s" % cells_run
if sys.version_info >= (3, 8):
def test_simple_reactive_var_load():
assert run_cell("x = 0")[1] == {1}
assert run_cell("y = $x + 1")[1] == {2}
assert run_cell("logging.info($y)")[1] == {3}
assert run_cell("x = 42")[1] == {4, 2, 3}
cell_id, cells_run = run_cell("y = 99")
assert cells_run - {cell_id} == {3}
def test_simple_reactive_var_store():
assert run_cell("x = 0")[1] == {1}
assert run_cell("y = x + 1")[1] == {2}
assert run_cell("logging.info(y)")[1] == {3}
assert run_cell("$x = 42")[1] == {4, 2}
cell_id, cells_run = run_cell("$y = 99")
assert cells_run - {cell_id} == {3}, "got %s" % cells_run
def test_simple_blocked_reactive_var_store():
assert run_cell("x = 0")[1] == {1}
assert run_cell("$:y = $x + 1")[1] == {2}
assert run_cell("logging.info($y)")[1] == {3}
assert run_cell("x = 42")[1] == {4, 2}
cell_id, cells_run = run_cell("z = 9001")
assert cells_run - {cell_id} == set(), "got %s" % (cells_run - {cell_id})
cell_id, cells_run = run_cell("y = 99")
assert cells_run - {cell_id} == {3}
def test_simple_blocked_reactive_var_load():
assert run_cell("x = 0")[1] == {1}
assert run_cell("y = $:x + 1")[1] == {2}
assert run_cell("logging.info(y)")[1] == {3}
assert run_cell("$x = 42")[1] == {4}
cell_id, cells_run = run_cell("$y = 99")
assert cells_run - {cell_id} == {3}, "got %s" % cells_run
def test_reactive_function_defn():
assert run_cell("x = 0")[1] == {1}
assert run_cell("def f(): return x")[1] == {2}
assert run_cell("logging.info(f())")[1] == {3}
assert run_cell("def $f(): return x + 3")[1] == {4, 3}
def test_reactive_function_call():
assert run_cell("x = 0")[1] == {1}
assert run_cell("def f(): return $x")[1] == {2}
assert run_cell("logging.info(f())")[1] == {3}
assert run_cell("x = 42")[1] == {4, 3}
def test_reactive_store_to_global_var_from_function_call():
assert run_cell("x = 0")[1] == {1}
assert run_cell("def f(): global x; $x = 42")[1] == {2}
assert run_cell("logging.info(x)")[1] == {3}
assert run_cell("f()")[1] == {4, 3}
def test_reactive_store_to_local_var_from_function_call():
assert run_cell("x = 0")[1] == {1}
assert run_cell("def f(): $x = 42")[1] == {2}
assert run_cell("logging.info(x)")[1] == {3}
assert run_cell("f()")[1] == {4}
def test_simple_cascading_reactive_load():
assert run_cell("x = 0")[1] == {1}
assert run_cell("y = $$x + 1")[1] == {2}
assert run_cell("logging.info(y)")[1] == {3}
assert run_cell("x = 42")[1] == {2, 3, 4}
def test_simple_cascading_reactive_store():
assert run_cell("x = 0")[1] == {1}
assert run_cell("y = x + 1")[1] == {2}
assert run_cell("logging.info(y)")[1] == {3}
assert run_cell("$$x = 42")[1] == {2, 3, 4}
assert run_cell("$x = 43")[1] == {2, 7}
def test_import_reactive_store():
assert (
run_cell(
"""
try:
logging.info(ast)
except:
pass
"""
)[1]
== {1}
)
assert run_cell("import $ast")[1] == {1, 2}
def test_cascading_import():
assert (
run_cell(
"""
try:
foo = ast.parse("bar")
except:
pass
"""
)[1]
== {1}
)
assert (
run_cell(
"""
try:
logging.info(foo.body)
except:
pass
"""
)[1]
== {2}
)
assert run_cell("import $$ast")[1] == {1, 2, 3}
def test_reactive_import_from():
assert (
run_cell(
"""
try:
foo = path.join("bar", "baz")
except:
pass
"""
)[1]
== {1}
)
assert run_cell("from os import $path")[1] == {1, 2}
def test_reactive_attr_load():
assert (
run_cell(
"""
from dataclasses import dataclass
@dataclass
class Example:
foo: str
bar: int
ex = Example('hi', 42)
"""
)[1]
== {1}
)
assert run_cell("logging.info($ex)")[1] == {2}
assert run_cell("logging.info(ex.$foo)")[1] == {3}
assert run_cell("logging.info(ex.$bar)")[1] == {4}
assert run_cell("logging.info($ex.foo)")[1] == {5}
assert run_cell("logging.info($ex.bar)")[1] == {6}
cell_id, cells_run = run_cell('ex.foo = "hello"')
assert cells_run - {cell_id} == {2, 3, 5, 6}
cell_id, cells_run = run_cell("ex.bar = 9001")
assert cells_run - {cell_id} == {2, 4, 5, 6}
cell_id, cells_run = run_cell('ex = Example("foo", 0)')
assert cells_run - {cell_id} == {2, 3, 4, 5, 6}
def test_reactive_attr_store():
assert (
run_cell(
"""
from dataclasses import dataclass
@dataclass
class Example:
foo: str
bar: int
ex = Example('hi', 42)
"""
)[1]
== {1}
)
assert run_cell("logging.info(ex)")[1] == {2}
assert run_cell("logging.info(ex.foo)")[1] == {3}
assert run_cell("logging.info(ex.bar)")[1] == {4}
assert run_cell("logging.info(ex.$foo)")[1] == {5}
assert run_cell("logging.info(ex.$bar)")[1] == {6}
cell_id, cells_run = run_cell('ex.$foo = "hello"')
assert cells_run - {cell_id} == {2, 3, 5}, "got %s" % (cells_run - {cell_id})
cell_id, cells_run = run_cell("ex.$bar = 9001")
assert cells_run - {cell_id} == {2, 4, 6}
cell_id, cells_run = run_cell('$ex.foo = "wat"')
assert cells_run - {cell_id} == {2, 3, 4, 5, 6}, "got %s" % (
cells_run - {cell_id}
)
cell_id, cells_run = run_cell('ex = Example("foo", 0)')
assert cells_run - {cell_id} == {5, 6}, "got %s" % (cells_run - {cell_id})
cell_id, cells_run = run_cell('$ex = Example("foo", 0)')
assert cells_run - {cell_id} == {2, 3, 4, 5, 6, 7, 11, 15}, "got %s" % (
cells_run - {cell_id}
)
def test_blocked_reactive_attr_store():
assert (
run_cell(
"""
from dataclasses import dataclass
@dataclass
class Example:
foo: str
bar: int
ex = Example('hi', 42)
"""
)[1]
== {1}
)
assert run_cell("logging.info($ex)")[1] == {2}
assert run_cell("logging.info(ex.$foo)")[1] == {3}
assert run_cell("logging.info(ex.$bar)")[1] == {4}
assert run_cell("logging.info($ex.foo)")[1] == {5}
assert run_cell("logging.info($ex.bar)")[1] == {6}
cell_id, cells_run = run_cell('ex.$:foo = "hello"')
assert cells_run - {cell_id} == set(), "got %s" % (cells_run - {cell_id})
cell_id, cells_run = run_cell("ex.$:bar = 9001")
assert cells_run - {cell_id} == set(), "got %s" % (cells_run - {cell_id})
cell_id, cells_run = run_cell('$:ex = Example("foo", 0)')
assert cells_run - {cell_id} == set(), "got %s" % (cells_run - {cell_id})
def test_blocked_reactive_attr_load():
assert (
run_cell(
"""
from dataclasses import dataclass
@dataclass
class Example:
foo: str
bar: int
ex = Example('hi', 42)
"""
)[1]
== {1}
)
assert run_cell("logging.info(ex)")[1] == {2}
assert run_cell("logging.info($:ex.foo)")[1] == {3}
assert run_cell("logging.info($:ex.bar)")[1] == {4}
assert run_cell("logging.info(ex.$:foo)")[1] == {5}
assert run_cell("logging.info(ex.$:bar)")[1] == {6}
cell_id, cells_run = run_cell('ex.$foo = "hello"')
assert cells_run - {cell_id} == {2}, "got %s" % (cells_run - {cell_id})
cell_id, cells_run = run_cell("ex.$bar = 9001")
assert cells_run - {cell_id} == {2}
cell_id, cells_run = run_cell('$:ex.foo = "wat"')
assert cells_run - {cell_id} == set(), "got %s" % (cells_run - {cell_id})
cell_id, cells_run = run_cell('ex = Example("foo", 0)')
assert cells_run - {cell_id} == set()
cell_id, cells_run = run_cell('$ex = Example("foo", 0)')
assert cells_run - {cell_id} == {2, 5, 6, 7, 9}, "got %s" % (
cells_run - {cell_id}
)
def test_store_after_blocked_store_reactively_executes():
assert run_cell("x = 0")[1] == {1}
assert run_cell("logging.info(x)")[1] == {2}
assert run_cell("$:x = 42")[1] == {3}
assert run_cell("$x = 42")[1] == {4, 2}
def test_nested_reactive_references():
assert run_cell("x = 42")[1] == {1}
assert (
run_cell(
"""
def assert_nonzero(v):
assert v != 0, "Got 0 for v!"
"""
)[1]
== {2}
)
assert run_cell("$assert_nonzero($x)")[1] == {3}
assert (
run_cell(
"""
def assert_nonzero(v):
assert v != 0, "v can't be 0"
"""
)[1]
== {3, 4}
)
rerun = run_cell("x = 43")[1]
assert rerun == {3, 6}, "got %s" % rerun
def test_nested_reactive_references_2():
assert run_cell("x = 42")[1] == {1}
assert (
run_cell(
"""
def assert_nonzero(v):
assert v != 0, "Got 0 for v!"
"""
)[1]
== {2}
)
assert run_cell("assert_nonzero($x)")[1] == {3}
assert (
run_cell(
"""
def $assert_nonzero(v):
assert v != 0, "v can't be 0"
"""
)[1]
== {3, 4}
)
rerun = run_cell("x = 43")[1]
assert rerun == {3, 6}, "got %s" % rerun
def test_namedexpr_reactive_store():
assert run_cell("x = 0")[1] == {1}
assert run_cell("y = x + 1")[1] == {2}
assert run_cell("if ($x := 1): pass")[1] == {2, 3}
def test_cascading_list_elt():
assert run_cell("x = 0")[1] == {1}
assert run_cell("lst = [$$x]")[1] == {2}
assert run_cell("logging.info(lst)")[1] == {3}
assert run_cell("x = 42")[1] == {2, 3, 4}
def test_nonlocal_reactive_ref():
assert (
run_cell(
"""
def foo():
x = 0
def bar():
return $x + 1
def baz(y):
nonlocal x
x = y
return bar, baz
bar, baz = foo()
"""
)[1]
== {1}
)
assert run_cell("logging.info(bar())")[1] == {2}
rerun = run_cell("baz(42)")[1]
assert rerun == {2, 3}, "got %s" % rerun
def test_cascading_nesting():
assert run_cell("matrix = [['f'] * 3 for _ in range(3)]")[1] == {1}
assert run_cell("x = matrix[0][0]")[1] == {2}
assert run_cell("x")[1] == {3}
rerun = run_cell("$$matrix = [['g'] * 3 for _ in range(3)]")[1]
assert rerun == {2, 3, 4}, "got %s" % rerun