|
30 | 30 |
|
31 | 31 | PLOTLYJS = plotly.offline.offline.get_plotlyjs()
|
32 | 32 |
|
| 33 | +cdn_script = ('<script src="https://cdn.plot.ly/plotly-latest.min.js">' |
| 34 | + '</script>') |
| 35 | + |
| 36 | +directory_script = '<script src="plotly.min.js"></script>' |
| 37 | + |
33 | 38 |
|
34 | 39 | class PlotlyOfflineBaseTestCase(TestCase):
|
35 | 40 | def tearDown(self):
|
36 | 41 | # Some offline tests produce an html file. Make sure we clean up :)
|
37 | 42 | try:
|
38 | 43 | os.remove('temp-plot.html')
|
| 44 | + # Some tests that produce temp-plot.html] |
| 45 | + # also produce plotly.min.js |
| 46 | + os.remove('plotly.min.js') |
39 | 47 | except OSError:
|
40 | 48 | pass
|
41 | 49 |
|
@@ -71,10 +79,109 @@ def test_default_plot_generates_expected_html(self):
|
71 | 79 | # and it's an <html> doc
|
72 | 80 | self.assertTrue(html.startswith('<html>') and html.endswith('</html>'))
|
73 | 81 |
|
74 |
| - def test_including_plotlyjs(self): |
75 |
| - html = self._read_html(plotly.offline.plot(fig, include_plotlyjs=False, |
76 |
| - auto_open=False)) |
77 |
| - self.assertNotIn(PLOTLYJS, html) |
| 82 | + def test_including_plotlyjs_truthy_html(self): |
| 83 | + # For backwards compatibility all truthy values that aren't otherwise |
| 84 | + # recognized are considered true |
| 85 | + for include_plotlyjs in [True, 34, 'non-empty-str']: |
| 86 | + html = self._read_html(plotly.offline.plot( |
| 87 | + fig, |
| 88 | + include_plotlyjs=include_plotlyjs, |
| 89 | + output_type='file', |
| 90 | + auto_open=False)) |
| 91 | + self.assertIn(PLOTLYJS, html) |
| 92 | + self.assertNotIn(cdn_script, html) |
| 93 | + self.assertNotIn(directory_script, html) |
| 94 | + |
| 95 | + def test_including_plotlyjs_truthy_div(self): |
| 96 | + # For backwards compatibility all truthy values that aren't otherwise |
| 97 | + # recognized are considered true |
| 98 | + for include_plotlyjs in [True, 34, 'non-empty-str']: |
| 99 | + html = plotly.offline.plot( |
| 100 | + fig, |
| 101 | + include_plotlyjs=include_plotlyjs, |
| 102 | + output_type='div') |
| 103 | + self.assertIn(PLOTLYJS, html) |
| 104 | + self.assertNotIn(cdn_script, html) |
| 105 | + self.assertNotIn(directory_script, html) |
| 106 | + |
| 107 | + def test_including_plotlyjs_false_html(self): |
| 108 | + # For backwards compatibility all truthy values that aren't otherwise |
| 109 | + # recognized are considered true |
| 110 | + for include_plotlyjs in [False, 0, '']: |
| 111 | + html = self._read_html(plotly.offline.plot( |
| 112 | + fig, |
| 113 | + include_plotlyjs=include_plotlyjs, |
| 114 | + output_type='file', |
| 115 | + auto_open=False)) |
| 116 | + self.assertNotIn(PLOTLYJS, html) |
| 117 | + self.assertNotIn(cdn_script, html) |
| 118 | + self.assertNotIn(directory_script, html) |
| 119 | + |
| 120 | + def test_including_plotlyjs_false_div(self): |
| 121 | + for include_plotlyjs in [False, 0, '']: |
| 122 | + html = plotly.offline.plot( |
| 123 | + fig, |
| 124 | + include_plotlyjs=include_plotlyjs, |
| 125 | + output_type='div') |
| 126 | + self.assertNotIn(PLOTLYJS, html) |
| 127 | + self.assertNotIn(cdn_script, html) |
| 128 | + self.assertNotIn(directory_script, html) |
| 129 | + |
| 130 | + def test_including_plotlyjs_cdn_html(self): |
| 131 | + for include_plotlyjs in ['cdn', 'CDN', 'Cdn']: |
| 132 | + html = self._read_html(plotly.offline.plot( |
| 133 | + fig, |
| 134 | + include_plotlyjs=include_plotlyjs, |
| 135 | + output_type='file', |
| 136 | + auto_open=False)) |
| 137 | + self.assertNotIn(PLOTLYJS, html) |
| 138 | + self.assertIn(cdn_script, html) |
| 139 | + self.assertNotIn(directory_script, html) |
| 140 | + |
| 141 | + def test_including_plotlyjs_cdn_div(self): |
| 142 | + for include_plotlyjs in ['cdn', 'CDN', 'Cdn']: |
| 143 | + html = plotly.offline.plot( |
| 144 | + fig, |
| 145 | + include_plotlyjs=include_plotlyjs, |
| 146 | + output_type='div') |
| 147 | + self.assertNotIn(PLOTLYJS, html) |
| 148 | + self.assertIn(cdn_script, html) |
| 149 | + self.assertNotIn(directory_script, html) |
| 150 | + |
| 151 | + def test_including_plotlyjs_directory_html(self): |
| 152 | + self.assertFalse(os.path.exists('plotly.min.js')) |
| 153 | + |
| 154 | + for include_plotlyjs in ['directory', 'Directory', 'DIRECTORY']: |
| 155 | + html = self._read_html(plotly.offline.plot( |
| 156 | + fig, |
| 157 | + include_plotlyjs=include_plotlyjs, |
| 158 | + auto_open=False)) |
| 159 | + self.assertNotIn(PLOTLYJS, html) |
| 160 | + self.assertNotIn(cdn_script, html) |
| 161 | + self.assertIn(directory_script, html) |
| 162 | + |
| 163 | + # plot creates plotly.min.js in the output directory |
| 164 | + self.assertTrue(os.path.exists('plotly.min.js')) |
| 165 | + with open('plotly.min.js', 'r') as f: |
| 166 | + self.assertEqual(f.read(), PLOTLYJS) |
| 167 | + |
| 168 | + def test_including_plotlyjs_directory_div(self): |
| 169 | + self.assertFalse(os.path.exists('plotly.min.js')) |
| 170 | + |
| 171 | + for include_plotlyjs in ['directory', 'Directory', 'DIRECTORY']: |
| 172 | + html = plotly.offline.plot( |
| 173 | + fig, |
| 174 | + include_plotlyjs=include_plotlyjs, |
| 175 | + output_type='div', |
| 176 | + auto_open=False) |
| 177 | + |
| 178 | + self.assertNotIn(PLOTLYJS, html) |
| 179 | + self.assertNotIn(cdn_script, html) |
| 180 | + self.assertIn(directory_script, html) |
| 181 | + |
| 182 | + # plot does NOT create a plotly.min.js file in the output directory |
| 183 | + # when output_type is div |
| 184 | + self.assertFalse(os.path.exists('plotly.min.js')) |
78 | 185 |
|
79 | 186 | def test_div_output(self):
|
80 | 187 | html = plotly.offline.plot(fig, output_type='div', auto_open=False)
|
@@ -103,7 +210,7 @@ def test_autoresizing(self):
|
103 | 210 | def test_autoresizing_div(self):
|
104 | 211 |
|
105 | 212 | # If width or height wasn't specified, then we add a window resizer
|
106 |
| - for include_plotlyjs in [True, False]: |
| 213 | + for include_plotlyjs in [True, False, 'cdn', 'directory']: |
107 | 214 | html = plotly.offline.plot(fig,
|
108 | 215 | output_type='div',
|
109 | 216 | include_plotlyjs=include_plotlyjs)
|
|
0 commit comments