def test_operations_evaluated_for_each_reference(self, mock_fetch_data: Mock, *mocks): eoe = ElectionOverElection(mock_dataset.fields.timestamp) mock_operation = Mock(name='mock_operation ', spec=f.Operation) mock_operation.alias, mock_operation.definition = 'mock_operation', mock_dataset.table.abc mock_operation.metrics = [] mock_widget = f.Widget(mock_operation) mock_widget.transform = Mock() mock_df = {} mock_fetch_data.return_value = mock_df # Need to keep widget the last call in the chain otherwise the object gets cloned and the assertion won't work mock_dataset.query \ .dimension(mock_dataset.fields.timestamp) \ .reference(eoe) \ .widget(mock_widget) \ .fetch() mock_operation.apply.assert_has_calls([ call(mock_df, None), call(mock_df, eoe), ])
def test_apply_to_timeseries_with_uni_dim_and_ref(self): rolling_mean = RollingMean(mock_dataset.fields.wins, 3) result = rolling_mean.apply(dimx2_date_str_ref_df, ElectionOverElection(mock_dataset.fields.timestamp)) expected = pd.Series([nan, nan, nan, nan, 4 / 3, 0., 4 / 3, 2 / 3, 4 / 3, 4 / 3, 2 / 3], name='$wins_eoe', index=dimx2_date_str_ref_df.index) pandas.testing.assert_series_equal(expected, result)
def test_apply_to_timeseries_with_uni_dim_and_ref(self): cumprod = CumProd(mock_dataset.fields.wins) result = cumprod.apply( dimx2_date_str_ref_df, ElectionOverElection(mock_dataset.fields.timestamp)) expected = pd.Series([2.0] + [0.0] * 10, name='$wins_eoe', index=dimx2_date_str_ref_df.index) pandas.testing.assert_series_equal(expected, result)
def test_time_series_ref(self): dimensions = [mock_dataset.fields.timestamp, mock_dataset.fields.political_party] references = [ElectionOverElection(mock_dataset.fields.timestamp)] result = Pandas(mock_dataset.fields.votes).transform(dimx2_date_str_ref_df, dimensions, references) expected = dimx2_date_str_ref_df.copy()[[f('votes'), f('votes_eoe')]] expected.index.names = ['Timestamp', 'Party'] expected.columns = ['Votes', 'Votes EoE'] expected.columns.name = 'Metrics' expected = expected.applymap(format_float) pandas.testing.assert_frame_equal(expected, result)
def test_time_series_multi_ref(self): query_dimensions = [mock_dataset.fields.timestamp, mock_dataset.fields.political_party] query_references = [ElectionOverElection(mock_dataset.fields.timestamp)] result = CSV(mock_dataset.fields.votes, mock_dataset.fields.wins) \ .transform(dimx2_date_str_ref_df, mock_dataset, query_dimensions, query_references) expected = dimx2_date_str_ref_df.copy()[[f('votes'), f('votes_eoe'), f('wins'), f('wins_eoe')]] expected.index.names = ['Timestamp', 'Party'] expected.columns = ['Votes', 'Votes EoE', 'Wins', 'Wins EoE'] expected = expected.applymap(_format_float) self.assertEqual(expected.to_csv(**csv_options), result)
def test_apply_to_timeseries_with_uni_dim_and_ref(self): cummean = CumMean(mock_dataset.fields.votes) result = cummean.apply( dimx2_date_str_ref_df, ElectionOverElection(mock_dataset.fields.timestamp)) expected = pd.Series([ 7579518.0, 1076384.0, 7072032.5, 4685666.5, 7503711.0, 6316507.333333333, 8136969.0, 7688157.0, 8407797.0, 8635351.2, 8364511.166666667 ], name='$votes_eoe', index=dimx2_date_str_ref_df.index) pandas.testing.assert_series_equal(expected, result)
def test_hidden_ref_dimx2_date_str(self): dimensions = [ mock_dataset.fields.timestamp, mock_dataset.fields.political_party ] references = [ElectionOverElection(mock_dataset.fields.timestamp)] result = CSV(mock_dataset.fields.votes, hide=['votes_eoe']).transform(dimx2_date_str_ref_df, dimensions, references) expected = dimx2_date_str_ref_df.copy()[[f('votes')]] expected.index.names = ['Timestamp', 'Party'] expected.columns = ['Votes'] expected.columns.name = 'Metrics' expected = expected.applymap(format_float_raw) self.assertEqual(expected.to_csv(**csv_options), result)