Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 36686ad

Browse files
authored
Merge pull request plotly#460 from plotly/Gantt
First PR for Gantt Charts - Adding Tests Very Soon
2 parents 079c924 + a7be116 commit 36686ad

File tree

3 files changed

+1021
-0
lines changed

3 files changed

+1021
-0
lines changed

plotly/tests/test_core/test_tools/test_figure_factory.py

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,246 @@ def test_table_with_index(self):
11251125
'zeroline': False}}}
11261126
self.assertEqual(index_table, exp_index_table)
11271127

1128+
1129+
class TestGantt(TestCase):
1130+
1131+
def test_validate_gantt(self):
1132+
1133+
# validate the basic gantt inputs
1134+
1135+
df = [{'Task': 'Job A',
1136+
'Start': '2009-02-01',
1137+
'Finish': '2009-08-30',
1138+
'Complete': 'a'}]
1139+
1140+
pattern2 = ('In order to use an indexing column and assign colors to '
1141+
'the values of the index, you must choose an actual '
1142+
'column name in the dataframe or key if a list of '
1143+
'dictionaries is being used.')
1144+
1145+
self.assertRaisesRegexp(PlotlyError, pattern2,
1146+
tls.FigureFactory.create_gantt,
1147+
df, index_col='foo')
1148+
1149+
df = 'foo'
1150+
1151+
pattern3 = ('You must input either a dataframe or a list of '
1152+
'dictionaries.')
1153+
1154+
self.assertRaisesRegexp(PlotlyError, pattern3,
1155+
tls.FigureFactory.create_gantt, df)
1156+
1157+
df = []
1158+
1159+
pattern4 = ('Your list is empty. It must contain at least one '
1160+
'dictionary.')
1161+
1162+
self.assertRaisesRegexp(PlotlyError, pattern4,
1163+
tls.FigureFactory.create_gantt, df)
1164+
1165+
df = ['foo']
1166+
1167+
pattern5 = ('Your list must only include dictionaries.')
1168+
1169+
self.assertRaisesRegexp(PlotlyError, pattern5,
1170+
tls.FigureFactory.create_gantt, df)
1171+
1172+
def test_gantt_index(self):
1173+
1174+
# validate the index used for gantt
1175+
1176+
df = [{'Task': 'Job A',
1177+
'Start': '2009-02-01',
1178+
'Finish': '2009-08-30',
1179+
'Complete': 50}]
1180+
1181+
pattern = ('In order to use an indexing column and assign colors to '
1182+
'the values of the index, you must choose an actual '
1183+
'column name in the dataframe or key if a list of '
1184+
'dictionaries is being used.')
1185+
1186+
self.assertRaisesRegexp(PlotlyError, pattern,
1187+
tls.FigureFactory.create_gantt,
1188+
df, index_col='foo')
1189+
1190+
df = [{'Task': 'Job A', 'Start': '2009-02-01',
1191+
'Finish': '2009-08-30', 'Complete': 'a'},
1192+
{'Task': 'Job A', 'Start': '2009-02-01',
1193+
'Finish': '2009-08-30', 'Complete': 50}]
1194+
1195+
pattern2 = ('Error in indexing column. Make sure all entries of each '
1196+
'column are all numbers or all strings.')
1197+
1198+
self.assertRaisesRegexp(PlotlyError, pattern2,
1199+
tls.FigureFactory.create_gantt,
1200+
df, index_col='Complete')
1201+
1202+
def test_gantt_validate_colors(self):
1203+
1204+
# validate the gantt colors variable
1205+
1206+
df = [{'Task': 'Job A', 'Start': '2009-02-01',
1207+
'Finish': '2009-08-30', 'Complete': 75, 'Resource': 'A'},
1208+
{'Task': 'Job B', 'Start': '2009-02-01',
1209+
'Finish': '2009-08-30', 'Complete': 50, 'Resource': 'B'}]
1210+
1211+
pattern = ('Whoops! The elements in your rgb colors tuples cannot '
1212+
'exceed 255.0.')
1213+
1214+
self.assertRaisesRegexp(PlotlyError, pattern,
1215+
tls.FigureFactory.create_gantt, df,
1216+
index_col='Complete', colors='rgb(300,1,1)')
1217+
1218+
self.assertRaises(PlotlyError, tls.FigureFactory.create_gantt,
1219+
df, index_col='Complete', colors='foo')
1220+
1221+
pattern2 = ('Whoops! The elements in your colors tuples cannot '
1222+
'exceed 1.0.')
1223+
1224+
self.assertRaisesRegexp(PlotlyError, pattern2,
1225+
tls.FigureFactory.create_gantt, df,
1226+
index_col='Complete', colors=(2, 1, 1))
1227+
1228+
# verify that if colors is a dictionary, its keys span all the
1229+
# values in the index column
1230+
colors_dict = {75: 'rgb(1, 2, 3)'}
1231+
1232+
pattern3 = ('If you are using colors as a dictionary, all of its '
1233+
'keys must be all the values in the index column.')
1234+
1235+
self.assertRaisesRegexp(PlotlyError, pattern3,
1236+
tls.FigureFactory.create_gantt, df,
1237+
index_col='Complete', colors=colors_dict)
1238+
1239+
# check: index is set if colors is a dictionary
1240+
colors_dict_good = {50: 'rgb(1, 2, 3)', 75: 'rgb(5, 10, 15)'}
1241+
1242+
pattern4 = ('Error. You have set colors to a dictionary but have not '
1243+
'picked an index. An index is required if you are '
1244+
'assigning colors to particular values in a dictioanry.')
1245+
1246+
self.assertRaisesRegexp(PlotlyError, pattern4,
1247+
tls.FigureFactory.create_gantt, df,
1248+
colors=colors_dict_good)
1249+
1250+
# check: number of colors is equal to or greater than number of
1251+
# unique index string values
1252+
pattern5 = ("Error. The number of colors in 'colors' must be no less "
1253+
"than the number of unique index values in your group "
1254+
"column.")
1255+
1256+
self.assertRaisesRegexp(PlotlyError, pattern5,
1257+
tls.FigureFactory.create_gantt, df,
1258+
index_col='Resource',
1259+
colors=['#ffffff'])
1260+
1261+
# check: if index is numeric, colors has at least 2 colors in it
1262+
pattern6 = ("You must use at least 2 colors in 'colors' if you "
1263+
"are using a colorscale. However only the first two "
1264+
"colors given will be used for the lower and upper "
1265+
"bounds on the colormap.")
1266+
1267+
self.assertRaisesRegexp(PlotlyError, pattern6,
1268+
tls.FigureFactory.create_gantt, df,
1269+
index_col='Complete',
1270+
colors=['#ffffff'])
1271+
1272+
def test_gantt_all_args(self):
1273+
1274+
# check if gantt chart matches with expected output
1275+
1276+
df = [{'Task': 'Run',
1277+
'Start': '2010-01-01',
1278+
'Finish': '2011-02-02',
1279+
'Complete': 0},
1280+
{'Task': 'Fast',
1281+
'Start': '2011-01-01',
1282+
'Finish': '2012-06-05',
1283+
'Complete': 25}]
1284+
1285+
test_gantt_chart = tls.FigureFactory.create_gantt(
1286+
df, colors='Blues', index_col='Complete', reverse_colors=True,
1287+
title='Title', bar_width=0.5, showgrid_x=True, showgrid_y=True,
1288+
height=500, width=500
1289+
)
1290+
1291+
exp_gantt_chart = {
1292+
'data': [{'marker': {'color': 'white'},
1293+
'name': '',
1294+
'x': ['2010-01-01', '2011-02-02'],
1295+
'y': [0, 0]},
1296+
{'marker': {'color': 'white'},
1297+
'name': '',
1298+
'x': ['2011-01-01', '2012-06-05'],
1299+
'y': [1, 1]}],
1300+
'layout': {'height': 500,
1301+
'hovermode': 'closest',
1302+
'shapes': [{'fillcolor': 'rgb(220.0, 220.0, 220.0)',
1303+
'line': {'width': 0},
1304+
'opacity': 1,
1305+
'type': 'rect',
1306+
'x0': '2010-01-01',
1307+
'x1': '2011-02-02',
1308+
'xref': 'x',
1309+
'y0': -0.5,
1310+
'y1': 0.5,
1311+
'yref': 'y'},
1312+
{'fillcolor': 'rgb(166.25, 167.5, 208.0)',
1313+
'line': {'width': 0},
1314+
'opacity': 1,
1315+
'type': 'rect',
1316+
'x0': '2011-01-01',
1317+
'x1': '2012-06-05',
1318+
'xref': 'x',
1319+
'y0': 0.5,
1320+
'y1': 1.5,
1321+
'yref': 'y'}],
1322+
'showlegend': False,
1323+
'title': 'Title',
1324+
'width': 500,
1325+
'xaxis': {'rangeselector': {'buttons': [
1326+
{'count': 7,
1327+
'label': '1w',
1328+
'step': 'day',
1329+
'stepmode': 'backward'},
1330+
{'count': 1,
1331+
'label': '1m',
1332+
'step': 'month',
1333+
'stepmode': 'backward'},
1334+
{'count': 6,
1335+
'label': '6m',
1336+
'step': 'month',
1337+
'stepmode': 'backward'},
1338+
{'count': 1,
1339+
'label': 'YTD',
1340+
'step': 'year',
1341+
'stepmode': 'todate'},
1342+
{'count': 1,
1343+
'label': '1y',
1344+
'step': 'year',
1345+
'stepmode': 'backward'},
1346+
{'step': 'all'}
1347+
]},
1348+
'showgrid': True,
1349+
'type': 'date',
1350+
'zeroline': False},
1351+
'yaxis': {'autorange': False,
1352+
'range': [-1, 3],
1353+
'showgrid': True,
1354+
'ticktext': ['Run', 'Fast'],
1355+
'tickvals': [0, 1],
1356+
'zeroline': False}}
1357+
}
1358+
1359+
self.assertEqual(test_gantt_chart['data'][0],
1360+
exp_gantt_chart['data'][0])
1361+
1362+
self.assertEqual(test_gantt_chart['data'][1],
1363+
exp_gantt_chart['data'][1])
1364+
1365+
self.assertEqual(test_gantt_chart['layout'],
1366+
exp_gantt_chart['layout'])
1367+
11281368
# class TestDistplot(TestCase):
11291369

