Ejemplo n.º 1
0
    def test_cost_summary_table(self):
        """Test that we select a valid table or view."""
        params = self.mocked_query_params("?", GCPForecastCostView)
        forecast = GCPForecast(params)
        self.assertEqual(forecast.cost_summary_table, GCPCostSummary)

        params = self.mocked_query_params("?", GCPForecastCostView, access={"gcp.account": {"read": ["1"]}})
        forecast = GCPForecast(params)
        self.assertEqual(forecast.cost_summary_table, GCPCostSummaryByAccount)

        params = self.mocked_query_params("?", GCPForecastCostView, access={"gcp.project": {"read": ["1"]}})
        forecast = GCPForecast(params)
        self.assertEqual(forecast.cost_summary_table, GCPCostSummaryByProject)

        params = self.mocked_query_params(
            "?", GCPForecastCostView, access={"gcp.account": {"read": ["1"]}, "gcp.project": {"read": ["1"]}}
        )
        forecast = GCPForecast(params)
        self.assertEqual(forecast.cost_summary_table, GCPCostSummaryByProject)

        params = self.mocked_query_params(
            "?", GCPForecastCostView, access={"gcp.account": {"read": ["1"]}, "gcp.project": {"read": ["1"]}}
        )

        forecast = GCPForecast(params)
        self.assertEqual(forecast.cost_summary_table, GCPCostSummaryByProject)
Ejemplo n.º 2
0
    def test_predict_flat(self):
        """Test that predict() returns expected values for flat costs."""
        dh = DateHelper()

        expected = []
        for n in range(0, 10):
            expected.append({
                "usage_start":
                (dh.this_month_start + timedelta(days=n)).date(),
                "total_cost":
                5 + (0.01 * n),
                "infrastructure_cost":
                3 + (0.01 * n),
                "supplementary_cost":
                2 + (0.01 * n),
            })
        mock_qset = MockQuerySet(expected)

        mocked_table = Mock()
        mocked_table.objects.filter.return_value.order_by.return_value.values.return_value.annotate.return_value = (  # noqa: E501
            mock_qset)
        mocked_table.len = mock_qset.len

        params = self.mocked_query_params("?", AzureCostForecastView)
        instance = GCPForecast(params)

        instance.cost_summary_table = mocked_table

        results = instance.predict()

        for result in results:
            for val in result.get("values", []):
                with self.subTest(values=val):
                    self.assertIsInstance(val.get("date"), date)

                    for item, cost, delta in [
                        (val.get("cost"), 5, 1),
                        (val.get("infrastructure"), 3, 1),
                        (val.get("supplementary"), 2, 1),
                    ]:
                        with self.subTest(cost=cost, delta=delta, item=item):
                            self.assertAlmostEqual(float(
                                item.get("total").get("value")),
                                                   cost,
                                                   delta=delta)
                            self.assertAlmostEqual(float(
                                item.get("confidence_max").get("value")),
                                                   cost,
                                                   delta=delta)
                            self.assertAlmostEqual(float(
                                item.get("confidence_min").get("value")),
                                                   cost,
                                                   delta=delta)
                            self.assertGreater(
                                float(item.get("rsquared").get("value")), 0)
                            for pval in item.get("pvalues").get("value"):
                                self.assertGreaterEqual(float(pval), 0)