def test_do_not_remove_totals_for_rollup_dimensions(self):
        result = scrub_totals_from_share_results(dimx1_str_totals_df,
                                                 [Rollup(mock_dataset.fields.political_party)])

        expected = dimx1_str_totals_df

        pandas.testing.assert_frame_equal(result, expected)
    def test_do_not_remove_totals_for_rollup_dimensions_with_multiindex_and_all_rolled_up(self):
        result = scrub_totals_from_share_results(dimx2_date_str_totalsx2_df,
                                                 [Rollup(mock_dataset.fields.timestamp),
                                                  Rollup(mock_dataset.fields.political_party)])

        expected = dimx2_date_str_totalsx2_df

        pandas.testing.assert_frame_equal(result, expected)
    def test_do_not_remove_totals_for_rollup_dimensions_with_multiindex_and_lower_dimension_totals(self):
        result = scrub_totals_from_share_results(dimx2_date_str_totalsx2_df,
                                                 [mock_dataset.fields.timestamp,
                                                  Rollup(mock_dataset.fields.political_party)])

        expected = dimx2_date_str_totalsx2_df.loc[:TIMESTAMP_UPPERBOUND]

        pandas.testing.assert_frame_equal(result, expected)
    def test_remove_totals_for_non_rollup_dimensions_with_multiindex_and_multiple_totals(self):
        result = scrub_totals_from_share_results(dimx2_date_str_totalsx2_df,
                                                 [mock_dataset.fields.timestamp,
                                                  mock_dataset.fields.political_party])

        expected = dimx2_date_str_df

        pandas.testing.assert_frame_equal(result, expected)
    def test_do_not_remove_totals_for_rollup_dimensions_with_multiindex_and_higher_dimension_totals(self):
        result = scrub_totals_from_share_results(dimx2_date_str_totalsx2_df,
                                                 [Rollup(mock_dataset.fields.timestamp),
                                                  mock_dataset.fields.political_party])

        expected = dimx2_date_str_totalsx2_df.loc[(slice(None),
                                                   slice('Democrat', 'Republican')), :] \
            .append(dimx2_date_str_totalsx2_df.iloc[-1])

        pandas.testing.assert_frame_equal(result, expected)
Esempio n. 6
0
    def fetch(self, hint=None) -> Iterable[Dict]:
        """
        Fetch the data for this query and transform it into the widgets.

        :param hint:
            A query hint label used with database vendors which support it. Adds a label comment to the query.
        :return:
            A list of dict (JSON) objects containing the widget configurations.
        """
        queries = add_hints(self.sql, hint)

        operations = find_operations_for_widgets(self._widgets)
        share_dimensions = find_share_dimensions(self._dimensions, operations)

        data_frame = fetch_data(
            self.dataset.database,
            queries,
            self._dimensions,
            share_dimensions,
            self.reference_groups,
        )

        # Apply operations
        for operation in operations:
            for reference in [None] + self._references:
                df_key = alias_selector(reference_alias(operation, reference))
                data_frame[df_key] = operation.apply(data_frame, reference)

        data_frame = scrub_totals_from_share_results(data_frame,
                                                     self._dimensions)
        data_frame = special_cases.apply_operations_to_data_frame(
            operations, data_frame)
        data_frame = paginate(
            data_frame,
            self._widgets,
            orders=self.orders,
            limit=self._limit,
            offset=self._offset,
        )

        # Apply transformations
        return [
            widget.transform(data_frame, self.dataset, self._dimensions,
                             self._references) for widget in self._widgets
        ]
Esempio n. 7
0
    def fetch(self, hint=None) -> Union[Iterable[Dict], Dict]:
        """
        Fetch the data for this query and transform it into the widgets.

        :param hint:
            A query hint label used with database vendors which support it. Adds a label comment to the query.
        :return:
            A list of dict (JSON) objects containing the widget configurations.
        """
        queries = add_hints(self.sql, hint)

        operations = find_operations_for_widgets(self._widgets)
        dimensions = self.dimensions

        share_dimensions = find_share_dimensions(dimensions, operations)

        annotation_frame = None
        if dimensions and self.dataset.annotation:
            alignment_dimension_alias = self.dataset.annotation.dataset_alignment_field_alias
            first_dimension = find_field_in_modified_field(dimensions[0])

            if first_dimension.alias == alignment_dimension_alias:
                annotation_frame = self.fetch_annotation()

        max_rows_returned, data_frame = fetch_data(
            self.dataset.database,
            queries,
            dimensions,
            share_dimensions,
            self.reference_groups,
        )

        # Apply reference filters
        for reference in self._references:
            data_frame = apply_reference_filters(data_frame, reference)

        # Apply operations
        for operation in operations:
            for reference in [None] + self._references:
                df_key = alias_selector(reference_alias(operation, reference))
                data_frame[df_key] = operation.apply(data_frame, reference)

        data_frame = scrub_totals_from_share_results(data_frame, dimensions)
        data_frame = special_cases.apply_operations_to_data_frame(operations, data_frame)

        data_frame = paginate(
            data_frame,
            self._widgets,
            orders=self.orders,
            limit=self._client_limit,
            offset=self._client_offset,
        )

        # Apply transformations
        widget_data = [
            widget.transform(
                data_frame,
                dimensions,
                self._references,
                annotation_frame,
            )
            for widget in self._widgets
        ]

        return self._transform_for_return(widget_data, max_rows_returned=max_rows_returned)
    def ignore_dimensionless_result_sets(self):
        result = scrub_totals_from_share_results(dimx0_metricx2_df, [])

        expected = dimx0_metricx2_df

        pandas.testing.assert_frame_equal(result, expected)