11301370
# def test_scipy_import_error(self):

plotly/tests/test_optional/test_figure_factory.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,85 @@ def test_scatter_plot_matrix_kwargs(self):
12671267
exp_scatter_plot_matrix['layout'])
12681268

12691269

1270+
class TestGantt(NumpyTestUtilsMixin, TestCase):
1271+
1272+
def test_df_dataframe(self):
1273+
1274+
# validate dataframe has correct column names
1275+
df1 = pd.DataFrame([[2, 'Apple']], columns=['Numbers', 'Fruit'])
1276+
self.assertRaises(PlotlyError, tls.FigureFactory.create_gantt, df1)
1277+
1278+
def test_df_dataframe_all_args(self):
1279+
1280+
# check if gantt chart matches with expected output
1281+
1282+
df = pd.DataFrame([['Job A', '2009-01-01', '2009-02-30'],
1283+
['Job B', '2009-03-05', '2009-04-15']],
1284+
columns=['Task', 'Start', 'Finish'])
1285+
1286+
test_gantt_chart = tls.FigureFactory.create_gantt(df)
1287+
1288+
exp_gantt_chart = {
1289+
'data': [{'marker': {'color': 'white'},
1290+
'name': '',
1291+
'x': ['2009-01-01', '2009-02-30'],
1292+
'y': [0, 0]}],
1293+
'layout': {'height': 600,
1294+
'hovermode': 'closest',
1295+
'shapes': [{'opacity': 1,
1296+
'y1': 0.2,
1297+
'xref': 'x',
1298+
'fillcolor': 'rgb(31.0, 119.0, 180.0)',
1299+
'yref': 'y',
1300+
'y0': -0.2,
1301+
'x0': '2009-01-01',
1302+
'x1': '2009-02-30',
1303+
'type': 'rect',
1304+
'line': {'width': 0}},
1305+
{'opacity': 1,
1306+
'y1': 1.2,
1307+
'xref': 'x',
1308+
'fillcolor': 'rgb(255.0, 127.0, 14.0)',
1309+
'yref': 'y',
1310+
'y0': 0.8,
1311+
'x0': '2009-03-05',
1312+
'x1': '2009-04-15',
1313+
'type': 'rect',
1314+
'line': {'width': 0}}],
1315+
'showlegend': False,
1316+
'title': 'Gantt Chart',
1317+
'width': 900,
1318+
'xaxis': {'rangeselector': {'buttons': [
1319+
{'count': 7, 'label': '1w',
1320+
'step': 'day', 'stepmode': 'backward'},
1321+
{'count': 1, 'label': '1m',
1322+
'step': 'month', 'stepmode': 'backward'},
1323+
{'count': 6, 'label': '6m',
1324+
'step': 'month', 'stepmode': 'backward'},
1325+
{'count': 1, 'label': 'YTD',
1326+
'step': 'year', 'stepmode': 'todate'},
1327+
{'count': 1, 'label': '1y',
1328+
'step': 'year', 'stepmode': 'backward'},
1329+
{'step': 'all'}
1330+
]},
1331+
'showgrid': False,
1332+
'type': 'date',
1333+
'zeroline': False},
1334+
'yaxis': {'autorange': False,
1335+
'range': [-1, 3],
1336+
'showgrid': False,
1337+
'ticktext': ['Job A', 'Job B'],
1338+
'tickvals': [0, 1],
1339+
'zeroline': False}}
1340+
}
1341+
1342+
self.assertEqual(test_gantt_chart['data'][0],
1343+
exp_gantt_chart['data'][0])
1344+
1345+
self.assert_dict_equal(test_gantt_chart['layout'],
1346+
exp_gantt_chart['layout'])
1347+
1348+
12701349
class TestViolin(NumpyTestUtilsMixin, TestCase):
12711350

12721351
def test_colors_validation(self):

0 commit comments

Comments
 (0)