9
9
10
10
import random
11
11
import string
12
+ from unittest import skip
12
13
13
- from nose import with_setup
14
14
from nose .plugins .attrib import attr
15
- from nose .tools import raises
16
- from unittest import skip
17
15
18
16
import plotly .plotly as py
19
17
from plotly .exceptions import InputError , PlotlyRequestError , PlotlyError
20
18
from plotly .graph_objs import Scatter
21
19
from plotly .grid_objs import Column , Grid
22
20
from plotly .plotly .plotly import parse_grid_id_args
21
+ from plotly .tests .utils import PlotlyTestCase
23
22
24
23
25
24
def random_filename ():
@@ -29,179 +28,154 @@ def random_filename():
29
28
return unique_filename
30
29
31
30
32
- def get_grid ():
33
- c1 = Column ([1 , 2 , 3 , 4 ], 'first column' )
34
- c2 = Column (['a' , 'b' , 'c' , 'd' ], 'second column' )
35
- g = Grid ([c1 , c2 ])
36
- return g
37
-
38
-
39
- def init ():
40
- py .sign_in ('PythonTest' , '9v9f20pext' )
41
-
42
-
43
- def upload_and_return_grid ():
44
- init ()
45
- g = get_grid ()
46
- unique_filename = random_filename ()
47
-
48
- py .grid_ops .upload (g , unique_filename , auto_open = False )
49
- return g
50
-
51
-
52
- # Nominal usage
53
- @attr ('slow' )
54
- def test_grid_upload ():
55
- upload_and_return_grid ()
56
-
57
-
58
- @attr ('slow' )
59
- @with_setup (init )
60
- def test_grid_upload_in_new_folder ():
61
- g = get_grid ()
62
- path = (
63
- 'new folder: {0}/grid in folder {1}'
64
- .format (random_filename (), random_filename ())
65
- )
66
- py .grid_ops .upload (g , path , auto_open = False )
67
-
68
-
69
- @attr ('slow' )
70
- @with_setup (init )
71
- def test_grid_upload_in_existing_folder ():
72
- g = get_grid ()
73
- folder = random_filename ()
74
- filename = random_filename ()
75
- py .file_ops .mkdirs (folder )
76
- path = (
77
- 'existing folder: {0}/grid in folder {1}'
78
- .format (folder , filename )
79
- )
80
- py .grid_ops .upload (g , path , auto_open = False )
81
-
82
-
83
- @attr ('slow' )
84
- def test_column_append ():
85
- g = upload_and_return_grid ()
86
- new_col = Column ([1 , 5 , 3 ], 'new col' )
87
-
88
- py .grid_ops .append_columns ([new_col ], grid = g )
89
-
90
-
91
- @attr ('slow' )
92
- def test_row_append ():
93
- g = upload_and_return_grid ()
94
- new_rows = [[1 , 2 ], [10 , 20 ]]
95
-
96
- py .grid_ops .append_rows (new_rows , grid = g )
97
-
98
-
99
- @attr ('slow' )
100
- def test_plot_from_grid ():
101
- g = upload_and_return_grid ()
102
- url = py .plot ([Scatter (xsrc = g [0 ], ysrc = g [1 ])],
103
- auto_open = False , filename = 'plot from grid' )
104
- return url , g
105
-
31
+ class GridTest (PlotlyTestCase ):
106
32
107
- @attr ('slow' )
108
- @with_setup (init )
109
- def test_get_figure_from_references ():
110
- url , g = test_plot_from_grid ()
111
- fig = py .get_figure (url )
112
- data = fig ['data' ]
113
- trace = data [0 ]
114
- assert (g [0 ].data == trace ['x' ])
115
- assert (g [1 ].data == trace ['y' ])
33
+ # Test grid args
34
+ _grid_id = 'chris:3043'
35
+ _grid = Grid ([])
36
+ _grid .id = _grid_id
37
+ _grid_url = 'https://plot.ly/~chris/3043/my-grid'
116
38
117
- # Test grid args
118
- _grid_id = 'chris:3043'
119
- _grid = Grid ([])
120
- _grid .id = _grid_id
121
- _grid_url = 'https://plot.ly/~chris/3043/my-grid'
39
+ def setUp (self ):
40
+ super (GridTest , self ).setUp ()
41
+ py .sign_in ('PythonTest' , '9v9f20pext' )
122
42
43
+ def get_grid (self ):
44
+ c1 = Column ([1 , 2 , 3 , 4 ], 'first column' )
45
+ c2 = Column (['a' , 'b' , 'c' , 'd' ], 'second column' )
46
+ g = Grid ([c1 , c2 ])
47
+ return g
123
48
124
- def test_grid_id_args ():
125
- assert (
126
- parse_grid_id_args (_grid , None ) == parse_grid_id_args (None , _grid_url )
127
- )
128
-
129
-
130
- @raises (InputError )
131
- def test_no_grid_id_args ():
132
- parse_grid_id_args (None , None )
133
-
134
-
135
- @raises (InputError )
136
- def test_overspecified_grid_args ():
137
- parse_grid_id_args (_grid , _grid_url )
138
-
139
-
140
- # Out of order usage
141
- @raises (InputError )
142
- def test_scatter_from_non_uploaded_grid ():
143
- c1 = Column ([1 , 2 , 3 , 4 ], 'first column' )
144
- c2 = Column (['a' , 'b' , 'c' , 'd' ], 'second column' )
145
- g = Grid ([c1 , c2 ])
146
-
147
- Scatter (xsrc = g [0 ], ysrc = g [1 ])
148
-
149
-
150
- @raises (PlotlyError )
151
- def test_column_append_of_non_uploaded_grid ():
152
- c1 = Column ([1 , 2 , 3 , 4 ], 'first column' )
153
- c2 = Column (['a' , 'b' , 'c' , 'd' ], 'second column' )
154
- g = Grid ([c1 ])
155
- py .grid_ops .append_columns ([c2 ], grid = g )
156
-
157
-
158
- @raises (PlotlyError )
159
- def test_row_append_of_non_uploaded_grid ():
160
- c1 = Column ([1 , 2 , 3 , 4 ], 'first column' )
161
- rows = [[1 ], [2 ]]
162
- g = Grid ([c1 ])
163
- py .grid_ops .append_rows (rows , grid = g )
164
-
165
-
166
- # Input Errors
167
- @attr ('slow' )
168
- @raises (InputError )
169
- def test_unequal_length_rows ():
170
- g = upload_and_return_grid ()
171
- rows = [[1 , 2 ], ['to' , 'many' , 'cells' ]]
172
- py .grid_ops .append_rows (rows , grid = g )
173
-
174
-
175
- # Test duplicate columns
176
- @raises (InputError )
177
- def test_duplicate_columns ():
178
- c1 = Column ([1 , 2 , 3 , 4 ], 'first column' )
179
- c2 = Column (['a' , 'b' , 'c' , 'd' ], 'first column' )
180
- Grid ([c1 , c2 ])
181
-
182
-
183
- # Test delete
184
- @attr ('slow' )
185
- @with_setup (init )
186
- def test_delete_grid ():
187
- g = get_grid ()
188
- fn = random_filename ()
189
- py .grid_ops .upload (g , fn , auto_open = False )
190
- py .grid_ops .delete (g )
191
- py .grid_ops .upload (g , fn , auto_open = False )
192
-
193
-
194
- # Plotly failures
195
- @skip ('adding this for now so test_file_tools pass, more info' +
196
- 'https://github.com/plotly/python-api/issues/262' )
197
- def test_duplicate_filenames ():
198
- c1 = Column ([1 , 2 , 3 , 4 ], 'first column' )
199
- g = Grid ([c1 ])
200
-
201
- random_chars = [random .choice (string .ascii_uppercase ) for _ in range (5 )]
202
- unique_filename = 'Valid Grid ' + '' .join (random_chars )
203
- py .grid_ops .upload (g , unique_filename , auto_open = False )
204
- try :
49
+ def upload_and_return_grid (self ):
50
+ g = self .get_grid ()
51
+ unique_filename = random_filename ()
52
+ py .grid_ops .upload (g , unique_filename , auto_open = False )
53
+ return g
54
+
55
+ # Nominal usage
56
+ @attr ('slow' )
57
+ def test_grid_upload (self ):
58
+ self .upload_and_return_grid ()
59
+
60
+ @attr ('slow' )
61
+ def test_grid_upload_in_new_folder (self ):
62
+ g = self .get_grid ()
63
+ path = (
64
+ 'new folder: {0}/grid in folder {1}'
65
+ .format (random_filename (), random_filename ())
66
+ )
67
+ py .grid_ops .upload (g , path , auto_open = False )
68
+
69
+ @attr ('slow' )
70
+ def test_grid_upload_in_existing_folder (self ):
71
+ g = self .get_grid ()
72
+ folder = random_filename ()
73
+ filename = random_filename ()
74
+ py .file_ops .mkdirs (folder )
75
+ path = (
76
+ 'existing folder: {0}/grid in folder {1}'
77
+ .format (folder , filename )
78
+ )
79
+ py .grid_ops .upload (g , path , auto_open = False )
80
+
81
+ @attr ('slow' )
82
+ def test_column_append (self ):
83
+ g = self .upload_and_return_grid ()
84
+ new_col = Column ([1 , 5 , 3 ], 'new col' )
85
+ py .grid_ops .append_columns ([new_col ], grid = g )
86
+
87
+ @attr ('slow' )
88
+ def test_row_append (self ):
89
+ g = self .upload_and_return_grid ()
90
+ new_rows = [[1 , 2 ], [10 , 20 ]]
91
+ py .grid_ops .append_rows (new_rows , grid = g )
92
+
93
+ @attr ('slow' )
94
+ def test_plot_from_grid (self ):
95
+ g = self .upload_and_return_grid ()
96
+ url = py .plot ([Scatter (xsrc = g [0 ], ysrc = g [1 ])],
97
+ auto_open = False , filename = 'plot from grid' )
98
+ return url , g
99
+
100
+ @attr ('slow' )
101
+ def test_get_figure_from_references (self ):
102
+ url , g = self .test_plot_from_grid ()
103
+ fig = py .get_figure (url )
104
+ data = fig ['data' ]
105
+ trace = data [0 ]
106
+ assert (g [0 ].data == trace ['x' ])
107
+ assert (g [1 ].data == trace ['y' ])
108
+
109
+ def test_grid_id_args (self ):
110
+ self .assertEqual (parse_grid_id_args (self ._grid , None ),
111
+ parse_grid_id_args (None , self ._grid_url ))
112
+
113
+ def test_no_grid_id_args (self ):
114
+ with self .assertRaises (InputError ):
115
+ parse_grid_id_args (None , None )
116
+
117
+ def test_overspecified_grid_args (self ):
118
+ with self .assertRaises (InputError ):
119
+ parse_grid_id_args (self ._grid , self ._grid_url )
120
+
121
+ # Out of order usage
122
+ def test_scatter_from_non_uploaded_grid (self ):
123
+ c1 = Column ([1 , 2 , 3 , 4 ], 'first column' )
124
+ c2 = Column (['a' , 'b' , 'c' , 'd' ], 'second column' )
125
+ g = Grid ([c1 , c2 ])
126
+ with self .assertRaises (InputError ):
127
+ Scatter (xsrc = g [0 ], ysrc = g [1 ])
128
+
129
+ def test_column_append_of_non_uploaded_grid (self ):
130
+ c1 = Column ([1 , 2 , 3 , 4 ], 'first column' )
131
+ c2 = Column (['a' , 'b' , 'c' , 'd' ], 'second column' )
132
+ g = Grid ([c1 ])
133
+ with self .assertRaises (PlotlyError ):
134
+ py .grid_ops .append_columns ([c2 ], grid = g )
135
+
136
+ def test_row_append_of_non_uploaded_grid (self ):
137
+ c1 = Column ([1 , 2 , 3 , 4 ], 'first column' )
138
+ rows = [[1 ], [2 ]]
139
+ g = Grid ([c1 ])
140
+ with self .assertRaises (PlotlyError ):
141
+ py .grid_ops .append_rows (rows , grid = g )
142
+
143
+ # Input Errors
144
+ @attr ('slow' )
145
+ def test_unequal_length_rows (self ):
146
+ g = self .upload_and_return_grid ()
147
+ rows = [[1 , 2 ], ['to' , 'many' , 'cells' ]]
148
+ with self .assertRaises (InputError ):
149
+ py .grid_ops .append_rows (rows , grid = g )
150
+
151
+ # Test duplicate columns
152
+ def test_duplicate_columns (self ):
153
+ c1 = Column ([1 , 2 , 3 , 4 ], 'first column' )
154
+ c2 = Column (['a' , 'b' , 'c' , 'd' ], 'first column' )
155
+ with self .assertRaises (InputError ):
156
+ Grid ([c1 , c2 ])
157
+
158
+ # Test delete
159
+ @attr ('slow' )
160
+ def test_delete_grid (self ):
161
+ g = self .get_grid ()
162
+ fn = random_filename ()
163
+ py .grid_ops .upload (g , fn , auto_open = False )
164
+ py .grid_ops .delete (g )
165
+ py .grid_ops .upload (g , fn , auto_open = False )
166
+
167
+ # Plotly failures
168
+ @skip ('adding this for now so test_file_tools pass, more info' +
169
+ 'https://github.com/plotly/python-api/issues/262' )
170
+ def test_duplicate_filenames (self ):
171
+ c1 = Column ([1 , 2 , 3 , 4 ], 'first column' )
172
+ g = Grid ([c1 ])
173
+
174
+ random_chars = [random .choice (string .ascii_uppercase )
175
+ for _ in range (5 )]
176
+ unique_filename = 'Valid Grid ' + '' .join (random_chars )
205
177
py .grid_ops .upload (g , unique_filename , auto_open = False )
206
- except PlotlyRequestError as e :
207
- assert (e .status_code == 409 )
178
+ try :
179
+ py .grid_ops .upload (g , unique_filename , auto_open = False )
180
+ except PlotlyRequestError as e :
181
+ assert (e .status_code == 409 )
0 commit comments