Skip to content

Commit

Permalink
Test both echo and not echo state
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongrout committed Mar 2, 2022
1 parent 6f135eb commit 202ae1e
Show file tree
Hide file tree
Showing 2 changed files with 427 additions and 153 deletions.
182 changes: 29 additions & 153 deletions ipywidgets/widgets/tests/test_set_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@

from .utils import setup, teardown

from ..widget import Widget
import ipywidgets
from ipywidgets import Widget

# Everything in this file assumes echo is false
@pytest.fixture(autouse=True)
def echo():
oldvalue = ipywidgets.widgets.widget.JUPYTER_WIDGETS_ECHO
ipywidgets.widgets.widget.JUPYTER_WIDGETS_ECHO = False
yield
ipywidgets.widgets.widget.JUPYTER_WIDGETS_ECHO = oldvalue

#
# First some widgets to test on:
#

# A widget with simple traits (list + tuple to ensure both are handled)
class SimpleWidget(Widget):
Expand Down Expand Up @@ -86,7 +92,7 @@ def test_set_state_simple():
c=[False, True, False],
))

assert len(w.comm.messages) == 1
assert w.comm.messages == []


def test_set_state_transformer():
Expand All @@ -96,18 +102,11 @@ def test_set_state_transformer():
))
# Since the deserialize step changes the state, this should send an update
assert w.comm.messages == [((), dict(
buffers=[],
data=dict(
buffer_paths=[],
method='echo_update',
state=dict(d=[True, False, True]),
))),
((), dict(
buffers=[],
data=dict(
buffer_paths=[],
method='update',
state=dict(d=[False, True, False]),
state=dict(d=[False, True, False])
)))]


Expand All @@ -118,7 +117,7 @@ def test_set_state_data():
a=True,
d={'data': data},
))
assert len(w.comm.messages) == 1
assert w.comm.messages == []


def test_set_state_data_truncate():
Expand All @@ -129,15 +128,15 @@ def test_set_state_data_truncate():
d={'data': data},
))
# Get message for checking
assert len(w.comm.messages) == 2 # ensure we didn't get more than expected
msg = w.comm.messages[1]
assert len(w.comm.messages) == 1 # ensure we didn't get more than expected
msg = w.comm.messages[0]
# Assert that the data update (truncation) sends an update
buffers = msg[1].pop('buffers')
assert msg == ((), dict(
data=dict(
buffer_paths=[['d', 'data']],
method='update',
state=dict(d={}),
buffer_paths=[['d', 'data']]
state=dict(d={})
)))

# Sanity:
Expand All @@ -157,8 +156,8 @@ def test_set_state_numbers_int():
i = 3,
ci = 4,
))
# Ensure one update message gets produced
assert len(w.comm.messages) == 1
# Ensure no update message gets produced
assert len(w.comm.messages) == 0


def test_set_state_numbers_float():
Expand All @@ -169,8 +168,8 @@ def test_set_state_numbers_float():
cf = 2.0,
ci = 4.0
))
# Ensure one update message gets produced
assert len(w.comm.messages) == 1
# Ensure no update message gets produced
assert len(w.comm.messages) == 0


def test_set_state_float_to_float():
Expand All @@ -180,8 +179,8 @@ def test_set_state_float_to_float():
f = 1.2,
cf = 2.6,
))
# Ensure one message gets produced
assert len(w.comm.messages) == 1
# Ensure no update message gets produced
assert len(w.comm.messages) == 0


def test_set_state_cint_to_float():
Expand All @@ -192,8 +191,8 @@ def test_set_state_cint_to_float():
ci = 5.6
))
# Ensure an update message gets produced
assert len(w.comm.messages) == 2
msg = w.comm.messages[1]
assert len(w.comm.messages) == 1
msg = w.comm.messages[0]
data = msg[1]['data']
assert data['method'] == 'update'
assert data['state'] == {'ci': 5}
Expand Down Expand Up @@ -248,136 +247,13 @@ def _propagate_value(self, change):
# this mimics a value coming from the front end
widget.set_state({'value': 42})
assert widget.value == 42
assert widget.stop is True

