def test_operations_evaluated_for_each_reference(self, mock_fetch_data: Mock, *mocks): eoe = ElectionOverElection(slicer.dimensions.timestamp) mock_operation = Mock(name='mock_operation ', spec=f.Operation) mock_operation.key, mock_operation.definition = 'mock_operation', slicer.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 slicer.data \ .dimension(slicer.dimensions.timestamp) \ .reference(eoe) \ .widget(mock_widget) \ .fetch() mock_operation.apply.assert_has_calls([ call(mock_df, None), call(mock_df, eoe), ])
def test_pass_slicer_database_as_arg(self, mock_fetch_data: Mock, *args): mock_widget = f.Widget(mock_dataset.fields.votes) mock_widget.transform = Mock() mock_dataset.query.widget(mock_widget).fetch() mock_fetch_data.assert_called_once_with(mock_dataset.database, ANY, ANY, ANY, ANY)
def test_builder_dimensions_as_arg_with_zero_dimensions( self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(mock_dataset.fields.votes) mock_widget.transform = Mock() mock_dataset.query.widget(mock_widget).fetch() mock_fetch_data.assert_called_once_with(ANY, ANY, [], ANY, ANY)
def test_returns_results_from_widget_transform(self, *args): mock_widget = f.Widget(mock_dataset.fields.votes) mock_widget.transform = Mock() # Need to keep widget the last call in the chain otherwise the object gets cloned and the assertion won't work result = mock_dataset.query.dimension( mock_dataset.fields.timestamp).widget(mock_widget).fetch() self.assertListEqual(result, [mock_widget.transform.return_value])
def test_pass_slicer_database_as_arg(self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(slicer.metrics.votes) mock_widget.transform = Mock() slicer.data \ .widget(mock_widget) \ .fetch() mock_fetch_data.assert_called_once_with(slicer.database, ANY, ANY, ANY, ANY)
def test_builder_dimensions_as_arg_with_zero_dimensions(self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(slicer.metrics.votes) mock_widget.transform = Mock() slicer.data \ .widget(mock_widget) \ .fetch() mock_fetch_data.assert_called_once_with(ANY, ANY, [], ANY, ANY)
def test_paginate_is_called(self, mock_fetch_data: Mock, mock_paginate: Mock, *mocks): mock_widget = f.Widget(slicer.metrics.votes) mock_widget.transform = Mock() # Need to keep widget the last call in the chain otherwise the object gets cloned and the assertion won't work slicer.data \ .dimension(slicer.dimensions.timestamp) \ .widget(mock_widget) \ .fetch() mock_paginate.assert_called_once_with(mock_fetch_data.return_value, [mock_widget], limit=None, offset=None, orders=[])
def test_returns_results_from_widget_transform(self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(slicer.metrics.votes) mock_widget.transform = Mock() # Need to keep widget the last call in the chain otherwise the object gets cloned and the assertion won't work result = slicer.data \ .dimension(slicer.dimensions.timestamp) \ .widget(mock_widget) \ .fetch() self.assertListEqual(result, [mock_widget.transform.return_value])
def test_builder_dimensions_as_arg_with_multiple_dimensions(self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(slicer.metrics.votes) 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, DimensionMatcher(*dimensions), ANY, ANY)
def test_find_share_dimensions_with_a_single_share_operation(self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(Share(slicer.metrics.votes, 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_builder_dimensions_as_arg_with_one_dimension( self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(mock_dataset.fields.votes) mock_widget.transform = Mock() dimensions = [mock_dataset.fields.state] mock_dataset.query.widget(mock_widget).dimension(*dimensions).fetch() mock_fetch_data.assert_called_once_with(ANY, ANY, FieldMatcher(*dimensions), ANY, ANY)
def test_call_transform_on_widget(self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(slicer.metrics.votes) mock_widget.transform = Mock() # Need to keep widget the last call in the chain otherwise the object gets cloned and the assertion won't work slicer.data \ .dimension(slicer.dimensions.timestamp) \ .widget(mock_widget) \ .fetch() mock_widget.transform.assert_called_once_with(mock_paginate.return_value, slicer, DimensionMatcher(slicer.dimensions.timestamp), [])
def test_find_no_share_dimensions_with_no_share_operation( self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(mock_dataset.fields.votes) 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, [], ANY)
def test_pass_query_from_builder_as_arg(self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(slicer.metrics.votes) mock_widget.transform = Mock() slicer.data \ .widget(mock_widget) \ .fetch() mock_fetch_data.assert_called_once_with(ANY, [PypikaQueryMatcher('SELECT SUM("votes") "$m$votes" ' 'FROM "politics"."politician"')], ANY, ANY, ANY)
def test_fetch_annotation_single_category_dimension(self, mock_fetch_data: Mock): dims = [mock_category_annotation_dataset.fields.political_party] widget = f.Widget(mock_category_annotation_dataset.fields.votes) widget.transform = Mock() mock_category_annotation_dataset.query.widget(widget).dimension(*dims).fetch() fetch_annotation_args, fetch_data_args = self.get_fetch_call_args( mock_fetch_data ) self.assertEqual( ( mock_date_annotation_dataset.database, [ PypikaQueryMatcher( "SELECT " '"political_party" "$political_party",' '"district_name" "$district-name" ' 'FROM "politics"."annotations" ' 'GROUP BY "$political_party","$district-name"' ) ], FieldMatcher( mock_category_annotation_dataset.annotation.alignment_field ), ), fetch_annotation_args, ) self.assertEqual( ( mock_category_annotation_dataset.database, [ PypikaQueryMatcher( "SELECT " '"political_party" "$political_party",' 'SUM("votes") "$votes" ' 'FROM "politics"."politician" ' 'GROUP BY "$political_party" ' 'ORDER BY "$political_party" ' 'LIMIT 200000' ) ], FieldMatcher(*dims), [], [], ), fetch_data_args, )
def test_pagination_applied_with_orders(self, mock_fetch_data: Mock, mock_paginate: Mock, *mocks): mock_widget = f.Widget(slicer.metrics.votes) mock_widget.transform = Mock() # Need to keep widget the last call in the chain otherwise the object gets cloned and the assertion won't work slicer.data \ .dimension(slicer.dimensions.timestamp) \ .widget(mock_widget) \ .orderby(slicer.metrics.votes, Order.asc) \ .fetch() votes_definition_with_alias_matcher = PypikaQueryMatcher(fn.Sum(slicer.table.votes).as_('$d$votes')) orders = [(votes_definition_with_alias_matcher, Order.asc)] mock_paginate.assert_called_once_with(mock_fetch_data.return_value, [mock_widget], limit=None, offset=None, orders=orders)
def test_envelopes_responses_if_return_additional_metadata_True( self, *args): dataset = copy.deepcopy(mock_dataset) mock_widget = f.Widget(dataset.fields.votes) mock_widget.transform = Mock() dataset.return_additional_metadata = True result = dataset.query.dimension( dataset.fields.timestamp).widget(mock_widget).fetch() self.assertEqual( dict(data=[mock_widget.transform.return_value], metadata=dict(max_rows_returned=100)), result, )
def test_builder_dimensions_as_arg_with_a_non_replaced_set_dimension( self, mock_fetch_data: Mock, *args ): mock_widget = f.Widget(mock_dataset.fields.votes) mock_widget.transform = Mock() dimensions = [mock_dataset.fields.state] set_filter = f.ResultSet(dimensions[0]=='On', will_replace_referenced_dimension=False) mock_dataset.query.widget(mock_widget).dimension(*dimensions).filter(set_filter).fetch() set_dimension = _make_set_dimension(set_filter, mock_dataset) mock_fetch_data.assert_called_once_with( ANY, ANY, FieldMatcher(set_dimension, dimensions[0]), ANY, ANY )
def test_paginate_is_called(self, mock_fetch_data: Mock, mock_paginate: Mock, *mocks): mock_widget = f.Widget(mock_dataset.fields.votes) mock_widget.transform = Mock() # 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).widget(mock_widget).fetch() mock_paginate.assert_called_once_with( mock_fetch_data.return_value, [mock_widget], limit=None, offset=None, orders=[(mock_dataset.fields.timestamp, None)], )
def test_call_transform_on_widget(self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(mock_dataset.fields.votes) mock_widget.transform = Mock() # 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).widget(mock_widget).fetch() mock_widget.transform.assert_called_once_with( mock_paginate.return_value, mock_dataset, FieldMatcher(mock_dataset.fields.timestamp), [], )
def test_operations_evaluated(self, mock_fetch_data: Mock, *mocks): 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 = 100, 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).widget(mock_widget).fetch() mock_operation.apply.assert_called_once_with(mock_df, None)
def test_builder_dimensions_as_arg_with_multiple_dimensions( self, mock_fetch_data: Mock, mock_paginate: Mock): mock_widget = f.Widget(mock_dataset.fields.votes) 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, FieldMatcher(*dimensions), ANY, ANY)
def test_pass_query_from_builder_as_arg(self, mock_fetch_data: Mock, *args): mock_widget = f.Widget(mock_dataset.fields.votes) mock_widget.transform = Mock() mock_dataset.query.widget(mock_widget).fetch() mock_fetch_data.assert_called_once_with( ANY, [ PypikaQueryMatcher( 'SELECT SUM("votes") "$votes" FROM "politics"."politician" ORDER BY 1 LIMIT 200000' ) ], ANY, ANY, ANY, )
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_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_operations_results_stored_in_data_frame(self, mock_fetch_data: Mock, *mocks): 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 = 100, 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).widget(mock_widget).fetch() f_op_key = alias_selector(mock_operation.alias) self.assertIn(f_op_key, mock_df) self.assertEqual(mock_df[f_op_key], mock_operation.apply.return_value)
def test_operations_results_stored_in_data_frame(self, mock_fetch_data: Mock, *mocks): mock_operation = Mock(name='mock_operation ', spec=f.Operation) mock_operation.key, mock_operation.definition = 'mock_operation', slicer.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 slicer.data \ .dimension(slicer.dimensions.timestamp) \ .widget(mock_widget) \ .fetch() f_op_key = format_metric_key(mock_operation.key) self.assertIn(f_op_key, mock_df) self.assertEqual(mock_df[f_op_key], mock_operation.apply.return_value)
def setUpClass(cls): cls.mock_widget = f.Widget(mock_date_annotation_dataset.fields.votes) cls.mock_widget.transform = Mock()