Skip to content

Commit

Permalink
Added unit tests for formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Oct 5, 2018
1 parent 051199a commit 6441ccf
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 3 deletions.
48 changes: 46 additions & 2 deletions tests/plotting/bokeh/testelementplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

try:
from bokeh.document import Document
from bokeh.models import FuncTickFormatter
from bokeh.models import FuncTickFormatter, PrintfTickFormatter, NumeralTickFormatter
except:
pass

Expand All @@ -24,6 +24,50 @@ def test_element_show_frame_disabled(self):
plot = bokeh_renderer.get_plot(curve).state
self.assertEqual(plot.outline_line_alpha, 0)

def test_element_xformatter_string(self):
curve = Curve(range(10)).options(xformatter='%d')
plot = bokeh_renderer.get_plot(curve)
xaxis = plot.handles['xaxis']
self.assertIsInstance(xaxis.formatter, PrintfTickFormatter)
self.assertEqual(xaxis.formatter.format, '%d')

def test_element_yformatter_string(self):
curve = Curve(range(10)).options(yformatter='%d')
plot = bokeh_renderer.get_plot(curve)
yaxis = plot.handles['yaxis']
self.assertIsInstance(yaxis.formatter, PrintfTickFormatter)
self.assertEqual(yaxis.formatter.format, '%d')

def test_element_xformatter_function(self):
def formatter(value):
return str(value) + ' %'
curve = Curve(range(10)).options(xformatter=formatter)
plot = bokeh_renderer.get_plot(curve)
xaxis = plot.handles['xaxis']
self.assertIsInstance(xaxis.formatter, FuncTickFormatter)

def test_element_yformatter_function(self):
def formatter(value):
return str(value) + ' %'
curve = Curve(range(10)).options(yformatter=formatter)
plot = bokeh_renderer.get_plot(curve)
yaxis = plot.handles['yaxis']
self.assertIsInstance(yaxis.formatter, FuncTickFormatter)

def test_element_xformatter_instance(self):
formatter = NumeralTickFormatter()
curve = Curve(range(10)).options(xformatter=formatter)
plot = bokeh_renderer.get_plot(curve)
xaxis = plot.handles['xaxis']
self.assertIs(xaxis.formatter, formatter)

def test_element_yformatter_instance(self):
formatter = NumeralTickFormatter()
curve = Curve(range(10)).options(yformatter=formatter)
plot = bokeh_renderer.get_plot(curve)
yaxis = plot.handles['yaxis']
self.assertIs(yaxis.formatter, formatter)

def test_empty_element_visibility(self):
curve = Curve([])
plot = bokeh_renderer.get_plot(curve)
Expand Down Expand Up @@ -151,7 +195,7 @@ def test_colormapper_symmetric(self):
cmapper = plot.handles['color_mapper']
self.assertEqual(cmapper.low, -3)
self.assertEqual(cmapper.high, 3)

def test_colormapper_color_levels(self):
cmap = process_cmap('viridis', provider='bokeh')
img = Image(np.array([[0, 1], [2, 3]])).options(color_levels=5, cmap=cmap)
Expand Down
82 changes: 81 additions & 1 deletion tests/plotting/matplotlib/testelementplot.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import numpy as np

from holoviews.core.spaces import DynamicMap
from holoviews.element import Image, Curve, Scatter
from holoviews.element import Image, Curve, Scatter, Scatter3D
from holoviews.streams import Stream

from .testplot import TestMPLPlot, mpl_renderer

try:
from matplotlib.ticker import FormatStrFormatter, FuncFormatter, PercentFormatter
except:
pass

class TestElementPlot(TestMPLPlot):

Expand All @@ -17,6 +21,82 @@ def test_stream_cleanup(self):
plot.cleanup()
self.assertFalse(bool(stream._subscribers))

def test_element_xformatter_string(self):
curve = Curve(range(10)).options(xformatter='%d')
plot = mpl_renderer.get_plot(curve)
xaxis = plot.handles['axis'].xaxis
xformatter = xaxis.get_major_formatter()
self.assertIsInstance(xformatter, FormatStrFormatter)
self.assertEqual(xformatter.fmt, '%d')

def test_element_yformatter_string(self):
curve = Curve(range(10)).options(yformatter='%d')
plot = mpl_renderer.get_plot(curve)
yaxis = plot.handles['axis'].yaxis
yformatter = yaxis.get_major_formatter()
self.assertIsInstance(yformatter, FormatStrFormatter)
self.assertEqual(yformatter.fmt, '%d')

def test_element_zformatter_string(self):
curve = Scatter3D([]).options(zformatter='%d')
plot = mpl_renderer.get_plot(curve)
zaxis = plot.handles['axis'].zaxis
zformatter = zaxis.get_major_formatter()
self.assertIsInstance(zformatter, FormatStrFormatter)
self.assertEqual(zformatter.fmt, '%d')

def test_element_xformatter_function(self):
def formatter(value):
return str(value) + ' %'
curve = Curve(range(10)).options(xformatter=formatter)
plot = mpl_renderer.get_plot(curve)
xaxis = plot.handles['axis'].xaxis
xformatter = xaxis.get_major_formatter()
self.assertIsInstance(xformatter, FuncFormatter)

def test_element_yformatter_function(self):
def formatter(value):
return str(value) + ' %'
curve = Curve(range(10)).options(yformatter=formatter)
plot = mpl_renderer.get_plot(curve)
yaxis = plot.handles['axis'].yaxis
yformatter = yaxis.get_major_formatter()
self.assertIsInstance(yformatter, FuncFormatter)

def test_element_yformatter_function(self):
def formatter(value):
return str(value) + ' %'
curve = Scatter3D([]).options(zformatter=formatter)
plot = mpl_renderer.get_plot(curve)
zaxis = plot.handles['axis'].zaxis
zformatter = zaxis.get_major_formatter()
self.assertIsInstance(zformatter, FuncFormatter)

def test_element_xformatter_instance(self):
formatter = PercentFormatter()
curve = Curve(range(10)).options(xformatter=formatter)
plot = mpl_renderer.get_plot(curve)
xaxis = plot.handles['axis'].xaxis
xformatter = xaxis.get_major_formatter()
self.assertIs(xformatter, formatter)

def test_element_yformatter_instance(self):
formatter = PercentFormatter()
curve = Curve(range(10)).options(yformatter=formatter)
plot = mpl_renderer.get_plot(curve)
yaxis = plot.handles['axis'].yaxis
yformatter = yaxis.get_major_formatter()
self.assertIs(yformatter, formatter)

def test_element_zformatter_instance(self):
formatter = PercentFormatter()
curve = Scatter3D([]).options(zformatter=formatter)
plot = mpl_renderer.get_plot(curve)
zaxis = plot.handles['axis'].zaxis
zformatter = zaxis.get_major_formatter()
self.assertIs(zformatter, formatter)



class TestColorbarPlot(TestMPLPlot):

Expand Down

0 comments on commit 6441ccf

Please sign in to comment.