示例#1
0
 def test_create_then_parse(self):
     sample = self._sample_doc()
     msg = Protocol("1.0").create("PUSH-DOC", sample)
     copy = document.Document()
     msg.push_to_document(copy)
     assert len(sample.roots) == 2
     assert len(copy.roots) == 2
示例#2
0
 def test_create_reply_then_parse(self):
     sample = self._sample_doc()
     msg = Protocol("1.0").create("PULL-DOC-REPLY", 'fakereqid', sample)
     copy = document.Document()
     msg.push_to_document(copy)
     assert len(sample.roots) == 2
     assert len(copy.roots) == 2
示例#3
0
 def __init__(self, session, websocket_url, io_loop=None):
     '''
       Opens a websocket connection to the server.
     '''
     self._url = websocket_url
     self._session = session
     self._protocol = Protocol("1.0")
     self._receiver = Receiver(self._protocol)
     self._socket = None
     self._state = self.NOT_YET_CONNECTED()
     if io_loop is None:
         # We can't use IOLoop.current because then we break
         # when running inside a notebook since ipython also uses it
         io_loop = IOLoop()
     self._loop = io_loop
     self._until_predicate = None
     self._protocol = Protocol("1.0")
     self._server_info = None
示例#4
0
    def test_should_suppress_model_changed(self):
        sample = self._sample_doc()
        root = None
        other_root = None
        for r in sample.roots:
            if r.child is not None:
                root = r
            else:
                other_root = r
        assert root is not None
        assert other_root is not None
        new_child = AnotherModelInTestPatchDoc(bar=56)

        # integer property changed
        event1 = document.ModelChangedEvent(sample, root, 'foo', root.foo, 42)
        msg = Protocol("1.0").create("PATCH-DOC", [event1])
        assert msg.should_suppress_on_change(event1)
        assert not msg.should_suppress_on_change(document.ModelChangedEvent(sample, root, 'foo', root.foo, 43))
        assert not msg.should_suppress_on_change(document.ModelChangedEvent(sample, root, 'bar', root.foo, 43))
        assert not msg.should_suppress_on_change(document.ModelChangedEvent(sample, other_root, 'foo', root.foo, 43))

        # Model property changed
        event2 = document.ModelChangedEvent(sample, root, 'child', root.child, new_child)
        msg2 = Protocol("1.0").create("PATCH-DOC", [event2])
        assert msg2.should_suppress_on_change(event2)
        assert not msg2.should_suppress_on_change(document.ModelChangedEvent(sample, root, 'child', root.child, other_root))
        assert not msg2.should_suppress_on_change(document.ModelChangedEvent(sample, root, 'blah', root.child, new_child))
        assert not msg2.should_suppress_on_change(document.ModelChangedEvent(sample, other_root, 'child', other_root.child, new_child))

        # Model property changed to None
        event3 = document.ModelChangedEvent(sample, root, 'child', root.child, None)
        msg3 = Protocol("1.0").create("PATCH-DOC", [event3])
        assert msg3.should_suppress_on_change(event3)
        assert not msg3.should_suppress_on_change(document.ModelChangedEvent(sample, root, 'child', root.child, other_root))
        assert not msg3.should_suppress_on_change(document.ModelChangedEvent(sample, root, 'blah', root.child, None))
        assert not msg3.should_suppress_on_change(document.ModelChangedEvent(sample, other_root, 'child', other_root.child, None))

        # Model property changed from None
        event4 = document.ModelChangedEvent(sample, other_root, 'child', other_root.child, None)
        msg4 = Protocol("1.0").create("PATCH-DOC", [event4])
        assert msg4.should_suppress_on_change(event4)
        assert not msg4.should_suppress_on_change(document.ModelChangedEvent(sample, other_root, 'child', other_root.child, root))
        assert not msg4.should_suppress_on_change(document.ModelChangedEvent(sample, other_root, 'blah', other_root.child, None))
        assert not msg4.should_suppress_on_change(document.ModelChangedEvent(sample, root, 'child', other_root.child, None))

        # RootAdded
        event5 = document.RootAddedEvent(sample, root)
        msg5 = Protocol("1.0").create("PATCH-DOC", [event5])
        assert msg5.should_suppress_on_change(event5)
        assert not msg5.should_suppress_on_change(document.RootAddedEvent(sample, other_root))
        assert not msg5.should_suppress_on_change(document.RootRemovedEvent(sample, root))

        # RootRemoved
        event6 = document.RootRemovedEvent(sample, root)
        msg6 = Protocol("1.0").create("PATCH-DOC", [event6])
        assert msg6.should_suppress_on_change(event6)
        assert not msg6.should_suppress_on_change(document.RootRemovedEvent(sample, other_root))
        assert not msg6.should_suppress_on_change(document.RootAddedEvent(sample, root))
示例#5
0
    def test_create_then_apply_model_changed(self):
        sample = self._sample_doc()

        foos = []
        for r in sample.roots:
            foos.append(r.foo)
        assert foos == [ 2, 2 ]

        obj = next(iter(sample.roots))
        assert obj.foo == 2
        event = document.ModelChangedEvent(sample, obj, 'foo', obj.foo, 42, 42)
        msg = Protocol("1.0").create("PATCH-DOC", [event])

        copy = document.Document.from_json_string(sample.to_json_string())
        msg.apply_to_document(copy)

        foos = []
        for r in copy.roots:
            foos.append(r.foo)
        foos.sort()
        assert foos == [ 2, 42 ]
示例#6
0
 def test_create_model_changed(self):
     sample = self._sample_doc()
     obj = next(iter(sample.roots))
     event = document.ModelChangedEvent(sample, obj, 'foo', obj.foo, 42, 42)
     Protocol("1.0").create("PATCH-DOC", [event])
示例#7
0
    def test_patch_event_contains_setter(self):
        sample = self._sample_doc()
        root = None
        other_root = None
        for r in sample.roots:
            if r.child is not None:
                root = r
            else:
                other_root = r
        assert root is not None
        assert other_root is not None
        new_child = AnotherModelInTestPatchDoc(bar=56)

        cds = ColumnDataSource(data={'a': [0, 1, 2]})
        sample.add_root(cds)

        mock_session = object()

        def sample_document_callback_assert(event):
            """Asserts that setter is correctly set on event"""
            assert event.setter is mock_session

        sample.on_change(sample_document_callback_assert)

        # Model property changed
        event = document.ModelChangedEvent(sample, root, 'child', root.child,
                                           new_child, new_child)
        msg = Protocol("1.0").create("PATCH-DOC", [event])
        msg.apply_to_document(sample, mock_session)

        # RootAdded
        event2 = document.RootAddedEvent(sample, root)
        msg2 = Protocol("1.0").create("PATCH-DOC", [event2])
        msg2.apply_to_document(sample, mock_session)

        # RootRemoved
        event3 = document.RootRemovedEvent(sample, root)
        msg3 = Protocol("1.0").create("PATCH-DOC", [event3])
        msg3.apply_to_document(sample, mock_session)

        # ColumnsStreamed
        event4 = document.ModelChangedEvent(sample,
                                            cds,
                                            'data',
                                            10,
                                            None,
                                            None,
                                            hint=document.ColumnsStreamedEvent(
                                                sample, cds, {"a": [3]}, None,
                                                mock_session))
        msg4 = Protocol("1.0").create("PATCH-DOC", [event4])
        msg4.apply_to_document(sample, mock_session)

        # ColumnsPatched
        event5 = document.ModelChangedEvent(sample,
                                            cds,
                                            'data',
                                            10,
                                            None,
                                            None,
                                            hint=document.ColumnsPatchedEvent(
                                                sample, cds, {"a": [(0, 11)]}))
        msg5 = Protocol("1.0").create("PATCH-DOC", [event5])
        msg5.apply_to_document(sample, mock_session)
示例#8
0
 def test_create(self):
     sample = self._sample_doc()
     Protocol("1.0").create("PUSH-DOC", sample)
示例#9
0
 def test_create_reply(self):
     sample = self._sample_doc()
     Protocol("1.0").create("PULL-DOC-REPLY", 'fakereqid', sample)
示例#10
0
 def test_create_req(self):
     Protocol("1.0").create("PULL-DOC-REQ")
示例#11
0
from __future__ import absolute_import

from bokeh.server.protocol import receiver, Protocol
from bokeh.util.string import decode_utf8

_proto = Protocol("1.0")


def test_creation():
    receiver.Receiver(None)


def test_validation_success():
    msg = _proto.create('ACK')
    r = receiver.Receiver(_proto)

    partial = r.consume(decode_utf8(msg.header_json)).result()
    assert partial is None

    partial = r.consume(decode_utf8(msg.metadata_json)).result()
    assert partial is None

    partial = r.consume(decode_utf8(msg.content_json)).result()
    assert partial is not None
    assert partial.msgtype == msg.msgtype
    assert partial.header == msg.header
    assert partial.content == msg.content
    assert partial.metadata == msg.metadata