# we expect no new state to be sent
calls = []
widget._send.assert_has_calls(calls)


def test_hold_sync():
# when this widget's value is set to 42, it sets the value to 2, and also sets a different trait value
class AnnoyingWidget(Widget):
value = Float().tag(sync=True)
other = Float().tag(sync=True)

@observe('value')
def _propagate_value(self, change):
print('_propagate_value', change.new)
if change.new == 42:
with self.hold_sync():
self.value = 2
self.other = 11

widget = AnnoyingWidget(value=1)
assert widget.value == 1

widget._send = mock.MagicMock()
# this mimics a value coming from the front end
widget.set_state({'value': 42})
assert widget.value == 2
assert widget.other == 11

msg = {'method': 'echo_update', 'state': {'value': 42.0}, 'buffer_paths': []}
call42 = mock.call(msg, buffers=[])

msg = {'method': 'update', 'state': {'value': 2.0, 'other': 11.0}, 'buffer_paths': []}
# we expect first the {'value': 2.0} state to be send, followed by the {'value': 42.0} state
msg = {'method': 'update', 'state': {'value': 2.0}, 'buffer_paths': []}
call2 = mock.call(msg, buffers=[])

calls = [call42, call2]
widget._send.assert_has_calls(calls)


def test_echo():
# we always echo values back to the frontend
class ValueWidget(Widget):
value = Float().tag(sync=True)

widget = ValueWidget(value=1)
assert widget.value == 1

widget._send = mock.MagicMock()
# this mimics a value coming from the front end
widget.set_state({'value': 42})
assert widget.value == 42

# we expect this to be echoed
msg = {'method': 'echo_update', 'state': {'value': 42.0}, 'buffer_paths': []}
msg = {'method': 'update', 'state': {'value': 42.0}, 'buffer_paths': []}
call42 = mock.call(msg, buffers=[])

calls = [call42]
widget._send.assert_has_calls(calls)



def test_echo_single():
# we always echo multiple changes back in 1 update
class ValueWidget(Widget):
value = Float().tag(sync=True)
square = Float().tag(sync=True)
@observe('value')
def _square(self, change):
self.square = self.value**2

widget = ValueWidget(value=1)
assert widget.value == 1

widget._send = mock.MagicMock()
# this mimics a value coming from the front end
widget._handle_msg({
'content': {
'data': {
'method': 'update',
'state': {
'value': 8,
}
}
}
})
assert widget.value == 8
assert widget.square == 64

# we expect this to be echoed
# note that only value is echoed, not square
msg = {'method': 'echo_update', 'state': {'value': 8.0}, 'buffer_paths': []}
call = mock.call(msg, buffers=[])

msg = {'method': 'update', 'state': {'square': 64}, 'buffer_paths': []}
call2 = mock.call(msg, buffers=[])


calls = [call, call2]
calls = [call2, call42]
widget._send.assert_has_calls(calls)


def test_no_echo():
# in cases where values coming from the frontend are 'heavy', we might want to opt out
class ValueWidget(Widget):
value = Float().tag(sync=True, echo_update=False)

widget = ValueWidget(value=1)
assert widget.value == 1

widget._send = mock.MagicMock()
# this mimics a value coming from the front end
widget._handle_msg({
'content': {
'data': {
'method': 'update',
'state': {
'value': 42,
}
}
}
})
assert widget.value == 42

# widget._send.assert_not_called(calls)
widget._send.assert_not_called()

# a regular set should sync to the frontend
widget.value = 43
widget._send.assert_has_calls([mock.call({'method': 'update', 'state': {'value': 43.0}, 'buffer_paths': []}, buffers=[])])



Loading

0 comments on commit 202ae1e

Please sign in to comment.