In [7]:
import unittest
import os
try:
    import cPickle as pickle
except:
    import pickle

path = os.getcwd()
path = path[:-6]
sep = os.path.sep
# Load a sample file to use for testing
comps = pickle.load(open(path + '%ssample_data_files%sunittest_comparisons.pkl'
                         % (sep, sep), 'rb'))

import sys
from bokeh.plotting import output_notebook

lib_path = os.path.abspath(os.path.join('..'))
sys.path.append(lib_path)
import interactive_plots as ip

output_notebook()

sa_dict = comps[0]

p = ip.interact_with_plot_all_outputs(sa_dict, demo=True, manual=False)


class TestInteractWithPlotAllOutputs(unittest.TestCase):

    def test_interact_with_plot_all_outputs_all_widgets_appear(self):
        """
        Are all widgets appearing and are they in right order?
        """
        l = p.widget.children
        self.assertEqual(len(l), 6)
        array_of_widget_names = []
        for i in range(len(l)):
            widgets = p.widget.children[i]
            array_of_widget_names.append(str(widgets.class_own_traits.im_self)
                                         .split('.')[-1].strip('\'>'))
        self.assertEqual(array_of_widget_names, ['BoundedFloatText',
                                       'IntText',
                                       'Checkbox',
                                       'Checkbox',
                                       'Checkbox',
                                       'SelectMultiple'])


    def test_interact_with_plot_all_outputs_default_values(self):
        """
        Are interactive widgets working properly and have proper
        default values??

        """
        self.assertEqual(p.widget.children[0].value, 0.01)
        self.assertEqual(p.widget.children[1].value, 20.0)
        self.assertEqual(p.widget.children[2].value, True)
        self.assertEqual(p.widget.children[3].value, True)
        self.assertEqual(p.widget.children[4].value, True)

        
    def test_interact_with_plot_all_outputs_plots_the_glyphs(self):
        """
        Are right number of glyphs showing up after calling interactive
        ipywidgets?
        """
        # This test confirms that the bokeh plots are generated
        # as glyphs can only be seen if plots are generated
        # when top 20 values are plotted number of glyphs = 12
        figure = p.widget.result.doc.roots[0]
        glyph_dict = figure.renderers
        
        # total number of glyphs = 12 when default values are set  
        self.assertEquals(len(glyph_dict),12)
        
    def test_interact_with_plot_all_outputs_error_bars_working(self):
        """
        Are right number of glyphs showing up after calling interactive
        ipywidgets?
        """
        # when error bars are removed 
        # number of glyphs = 15
        
        # q is the show error bars widget
        q = p.widget.children[3]
        q.value = False
        
        # n is the number of tabs after 2 runs of interact_with__all_plots
        # n = 4 after q.value is changed as it plots everything again
        
        n = p.widget.result.doc.roots
        glyph_dict_new = n[3].renderers
        
        # since error bars are removed number of glyphs now become 9
        self.assertEquals(len(glyph_dict_new),9)
        
    def test_interact_with_plot_all_outputs_top_values_working(self):
        """
        Are right number of glyphs showing up after calling interactive
        ipywidgets
        """
        # when error bars are removed 
        # number of glyphs = 15
        
        # r is the show top widget for r = 1 
        # the graph is histogram and has 7 glyphs
        r = p.widget.children[1]
        r.value = 1
        
        # n is the number of tabs after 3 runs of interact_with__all_plots
        # n = 6 after q.value is changed as it plots everything again
        
        n = p.widget.result.doc.roots
        glyph_dict_new = n[5].renderers
        
        # for histograms the number of glyphs now become 7 
        # when show top: value is 1
        self.assertEquals(len(glyph_dict_new),7)

    # Note other tests to be written to check interactivity of other widgets
        
suite = unittest.TestLoader().loadTestsFromTestCase(TestInteractWithPlotAllOutputs)
unittest.TextTestRunner(verbosity=20,stream=sys.stderr).run(suite)


<Bokeh Notebook handle for In[7]>

ok

----------------------------------------------------------------------
Ran 5 tests in 2.472s

OK
Out[7]:
<unittest.runner.TextTestResult run=5 errors=0 failures=0>

In [ ]: