Skip to content

Commit

Permalink
Reorganized callback tests
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Oct 1, 2018
1 parent adc17f8 commit 9738858
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 129 deletions.
112 changes: 77 additions & 35 deletions tests/plotting/bokeh/testcallbacks.py
@@ -1,9 +1,8 @@
from collections import deque, namedtuple
from unittest import SkipTest

import numpy as np

from holoviews.core import DynamicMap
from holoviews.core import DynamicMap, NdOverlay
from holoviews.core.options import Store
from holoviews.element import Points, Polygons, Box, Curve, Table
from holoviews.element.comparison import ComparisonTestCase
Expand All @@ -13,32 +12,37 @@
import pyviz_comms as comms

try:
from bokeh.models import PolyEditTool
from bokeh.events import Tap
from bokeh.models import Range1d, Plot, ColumnDataSource, Selection, PolyEditTool
from holoviews.plotting.bokeh.callbacks import (
Callback, PointDrawCallback, PolyDrawCallback, PolyEditCallback,
BoxEditCallback
BoxEditCallback, Selection1DCallback
)
from holoviews.plotting.bokeh.renderer import BokehRenderer
from holoviews.plotting.bokeh.util import bokeh_version
bokeh_server_renderer = BokehRenderer.instance(mode='server')
bokeh_renderer = BokehRenderer.instance()
except:
bokeh_renderer = None
bokeh_server_renderer = None



class TestCallbacks(ComparisonTestCase):
class CallbackTestCase(ComparisonTestCase):

def setUp(self):
self.previous_backend = Store.current_backend
Store.current_backend = 'bokeh'
self.comm_manager = bokeh_renderer.comm_manager
bokeh_renderer.comm_manager = comms.CommManager

def tearDown(self):
Store.current_backend = self.previous_backend
bokeh_server_renderer.last_plot = None
bokeh_renderer.last_plot = None
Callback._callbacks = {}
bokeh_renderer.comm_manager = self.comm_manager


class TestCallbacks(CallbackTestCase):

def test_stream_callback(self):
dmap = DynamicMap(lambda x, y: Points([(x, y)]), kdims=[], streams=[PointerXY()])
Expand Down Expand Up @@ -85,13 +89,7 @@ def test_callback_cleanup(self):
self.assertFalse(bool(Callback._callbacks))


class TestResetCallback(ComparisonTestCase):

def setUp(self):
self.previous_backend = Store.current_backend
Store.current_backend = 'bokeh'
self.comm_manager = bokeh_renderer.comm_manager
bokeh_renderer.comm_manager = comms.CommManager
class TestResetCallback(CallbackTestCase):

def test_reset_callback(self):
resets = []
Expand All @@ -104,22 +102,7 @@ def record(resetting):
self.assertEqual(resets, [True])


class TestEditToolCallbacks(ComparisonTestCase):

def setUp(self):
self.previous_backend = Store.current_backend
if not bokeh_server_renderer or bokeh_version < '0.12.14':
raise SkipTest("Bokeh >= 0.12.14 required to test edit tool streams")
Store.current_backend = 'bokeh'
self.comm_manager = bokeh_renderer.comm_manager
bokeh_renderer.comm_manager = comms.CommManager

def tearDown(self):
Store.current_backend = self.previous_backend
bokeh_server_renderer.last_plot = None
bokeh_renderer.last_plot = None
Callback._callbacks = {}
bokeh_renderer.comm_manager = self.comm_manager
class TestEditToolCallbacks(CallbackTestCase):

def test_point_draw_callback(self):
points = Points([(0, 1)])
Expand Down Expand Up @@ -298,18 +281,36 @@ def test_point_draw_shared_datasource_callback(self):



class TestServerCallbacks(ComparisonTestCase):
class TestServerCallbacks(CallbackTestCase):

