Adding tests for the time table viz (#4659)

This commit is contained in:
michellethomas 2018-04-11 13:24:51 -07:00 committed by Maxime Beauchemin
parent e96590744f
commit 725b8f94c9
1 changed files with 80 additions and 0 deletions

View File

@ -636,3 +636,83 @@ class RoseVisTestCase(unittest.TestCase):
],
}
self.assertEqual(expected, res)
class TimeSeriesTableVizTestCase(unittest.TestCase):
def test_get_data_metrics(self):
form_data = {
'metrics': ['sum__A', 'count'],
'groupby': [],
}
datasource = Mock()
raw = {}
t1 = pd.Timestamp('2000')
t2 = pd.Timestamp('2002')
raw[DTTM_ALIAS] = [t1, t2]
raw['sum__A'] = [15, 20]
raw['count'] = [6, 7]
df = pd.DataFrame(raw)
test_viz = viz.TimeTableViz(datasource, form_data)
data = test_viz.get_data(df)
# Check method correctly transforms data
self.assertEqual(set(['count', 'sum__A']), set(data['columns']))
time_format = '%Y-%m-%d %H:%M:%S'
expected = {
t1.strftime(time_format): {
'sum__A': 15,
'count': 6,
},
t2.strftime(time_format): {
'sum__A': 20,
'count': 7,
},
}
self.assertEqual(expected, data['records'])
def test_get_data_group_by(self):
form_data = {
'metrics': ['sum__A'],
'groupby': ['groupby1'],
}
datasource = Mock()
raw = {}
t1 = pd.Timestamp('2000')
t2 = pd.Timestamp('2002')
raw[DTTM_ALIAS] = [t1, t1, t1, t2, t2, t2]
raw['sum__A'] = [15, 20, 25, 30, 35, 40]
raw['groupby1'] = ['a1', 'a2', 'a3', 'a1', 'a2', 'a3']
df = pd.DataFrame(raw)
test_viz = viz.TimeTableViz(datasource, form_data)
data = test_viz.get_data(df)
# Check method correctly transforms data
self.assertEqual(set(['a1', 'a2', 'a3']), set(data['columns']))
time_format = '%Y-%m-%d %H:%M:%S'
expected = {
t1.strftime(time_format): {
'a1': 15,
'a2': 20,
'a3': 25,
},
t2.strftime(time_format): {
'a1': 30,
'a2': 35,
'a3': 40,
},
}
self.assertEqual(expected, data['records'])
@patch('superset.viz.BaseViz.query_obj')
def test_query_obj_throws_metrics_and_groupby(self, super_query_obj):
datasource = Mock()
form_data = {
'groupby': ['a'],
}
super_query_obj.return_value = {}
test_viz = viz.TimeTableViz(datasource, form_data)
with self.assertRaises(Exception):
test_viz.query_obj()
form_data['metrics'] = ['x', 'y']
test_viz = viz.TimeTableViz(datasource, form_data)
with self.assertRaises(Exception):
test_viz.query_obj()