def test_envelopes_responses_if_return_additional_metadata_True( self, mock_fetch_data): dataset = DataSet( table=politicians_table, database=test_database, return_additional_metadata=True, fields=[ Field( "timestamp1", label="timestamp1", definition=politicians_table.timestamp1, data_type=DataType.text, hyperlink_template="http://example.com/{political_party}", ) ], ) df = pd.DataFrame({ 'political_party': ['a', 'b', 'c'] }).set_index('political_party') mock_fetch_data.return_value = 100, df result = dataset.latest(dataset.fields.timestamp1).fetch() self.assertEqual(dict(max_rows_returned=100), result['metadata']) self.assertTrue(result['data'].equals( pd.Series(['a'], index=['political_party'])))
def setUpClass(cls): db = TestDatabase() t0 = Table("test0") cls.dataset = DataSet( table=t0, database=db, fields=[ Field( "timestamp", label="Timestamp", definition=t0.timestamp, data_type=DataType.date, ), Field( "metric0", label="Metric0", definition=t0.metric, data_type=DataType.number, ), Field( "unused_metric", label="Unused Metric", definition=t0.unused_metric, data_type=DataType.number, ), ], ) cls.df = pd.DataFrame.from_dict({ "$metric0": [1, 2, 3, 4], "$metric0_dod": [1, 5, 9, 12] })
def test_reference_filters_are_applied(self, mock_fetch: Mock, mock_2: Mock, mock_apply_reference_filters: Mock): db = TestDatabase() t0 = Table("test0") dataset = DataSet( table=t0, database=db, fields=[ Field( "timestamp", label="Timestamp", definition=t0.timestamp, data_type=DataType.date, ), Field( "metric0", label="Metric0", definition=t0.metric, data_type=DataType.number, ), ], ) mock_widget = f.Widget(dataset.fields.metric0) mock_widget.transform = Mock() reference_filter = ReferenceFilter( dataset.fields.metric0, ComparisonOperator.gt, 5 ) reference = f.DayOverDay(dataset.fields.timestamp, filters=[reference_filter]) df = pd.DataFrame.from_dict({"$value": [1]}) mock_fetch.return_value = 100, df mock_apply_reference_filters.return_value = df ( dataset.query() .dimension(dataset.fields.timestamp) .widget(mock_widget) .reference(reference) ).fetch() mock_apply_reference_filters.assert_called_once_with(df, reference)
def test_pivoted_df_transformation_formats_totals_correctly(self): test_table = Table('test') ds = DataSet( table=test_table, database=test_database, fields=[ Field('date', label='Date', definition=test_table.date, data_type=DataType.date), Field('locale', label='Locale', definition=test_table.locale, data_type=DataType.text), Field('company', label='Company', definition=test_table.text, data_type=DataType.text), Field('metric1', label='Metric1', definition=Sum(test_table.number), data_type=DataType.number), Field('metric2', label='Metric2', definition=Sum(test_table.number), data_type=DataType.number), ], ) df = pd.DataFrame.from_dict( { '$metric1': {('~~totals', '~~totals'): 3, ('za', '~~totals'): 3, ('za', 'C1'): 2, ('za', 'C2'): 1}, '$metric2': {('~~totals', '~~totals'): 4, ('za', '~~totals'): 4, ('za', 'C1'): 2, ('za', 'C2'): 2}, } ) df.index.names = [f(ds.fields.locale.alias), f(ds.fields.company.alias)] result = Pandas(ds.fields.metric1, ds.fields.metric2, pivot=[ds.fields.company]).transform( df, [Rollup(ds.fields.locale), Rollup(ds.fields.company)], [], use_raw_values=True ) self.assertEqual(['Metrics', 'Company'], list(result.columns.names)) self.assertEqual( [ ('Metric1', 'C1'), ('Metric1', 'C2'), ('Metric1', 'Totals'), ('Metric2', 'C1'), ('Metric2', 'C2'), ('Metric2', 'Totals'), ], result.columns.values.tolist(), ) self.assertEqual(['Locale'], list(result.index.names)) self.assertEqual(['za', 'Totals'], result.index.values.tolist()) self.assertEqual([['2', '1', '3', '2', '2', '4'], ['', '', '3', '', '', '4']], result.values.tolist())