def test_apply_to_two_dims_over_second(self): share = Share(mock_dataset.fields.votes, over=mock_dataset.fields.political_party) result = share.apply(dimx2_date_str_totals_df, None) f_metric_key = alias_selector(mock_dataset.fields.votes.alias) expected = pd.Series( [ 49.79, 7.07, 43.12, 100.0, 49.78, 50.21, 100.0, 48.83, 51.16, 100.0, 55.42, 44.57, 100.0, 60.39, 39.60, 100.0, 26.60, 73.39, 100.0, ], name=f_metric_key, index=dimx2_date_str_totals_df.index, ) pandas.testing.assert_series_equal(expected, result, rtol=0.5e-3)
def test_apply_to_zero_dims(self): share = Share(mock_dataset.fields.votes) result = share.apply(dimx0_metricx1_df, None) f_metric_key = alias_selector(mock_dataset.fields.votes.alias) expected = pd.Series([100.], name=f_metric_key) pandas.testing.assert_series_equal(expected, result)
def test_apply_to_zero_dims(self): share = Share(slicer.metrics.votes) result = share.apply(single_metric_df, None) f_metric_key = format_metric_key(slicer.metrics.votes.key) expected = pd.Series([100.], name=f_metric_key) pandas.testing.assert_series_equal(expected, result)
def test_apply_to_two_dims_over_first(self): share = Share(mock_dataset.fields.votes, over=mock_dataset.fields.timestamp) result = share.apply(dimx2_date_str_totalsx2_df, None) f_metric_key = alias_selector(mock_dataset.fields.votes.alias) metric_series = dimx2_date_str_totalsx2_df[f_metric_key] expected = 100 * metric_series / metric_series.iloc[-1] pandas.testing.assert_series_equal(expected, result, rtol=0.5e-3)
def test_apply_to_one_dim_over_none(self): share = Share(mock_dataset.fields.votes) result = share.apply(dimx1_str_df, None) f_metric_key = alias_selector(mock_dataset.fields.votes.alias) expected = pd.Series([100.] * 3, name=f_metric_key, index=dimx1_str_df.index) pandas.testing.assert_series_equal(expected, result, rtol=0.5e-3)
def test_apply_to_one_dim_over_first(self): share = Share(mock_dataset.fields.votes, over=mock_dataset.fields.political_party) result = share.apply(dimx1_str_totals_df, None) f_metric_key = alias_selector(mock_dataset.fields.votes.alias) expected = pd.Series([48.8487, 0.9638, 50.1873, 100.0], name=f_metric_key, index=dimx1_str_totals_df.index) pandas.testing.assert_series_equal(expected, result, rtol=0.5e-3)
def test_apply_to_two_dims_over_first(self): share = Share(slicer.metrics.votes, over=slicer.dimensions.timestamp) result = share.apply(cont_uni_dim_all_totals_df, None) f_metric_key = format_metric_key(slicer.metrics.votes.key) metric_series = cont_uni_dim_all_totals_df[f_metric_key] expected = 100 * metric_series / metric_series.iloc[-1] pandas.testing.assert_series_equal(expected, result, check_less_precise=True)
def test_apply_to_two_dims_over_none(self): share = Share(mock_dataset.fields.votes) result = share.apply(dimx2_date_str_df, None) f_metric_key = alias_selector(mock_dataset.fields.votes.alias) expected = pd.Series([100.] * 13, name=f_metric_key, index=dimx2_date_str_df.index) pandas.testing.assert_series_equal(expected, result, check_less_precise=True)
def test_apply_to_one_dim_over_none(self): share = Share(slicer.metrics.votes) result = share.apply(cat_dim_df, None) f_metric_key = format_metric_key(slicer.metrics.votes.key) expected = pd.Series([100.] * 3, name=f_metric_key, index=cat_dim_df.index) pandas.testing.assert_series_equal(expected, result, check_less_precise=True)
def test_find_share_dimensions_with_a_multiple_share_operations(self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(Share(slicer.metrics.votes, over=slicer.dimensions.state), Share(slicer.metrics.wins, over=slicer.dimensions.state)) mock_widget.transform = Mock() dimensions = slicer.dimensions.timestamp, slicer.dimensions.state, slicer.dimensions.political_party slicer.data \ .widget(mock_widget) \ .dimension(*dimensions) \ .fetch() mock_fetch_data.assert_called_once_with(ANY, ANY, ANY, DimensionMatcher(slicer.dimensions.state), ANY)
def test_apply_to_one_dim_over_first(self): share = Share(slicer.metrics.votes, over=slicer.dimensions.political_party) result = share.apply(cat_dim_totals_df, None) f_metric_key = format_metric_key(slicer.metrics.votes.key) expected = pd.Series([48.849, 0.964, 50.187, 100.0], name=f_metric_key, index=cat_dim_totals_df.index) pandas.testing.assert_series_equal(expected, result, check_less_precise=True)
def test_apply_to_two_dims_over_second_with_one_row_per_group(self): raw_df = dimx2_date_str_totals_df.iloc[[0, 3, 4, 6]] share = Share(mock_dataset.fields.votes, over=mock_dataset.fields.political_party) result = share.apply(raw_df, None) f_metric_key = alias_selector(mock_dataset.fields.votes.alias) expected = pd.Series([49.79, 100.0, 49.78, 100.0], name=f_metric_key, index=raw_df.index) pandas.testing.assert_series_equal(expected, result, rtol=0.5e-3)
def test_apply_to_two_dims_over_second_with_one_row_per_group(self): raw_df = cont_uni_dim_totals_df.iloc[[0, 2, 3, 5]] share = Share(slicer.metrics.votes, over=slicer.dimensions.state) result = share.apply(raw_df, None) f_metric_key = format_metric_key(slicer.metrics.votes.key) expected = pd.Series([36.624, 100., 37.411, 100.], name=f_metric_key, index=raw_df.index) pandas.testing.assert_series_equal(expected, result, check_less_precise=True)
def test_apply_to_two_dims_over_second(self): share = Share(slicer.metrics.votes, over=slicer.dimensions.state) result = share.apply(cont_uni_dim_totals_df, None) f_metric_key = format_metric_key(slicer.metrics.votes.key) expected = pd.Series([ 36.624, 63.376, 100., 37.411, 62.589, 100., 37.521, 62.479, 100., 37.606, 62.394, 100., 38.294, 61.706, 100., 27.705, 72.295, 100. ], name=f_metric_key, index=cont_uni_dim_totals_df.index) pandas.testing.assert_series_equal(expected, result, check_less_precise=True)
def test_find_share_dimensions_with_a_multiple_share_operations( self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget( Share(mock_dataset.fields.votes, over=mock_dataset.fields.state), Share(mock_dataset.fields.wins, over=mock_dataset.fields.state), ) mock_widget.transform = Mock() dimensions = ( mock_dataset.fields.timestamp, mock_dataset.fields.state, mock_dataset.fields.political_party, ) mock_dataset.query.widget(mock_widget).dimension(*dimensions).fetch() mock_fetch_data.assert_called_once_with( ANY, ANY, ANY, FieldMatcher(mock_dataset.fields.state), ANY)
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)