def setUp(self):
self.previous_backend = Store.current_backend
Store.current_backend = 'bokeh'
def test_server_callback_resolve_attr_spec_range1d_start(self):
range1d = Range1d(start=0, end=10)
msg = Callback.resolve_attr_spec('x_range.attributes.start', range1d)
self.assertEqual(msg, {'id': range1d.ref['id'], 'value': 0})

def test_server_callback_resolve_attr_spec_range1d_end(self):
range1d = Range1d(start=0, end=10)
msg = Callback.resolve_attr_spec('x_range.attributes.end', range1d)
self.assertEqual(msg, {'id': range1d.ref['id'], 'value': 10})

def test_server_callback_resolve_attr_spec_source_selected(self):
source = ColumnDataSource()
source.selected = Selection(indices=[1, 2, 3])
msg = Callback.resolve_attr_spec('cb_obj.selected.indices', source)
self.assertEqual(msg, {'id': source.ref['id'], 'value': [1, 2, 3]})

def test_server_callback_resolve_attr_spec_tap_event(self):
plot = Plot()
event = Tap(plot, x=42)
msg = Callback.resolve_attr_spec('cb_obj.x', event, plot)
self.assertEqual(msg, {'id': plot.ref['id'], 'value': 42})

def test_selection1d_resolves(self):
points = Points([1, 2, 3])
Selection1D(source=points)
plot = bokeh_server_renderer.get_plot(points)
cds = plot.handles['cds']
cds.selected.indices = [0, 2]
cds.selected = Selection(indices=[0, 2])
callback = plot.callbacks[0]
spec = callback.attributes['index']
resolved = callback.resolve_attr_spec(spec, cds, model=cds)
Expand Down Expand Up @@ -359,3 +360,44 @@ def test_cds_resolves(self):
self.assertEqual(resolved, {'id': cds.ref['id'],
'value': points.columns()})




class TestBokehCustomJSCallbacks(CallbackTestCase):

def test_customjs_callback_attributes_js_for_model(self):
js_code = Callback.attributes_js({'x0': 'x_range.attributes.start',
'x1': 'x_range.attributes.end'})

code = (
'if ((x_range != undefined)) { data["x0"] = {id: x_range["id"], value: '
'x_range["attributes"]["start"]};\n }'
'if ((x_range != undefined)) { data["x1"] = {id: x_range["id"], value: '
'x_range["attributes"]["end"]};\n }'
)
self.assertEqual(js_code, code)

def test_customjs_callback_attributes_js_for_cb_obj(self):
js_code = Callback.attributes_js({'x': 'cb_obj.x',
'y': 'cb_obj.y'})
code = 'data["x"] = cb_obj["x"];\ndata["y"] = cb_obj["y"];\n'
self.assertEqual(js_code, code)

def test_customjs_callback_attributes_js_for_cb_data(self):
js_code = Callback.attributes_js({'x0': 'cb_data.geometry.x0',
'x1': 'cb_data.geometry.x1',
'y0': 'cb_data.geometry.y0',
'y1': 'cb_data.geometry.y1'})
code = ('data["x0"] = cb_data["geometry"]["x0"];\n'
'data["x1"] = cb_data["geometry"]["x1"];\n'
'data["y0"] = cb_data["geometry"]["y0"];\n'
'data["y1"] = cb_data["geometry"]["y1"];\n')
self.assertEqual(js_code, code)

def test_callback_on_ndoverlay_is_attached(self):
ndoverlay = NdOverlay({i: Curve([i]) for i in range(5)})
selection = Selection1D(source=ndoverlay)
plot = bokeh_renderer.get_plot(ndoverlay)
self.assertEqual(len(plot.callbacks), 1)
self.assertIsInstance(plot.callbacks[0], Selection1DCallback)
self.assertIn(selection, plot.callbacks[0].streams)
94 changes: 0 additions & 94 deletions tests/testbokehcallbacks.py

This file was deleted.

0 comments on commit 9738858

Please sign in to comment.