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_converts_percentage_to_decimal_when_use_raw_value_True_and_suffix_is_percentage(self): with self.subTest('when no precision'): field = Field("number", None, data_type=DataType.number, suffix="%") self.assertEqual("0.87123123131", formats.display_value(87.123123131, field, use_raw_value=True)) with self.subTest('when precision'): field = Field("number", None, data_type=DataType.number, suffix="%", precision=2) self.assertEqual("0.0739", formats.display_value(7.38652, field, use_raw_value=True))
def test_style_numbers_with_thousands_separator(self): euro_field = Field("number", None, data_type=DataType.number, thousands=",") self.assertEqual("1,000,000", formats.display_value(1000000, euro_field))
def test_raw_value_when_precision_specified(self): field = Field("number", None, data_type=DataType.number, precision=4) self.assertEqual( "1244996.1138", formats.display_value(1244996.1138000000000000, field, use_raw_value=True))
def test_envelopes_responses_if_return_additional_metadata_True( self, mock_fetch_data): mock_dataset = DataSet( table=politicians_table, database=test_database, return_additional_metadata=True, fields=[ Field( "political_party", label="Party", definition=politicians_table.political_party, 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 = mock_dataset.fields.political_party.choices.fetch() self.assertEqual(dict(max_rows_returned=100), result['metadata']) self.assertTrue( pd.Series(['a', 'b', 'c'], index=['a', 'b', 'c'], name='political_party').equals(result['data']))
def test_use_raw_value_int(self): field = Field("number", None, data_type=DataType.number, suffix="€", prefix="$") self.assertEqual( "1234", formats.display_value(1234, field, use_raw_value=True))
def test_style_numbers_with_mixed(self): euro_field = Field( "number", None, data_type=DataType.number, prefix="$", thousands=",", precision=2, ) self.assertEqual("$-1,000,000.00", formats.display_value(-1000000, euro_field))
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())
def test_with_empty_dataset_multiple_dimensions(self): metric = Field("test", None, label="Test") mi = pd.MultiIndex.from_product([[], []], names=("level0", "level1")) frame = pd.DataFrame([], index=mi, columns=["$test"], dtype=np.float) expected = pd.Series([], index=mi, name="$test", dtype=np.float) for op in (CumSum, CumMean, CumProd): with self.subTest(op.__name__): op_cum = op(metric) result = op_cum.apply(frame, None) pd.testing.assert_series_equal(result, expected)
def test_with_empty_dataset_single_dimension(self): metric = Field("test", None, label="Test") idx = pd.Index([], name="level0") frame = pd.DataFrame([], index=idx, columns=["$test"], dtype=np.float) expected = pd.Series([], index=idx, name="$test", dtype=np.float) for op in (CumSum, CumMean, CumProd): with self.subTest(op.__name__): op_cum = op(metric) result = op_cum.apply(frame, None) pd.testing.assert_series_equal(result, expected)
def test_share_for_references_with_delta(self): dataset = MagicMock() dataset.table._table_name = "table" value_field = Field("value", None) over_field = Field("dim-over", None) share = Share(value_field, over_field) reference = Reference(value_field, WeekOverWeek, delta=True, delta_percent=False) df = pd.DataFrame.from_dict({ "$value_wow": [10, 15, 20, 5, 50], "$value": [12, 16, 14, 8, 50], "$share(value,dim-over)": [24, 32, 28, 16, 100], "$dim-over": ["A", "B", "C", "D", "~~totals"] }).set_index('$dim-over') result = share.apply(df, reference) np.testing.assert_array_equal(([4, 2, -12, 6, 0]), result.values)
def test_apply_cummulative_for_delta_percent(self): dataset = MagicMock() dataset.table._table_name = "table" field = Field("value", None) cumsum = CumProd(field) reference = Reference(field, WeekOverWeek, delta=True, delta_percent=True) df = pd.DataFrame.from_dict({ "$value": [55, 60, 108], "$cumprod(value)": [55, 3300, 356400], "$value_wow_delta_percent": [10, 20, 8], }) result = cumsum.apply(df, reference) pandas.testing.assert_series_equal(pd.Series([10.0, 32.0, 42.56]), result)
def test_raw_value_does_not_trim_zero_value(self): field = Field("number", None, data_type=DataType.number) self.assertEqual("0", formats.display_value(0, field, use_raw_value=True))
def test_raw_value_formats_integers_with_trailing_zeros(self): field = Field("number", None, data_type=DataType.number) self.assertEqual( "126500", formats.display_value(126500, field, use_raw_value=True))
def test_style_numbers_with_precision(self): euro_field = Field("number", None, data_type=DataType.number, precision=2) self.assertEqual("1.23", formats.display_value(1.234567, euro_field))
def test_style_numbers_with_suffix(self): euro_field = Field("number", None, data_type=DataType.number, suffix="€") self.assertEqual("1€", formats.display_value(1, euro_field))
def test_style_negative_numbers_with_prefix(self): dollar_field = Field("number", None, data_type=DataType.number, prefix="$") self.assertEqual("$-1", formats.display_value(-1, dollar_field))
Field, day, formats, hour, month, quarter, week, year, ) from fireant.dataset.totals import ( DATE_TOTALS, NUMBER_TOTALS, TEXT_TOTALS, ) text_field = Field("text", None, data_type=DataType.text) number_field = Field("number", None, data_type=DataType.number) boolean_field = Field("boolean", None, data_type=DataType.boolean) date_field = Field("date", None, data_type=DataType.date) class SafeRawValueTests(TestCase): def test_none_returned_as_null_label(self): self.assertEqual("null", formats.safe_value(None)) def test_nan_returned_as_null_label(self): self.assertEqual("null", formats.safe_value(np.nan)) def test_inf_returned_as_inf_label(self): with self.subTest("positive inf"): self.assertEqual("inf", formats.safe_value(np.inf))