Example #1
0
 def test_query_obj_throws_columns_and_metrics(self, super_query_obj):
     datasource = Mock()
     form_data = {
         'all_columns': ['A', 'B'],
         'metrics': ['x', 'y'],
     }
     super_query_obj.return_value = {}
     test_viz = viz.TableViz(datasource, form_data)
     with self.assertRaises(Exception):
         test_viz.query_obj()
     del form_data['metrics']
     form_data['groupby'] = ['B', 'C']
     test_viz = viz.TableViz(datasource, form_data)
     with self.assertRaises(Exception):
         test_viz.query_obj()
Example #2
0
 def test_query_obj_merges_all_columns(self, super_query_obj):
     datasource = Mock()
     form_data = {
         'all_columns': ['colA', 'colB', 'colC'],
         'order_by_cols': ['["colA", "colB"]', '["colC"]'],
     }
     super_query_obj.return_value = {
         'columns': ['colD', 'colC'],
         'groupby': ['colA', 'colB'],
     }
     test_viz = viz.TableViz(datasource, form_data)
     query_obj = test_viz.query_obj()
     self.assertEqual(form_data['all_columns'], query_obj['columns'])
     self.assertEqual([], query_obj['groupby'])
     self.assertEqual([['colA', 'colB'], ['colC']], query_obj['orderby'])
Example #3
0
 def test_query_obj_merges_percent_metrics(self, super_query_obj):
     datasource = Mock()
     form_data = {
         'percent_metrics': ['sum__A', 'avg__B', 'max__Y'],
         'metrics': ['sum__A', 'count', 'avg__C'],
     }
     test_viz = viz.TableViz(datasource, form_data)
     f_query_obj = {
         'metrics': form_data['metrics'],
     }
     super_query_obj.return_value = f_query_obj
     query_obj = test_viz.query_obj()
     self.assertEqual([
         'sum__A', 'count', 'avg__C',
         'avg__B', 'max__Y',
     ], query_obj['metrics'])
Example #4
0
 def test_query_obj_uses_sortby(self, super_query_obj):
     datasource = Mock()
     form_data = {
         'timeseries_limit_metric': '__time__',
         'order_desc': False,
     }
     super_query_obj.return_value = {
         'metrics': ['colA', 'colB'],
     }
     test_viz = viz.TableViz(datasource, form_data)
     query_obj = test_viz.query_obj()
     self.assertEqual([
         'colA', 'colB', '__time__',
     ], query_obj['metrics'])
     self.assertEqual([(
         '__time__', True,
     )], query_obj['orderby'])
Example #5
0
 def test_get_data_applies_percentage(self):
     form_data = {
         'percent_metrics': ['sum__A', 'avg__B'],
         'metrics': ['sum__A', 'count', 'avg__C'],
     }
     datasource = Mock()
     raw = {}
     raw['sum__A'] = [15, 20, 25, 40]
     raw['avg__B'] = [10, 20, 5, 15]
     raw['avg__C'] = [11, 22, 33, 44]
     raw['count'] = [6, 7, 8, 9]
     raw['groupA'] = ['A', 'B', 'C', 'C']
     raw['groupB'] = ['x', 'x', 'y', 'z']
     df = pd.DataFrame(raw)
     test_viz = viz.TableViz(datasource, form_data)
     data = test_viz.get_data(df)
     # Check method correctly transforms data and computes percents
     self.assertEqual(set([
         'groupA', 'groupB', 'count',
         'sum__A', 'avg__C',
         '%sum__A', '%avg__B',
     ]), set(data['columns']))
     expected = [
         {
             'groupA': 'A', 'groupB': 'x',
             'count': 6, 'sum__A': 15, 'avg__C': 11,
             '%sum__A': 0.15, '%avg__B': 0.2,
         },
         {
             'groupA': 'B', 'groupB': 'x',
             'count': 7, 'sum__A': 20, 'avg__C': 22,
             '%sum__A': 0.2, '%avg__B': 0.4,
         },
         {
             'groupA': 'C', 'groupB': 'y',
             'count': 8, 'sum__A': 25, 'avg__C': 33,
             '%sum__A': 0.25, '%avg__B': 0.1,
         },
         {
             'groupA': 'C', 'groupB': 'z',
             'count': 9, 'sum__A': 40, 'avg__C': 44,
             '%sum__A': 0.40, '%avg__B': 0.3,
         },
     ]
     self.assertEqual(expected, data['records'])
Example #6
0
 def test_should_be_timeseries_raises_when_no_granularity(self):
     datasource = Mock()
     form_data = {'include_time': True}
     test_viz = viz.TableViz(datasource, form_data)
     with self.assertRaises(Exception):
         test_viz.should_be_timeseries()