/
testwidgets.py
66 lines (53 loc) · 2.54 KB
/
testwidgets.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
"""
Test cases for the HTML/JavaScript scrubber and widgets.
"""
import re
from hashlib import sha256
from unittest import SkipTest
import numpy as np
try:
from holoviews.ipython import IPTestCase
from holoviews.plotting.mpl.widgets import ScrubberWidget, SelectionWidget
# Standardize backend due to random inconsistencies
from matplotlib import pyplot
pyplot.switch_backend('agg')
except:
raise SkipTest("Matplotlib required to test widgets")
from holoviews import Image, HoloMap
from holoviews.plotting.mpl import RasterPlot
def digest_data(data):
hashfn = sha256()
hashfn.update(data.encode('utf-16'))
return hashfn.hexdigest()
prefixes = ['anim', '_anim_slider', '_anim_img',
'_anim_loop_select', 'textInput', '_anim_widget', 'valMap']
filters = [re.compile('{p}[a-f0-9]+'.format(p=p)) for p in prefixes]
filters += [re.compile('new ScrubberWidget\([a-z0-9_, "]+')]
filters += [re.compile('new SelectionWidget\([a-z0-9_, "]+')]
def normalize(data):
for f in filters:
data = re.sub(f, '[CLEARED]', data)
# Hack around inconsistencies in jinja between Python 2 and 3
return data.replace('0.0', '0').replace('1.0', '1')
class TestWidgets(IPTestCase):
def setUp(self):
super(TestWidgets, self).setUp()
im1 = Image(np.array([[1,2],[3,4]]))
im2 = Image(np.array([[1,2],[3,5]]))
holomap = HoloMap(initial_items=[(0,im1), (1,im2)], kdims=['test'])
self.plot1 = RasterPlot(im1)
self.plot2 = RasterPlot(holomap)
def tearDown(self):
super(TestWidgets, self).tearDown()
def test_scrubber_widget_1(self):
html = normalize(ScrubberWidget(self.plot1, display_options={'figure_format': 'png'})())
self.assertEqual(digest_data(html), 'ca9778db5ef562c045a94b2664704882f4c66fc1ef29a48623fe0e9b73f27b8d')
def test_scrubber_widget_2(self):
html = normalize(ScrubberWidget(self.plot2, display_options={'figure_format': 'png'})())
self.assertEqual(digest_data(html), '764c0636eb762b6970287b2727b54af3621ff55d61faf45c896aecfe73fae90b')
def test_selection_widget_1(self):
html = normalize(SelectionWidget(self.plot1, display_options={'figure_format': 'png'})())
self.assertEqual(digest_data(html), '11cc21191dd5572dc25187a8167a3ecc46dcbe868077333fb3843d9ae471cd5d')
def test_selection_widget_2(self):
html = normalize(SelectionWidget(self.plot2, display_options={'figure_format': 'png'})())
self.assertEqual(digest_data(html), '08900ab3119f3d23d4e0185eb856894192754dcc02809682ecb8b742571cdcb6')