Beispiel #1
0
    def test_reference_filter_with_2_filters_combined(self):
        reference_filter_gt = ReferenceFilter(self.dataset.fields.metric0, ComparisonOperator.gt, 3)
        reference_filter_lt = ReferenceFilter(self.dataset.fields.metric0, ComparisonOperator.lt, 10)
        reference = DayOverDay(
            self.dataset.fields.timestamp,
            filters=[reference_filter_gt, reference_filter_lt],
        )

        result_df = apply_reference_filters(self.df, reference)

        pd.testing.assert_frame_equal(
            result_df.reset_index(drop=True),
            pd.DataFrame.from_dict({"$metric0": [2, 3], "$metric0_dod": [5, 9]}),
        )
Beispiel #2
0
    def test_reference_filter_for_an_unselected_metric(self):
        reference_filter = ReferenceFilter(self.dataset.fields.unused_metric, ComparisonOperator.gt, 5)
        reference = DayOverDay(self.dataset.fields.timestamp, filters=[reference_filter])

        result_df = apply_reference_filters(self.df, reference)

        pd.testing.assert_frame_equal(result_df, self.df)
Beispiel #3
0
    def test_reference_filter_with_not_equal_to(self):
        reference_filter = ReferenceFilter(self.dataset.fields.metric0, ComparisonOperator.ne, 5)
        reference = DayOverDay(self.dataset.fields.timestamp, filters=[reference_filter])

        result_df = apply_reference_filters(self.df, reference)

        pd.testing.assert_frame_equal(
            result_df.reset_index(drop=True),
            pd.DataFrame.from_dict({"$metric0": [1, 3, 4], "$metric0_dod": [1, 9, 12]}),
        )
Beispiel #4
0
    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)