| 
 | 1 | +"""  | 
 | 2 | +test__offline  | 
 | 3 | +
  | 
 | 4 | +"""  | 
 | 5 | +from __future__ import absolute_import  | 
 | 6 | + | 
 | 7 | +from nose.tools import raises  | 
 | 8 | +from unittest import TestCase  | 
 | 9 | +import json  | 
 | 10 | + | 
 | 11 | +import plotly  | 
 | 12 | + | 
 | 13 | +fig = {  | 
 | 14 | +    'data': [  | 
 | 15 | +        plotly.graph_objs.Scatter(x=[1, 2, 3], y=[10, 20, 30])  | 
 | 16 | +    ],  | 
 | 17 | +    'layout': plotly.graph_objs.Layout(  | 
 | 18 | +        title='offline plot'  | 
 | 19 | +    )  | 
 | 20 | +}  | 
 | 21 | + | 
 | 22 | +PLOTLYJS = plotly.offline.offline.get_plotlyjs()  | 
 | 23 | + | 
 | 24 | +class PlotlyOfflineTestCase(TestCase):  | 
 | 25 | +    def setUp(self):  | 
 | 26 | +        pass  | 
 | 27 | + | 
 | 28 | +    def _read_html(self, file_url):  | 
 | 29 | +        """ Read and return the HTML contents from a file_url  | 
 | 30 | +        in the form e.g. file:///Users/chriddyp/Repos/plotly.py/plotly-temp.html  | 
 | 31 | +        """  | 
 | 32 | +        with open(file_url.replace('file://', '').replace(' ', '')) as f:  | 
 | 33 | +            return f.read()  | 
 | 34 | + | 
 | 35 | +    def test_default_plot_generates_expected_html(self):  | 
 | 36 | +        data_json = json.dumps(fig['data'], cls=plotly.utils.PlotlyJSONEncoder)  | 
 | 37 | +        layout_json = json.dumps(  | 
 | 38 | +            fig['layout'],  | 
 | 39 | +            cls=plotly.utils.PlotlyJSONEncoder)  | 
 | 40 | + | 
 | 41 | +        html = self._read_html(plotly.offline.plot(fig))  | 
 | 42 | + | 
 | 43 | +        # I don't really want to test the entire script output, so  | 
 | 44 | +        # instead just make sure a few of the parts are in here?  | 
 | 45 | +        self.assertTrue('Plotly.newPlot' in html) # plot command is in there  | 
 | 46 | +        self.assertTrue(data_json in html)        # data is in there  | 
 | 47 | +        self.assertTrue(layout_json in html)      # so is layout  | 
 | 48 | +        self.assertTrue(PLOTLYJS in html)         # and the source code  | 
 | 49 | +        # and it's an <html> doc  | 
 | 50 | +        self.assertTrue(html.startswith('<html>') and html.endswith('</html>'))  | 
 | 51 | + | 
 | 52 | +    def test_including_plotlyjs(self):  | 
 | 53 | +        html = self._read_html(plotly.offline.plot(fig, include_plotlyjs=False))  | 
 | 54 | +        self.assertTrue(PLOTLYJS not in html)  | 
 | 55 | + | 
 | 56 | +    def test_div_output(self):  | 
 | 57 | +        html = plotly.offline.plot(fig, output_type='div')  | 
 | 58 | + | 
 | 59 | +        self.assertTrue('<html>' not in html and '</html>' not in html)  | 
 | 60 | +        self.assertTrue(html.startswith('<div>') and html.endswith('</div>'))  | 
 | 61 | + | 
 | 62 | +    def test_autoresizing(self):  | 
 | 63 | +        resize_code_strings = [  | 
 | 64 | +            'window.addEventListener("resize", ',  | 
 | 65 | +            'Plotly.Plots.resize('  | 
 | 66 | +        ]  | 
 | 67 | +        # If width or height wasn't specified, then we add a window resizer  | 
 | 68 | +        html = self._read_html(plotly.offline.plot(fig))  | 
 | 69 | +        for resize_code_string in resize_code_strings:  | 
 | 70 | +            self.assertTrue(resize_code_string in html)  | 
 | 71 | + | 
 | 72 | +        # If width or height was specified, then we don't resize  | 
 | 73 | +        html = plotly.offline.plot({  | 
 | 74 | +            'data': fig['data'],  | 
 | 75 | +            'layout': {  | 
 | 76 | +                'width': 500, 'height': 500  | 
 | 77 | +            }  | 
 | 78 | +        })  | 
 | 79 | +        for resize_code_string in resize_code_strings:  | 
 | 80 | +            self.assertTrue(resize_code_string not in html)  | 
0 commit comments