示例#1
0
    def test__hinted_value_with_hint_ColumnsStreamed(self) -> None:
        from bokeh.document.events import ColumnsStreamedEvent

        prop = bcpc.ColumnData(String, Seq(Any))
        source = ColumnDataSource(data=dict(foo=[10], bar=[20], baz=[30]))
        new_data = dict(foo=[11], bar=[21], baz=[31])
        hint = ColumnsStreamedEvent("doc", source, "data", new_data, rollover=10)
        assert prop._hinted_value(source.data, hint) == new_data
示例#2
0
    def test_patch_event_contains_setter(self) -> None:
        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': np.array([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 = ModelChangedEvent(sample, root, 'child', new_child)
        msg = proto.create("PATCH-DOC", [event])
        msg.apply_to_document(sample, mock_session)
        assert msg.buffers == []

        # RootAdded
        event2 = RootAddedEvent(sample, root)
        msg2 = proto.create("PATCH-DOC", [event2])
        msg2.apply_to_document(sample, mock_session)
        assert msg2.buffers == []

        # RootRemoved
        event3 = RootRemovedEvent(sample, root)
        msg3 = proto.create("PATCH-DOC", [event3])
        msg3.apply_to_document(sample, mock_session)
        assert msg3.buffers == []

        # ColumnsStreamed
        event4 = ColumnsStreamedEvent(sample, cds, "data", {"a": [3]}, None, mock_session)
        msg4 = proto.create("PATCH-DOC", [event4])
        msg4.apply_to_document(sample, mock_session)
        assert msg4.buffers == []

        # ColumnsPatched
        event5 = ColumnsPatchedEvent(sample, cds, "data", {"a": [(0, 11)]})
        msg5 = proto.create("PATCH-DOC", [event5])
        msg5.apply_to_document(sample, mock_session)
        assert msg5.buffers == []

        # ColumnDataChanged
        event7 = ColumnDataChangedEvent(sample, cds, "data")
        msg7 = proto.create("PATCH-DOC", [event7])
        msg7.apply_to_document(sample, mock_session)
        assert len(msg7.buffers) == 1

        # reports CDS buffer *as it is* Normally events called by setter and
        # value in local object would have been already mutated.
        [buf] = msg7.buffers
        assert bytes(buf.data) == np.array([11., 1., 2., 3]).tobytes()
示例#3
0
    def test_patch_event_contains_setter(self) -> None:
        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': np.array([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 = ModelChangedEvent(sample, root, 'child', root.child, new_child,
                                  new_child)
        msg = proto.create("PATCH-DOC", [event])
        msg.apply_to_document(sample, mock_session)
        assert msg.buffers == []

        # RootAdded
        event2 = RootAddedEvent(sample, root)
        msg2 = proto.create("PATCH-DOC", [event2])
        msg2.apply_to_document(sample, mock_session)
        assert msg2.buffers == []

        # RootRemoved
        event3 = RootRemovedEvent(sample, root)
        msg3 = proto.create("PATCH-DOC", [event3])
        msg3.apply_to_document(sample, mock_session)
        assert msg3.buffers == []

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

        # ColumnsPatched
        event5 = ModelChangedEvent(sample,
                                   cds,
                                   'data',
                                   10,
                                   None,
                                   None,
                                   hint=ColumnsPatchedEvent(
                                       sample, cds, {"a": [(0, 11)]}))
        msg5 = proto.create("PATCH-DOC", [event5])
        msg5.apply_to_document(sample, mock_session)
        assert msg5.buffers == []

        # ColumnDataChanged, use_buffers=False
        event6 = ModelChangedEvent(sample,
                                   cds,
                                   'data', {'a': np.array([0., 1.])},
                                   None,
                                   None,
                                   hint=ColumnDataChangedEvent(sample, cds))
        msg6 = proto.create("PATCH-DOC", [event6], use_buffers=False)
        msg6.apply_to_document(sample, mock_session)
        assert msg6.buffers == []

        print(cds.data)
        # ColumnDataChanged, use_buffers=True
        event7 = ModelChangedEvent(sample,
                                   cds,
                                   'data', {'a': np.array([0., 1.])},
                                   None,
                                   None,
                                   hint=ColumnDataChangedEvent(sample, cds))
        msg7 = proto.create("PATCH-DOC", [event7])
        # can't test apply, doc not set up to *receive* binary buffers
        # msg7.apply_to_document(sample, mock_session)
        assert len(msg7.buffers) == 1
        buf = msg7.buffers.pop()
        assert len(buf) == 2
        assert isinstance(buf[0], dict)
        assert list(buf[0]) == ['id']

        # reports CDS buffer *as it is* Normally events called by setter and
        # value in local object would have been already mutated.
        assert buf[1] == np.array([11., 1., 2., 3]).tobytes()
示例#4
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 = 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 = RootAddedEvent(sample, root)
        msg2 = Protocol("1.0").create("PATCH-DOC", [event2])
        msg2.apply_to_document(sample, mock_session)

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

        # ColumnsStreamed
        event4 = ModelChangedEvent(sample,
                                   cds,
                                   'data',
                                   10,
                                   None,
                                   None,
                                   hint=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 = ModelChangedEvent(sample,
                                   cds,
                                   'data',
                                   10,
                                   None,
                                   None,
                                   hint=ColumnsPatchedEvent(
                                       sample, cds, {"a": [(0, 11)]}))
        msg5 = Protocol("1.0").create("PATCH-DOC", [event5])
        msg5.apply_to_document(sample, mock_session)