-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathtest_offline.py
92 lines (66 loc) · 2.58 KB
/
test_offline.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
test__offline
"""
import re
from unittest import TestCase
import pytest
import plotly
import plotly.io as pio
from plotly import optional_imports
matplotlylib = optional_imports.get_module("plotly.matplotlylib")
if matplotlylib:
import matplotlib
# Force matplotlib to not use any Xwindows backend.
matplotlib.use("Agg")
import matplotlib.pyplot as plt
PLOTLYJS = plotly.offline.offline.get_plotlyjs()
class PlotlyOfflineTestCase(TestCase):
def setUp(self):
pass
def test_iplot_works_without_init_notebook_mode(self):
plotly.offline.iplot([{}])
def test_iplot_works_after_you_call_init_notebook_mode(self):
plotly.offline.init_notebook_mode()
plotly.offline.iplot([{}])
if matplotlylib:
@pytest.mark.matplotlib
def test_iplot_mpl_works(self):
plotly.offline.init_notebook_mode()
# Generate matplotlib plot for tests
fig = plt.figure()
x = [10, 20, 30]
y = [100, 200, 300]
plt.plot(x, y)
plotly.offline.iplot_mpl(fig)
class PlotlyOfflineMPLTestCase(TestCase):
def setUp(self):
pass
def _read_html(self, file_url):
"""Read and return the HTML contents from a file_url in the
form e.g. file:///Users/chriddyp/Repos/plotly.py/plotly-temp.html
"""
with open(file_url.replace("file://", "").replace(" ", "")) as f:
return f.read()
if matplotlylib:
@pytest.mark.matplotlib
def test_default_mpl_plot_generates_expected_html(self):
# Generate matplotlib plot for tests
fig = plt.figure()
x = [10, 20, 30]
y = [100, 200, 300]
plt.plot(x, y)
figure = plotly.tools.mpl_to_plotly(fig).to_dict()
data = figure["data"]
layout = figure["layout"]
data_json = pio.json.to_json_plotly(data)
layout_json = pio.json.to_json_plotly(layout)
html = self._read_html(plotly.offline.plot_mpl(fig))
# blank out uid before comparisons
data_json = re.sub('"uid": "[^"]+"', '"uid": ""', data_json)
html = re.sub('"uid": "[^"]+"', '"uid": ""', html)
# just make sure a few of the parts are in here
# like PlotlyOfflineTestCase(TestCase) in test_core
self.assertTrue(data_json in html) # data is in there
self.assertTrue(PLOTLYJS in html) # and the source code
# and it's an <html> doc
self.assertTrue(html.startswith("<html>") and html.endswith("</html>"))