示例#1
0
 def test_query_params_invalid_nested_fields(self):
     """Test parse of query params for invalid nested_fields."""
     query_params = {
         "group_by": {
             "invalid": ["invalid"]
         },
         "order_by": {
             "cost": "asc"
         },
         "filter": {
             "resolution": "daily",
             "time_scope_value": "-10",
             "time_scope_units": "day",
             "resource_scope": [],
         },
     }
     serializer = QueryParamSerializer(data=query_params)
     with self.assertRaises(serializers.ValidationError):
         serializer.is_valid(raise_exception=True)
示例#2
0
 def test_query_params_invalid_nested_fields(self):
     """Test parse of query params for invalid nested_fields."""
     query_params = {
         'group_by': {
             'invalid': ['invalid']
         },
         'order_by': {
             'cost': 'asc'
         },
         'filter': {
             'resolution': 'daily',
             'time_scope_value': '-10',
             'time_scope_units': 'day',
             'resource_scope': []
         }
     }
     serializer = QueryParamSerializer(data=query_params)
     with self.assertRaises(serializers.ValidationError):
         serializer.is_valid(raise_exception=True)
示例#3
0
 def test_multiple_group_by_valid_with_or(self):
     """Test for valid key on special case account alias with or group_by parameters."""
     query_params = {
         "group_by": {
             "or:account": ["account1"],
             "or:project": ["project1"]
         },
         "order_by": {
             "account_alias": "asc"
         },
         "filter": {
             "resolution": "daily",
             "time_scope_value": "-10",
             "time_scope_units": "day",
             "resource_scope": [],
         },
     }
     serializer = QueryParamSerializer(data=query_params)
     with self.assertRaises(serializers.ValidationError):
         serializer.is_valid(raise_exception=True)
示例#4
0
 def test_multiple_group_by_error_invalid_key(self):
     """Test error when invalid order_by parameter is passed."""
     query_params = {
         "group_by": {
             "or:account": "*",
             "or:project": "*"
         },
         "order_by": {
             "region": "asc"
         },
         "filter": {
             "resolution": "daily",
             "time_scope_value": "-10",
             "time_scope_units": "day",
             "resource_scope": [],
         },
     }
     serializer = QueryParamSerializer(data=query_params)
     with self.assertRaises(serializers.ValidationError):
         serializer.is_valid(raise_exception=True)
示例#5
0
 def test_multiple_group_by(self):
     """Test parse of query params with multiple group_bys."""
     query_params = {
         "group_by": {
             "account": ["account1"],
             "project": ["project1"]
         },
         "order_by": {
             "cost": "asc"
         },
         "filter": {
             "resolution": "daily",
             "time_scope_value": "-10",
             "time_scope_units": "day",
             "resource_scope": [],
         },
     }
     serializer = QueryParamSerializer(data=query_params)
     with self.assertRaises(serializers.ValidationError):
         serializer.is_valid(raise_exception=True)
示例#6
0
 def test_multiple_group_by(self):
     """Test parse of query params with multiple group_bys."""
     query_params = {
         'group_by': {
             'account': ['account1'],
             'project': ['project1']
         },
         'order_by': {
             'cost': 'asc'
         },
         'filter': {
             'resolution': 'daily',
             'time_scope_value': '-10',
             'time_scope_units': 'day',
             'resource_scope': []
         }
     }
     serializer = QueryParamSerializer(data=query_params)
     with self.assertRaises(serializers.ValidationError):
         serializer.is_valid(raise_exception=True)
示例#7
0
    def test_parse_filter_dates_invalid_resolution(self):
        """Test parse of a filter date-based param with monthly presolution should not succeed."""
        dh = DateHelper()
        scenarios = [
            {
                "start_date": dh.last_month_end.date(),
                "end_date": dh.this_month_start.date(),
                "filter": {"resolution": "monthly"},
            },
            {
                "start_date": materialized_view_month_start().date(),
                "end_date": dh.today.date(),
                "filter": {"resolution": "monthly"},
            },
        ]

        for params in scenarios:
            with self.subTest(params=params):
                with self.assertRaises(ValidationError):
                    serializer = QueryParamSerializer(data=params)
                    serializer.is_valid(raise_exception=True)
示例#8
0
 def test_multiple_group_by_with_matching_sort(self):
     """Test multiple group by with a matching sort for group_by parameters"""
     query_params = {
         "group_by": {"account": "*", "region": "east"},
         "order_by": {"region": "asc"},
         "filter": {
             "resolution": "daily",
             "time_scope_value": "-10",
             "time_scope_units": "day",
             "resource_scope": [],
         },
     }
     serializer = QueryParamSerializer(data=query_params)
     self.assertTrue(serializer.is_valid())
示例#9
0
    def test_parse_filter_dates_invalid_delta_pairing(self):
        """Test parse of a filter date-based param with delta should not succeed."""
        dh = DateHelper()
        scenarios = [
            {
                "end_date": dh.this_month_start.date(),
                "delta": "cost"
            },
            {
                "start_date": materialized_view_month_start().date(),
                "delta": "cost"
            },
            {
                "start_date": materialized_view_month_start().date(),
                "end_date": dh.today.date(),
                "delta": "cost"
            },
        ]

        for params in scenarios:
            with self.subTest(params=params):
                with self.assertRaises(ValidationError):
                    serializer = QueryParamSerializer(data=params)
                    serializer.is_valid(raise_exception=True)
示例#10
0
 def test_parse_query_params_success(self):
     """Test parse of a query params successfully."""
     query_params = {
         "group_by": {"account": ["account1"]},
         "order_by": {"usage": "asc"},
         "filter": {
             "resolution": "daily",
             "time_scope_value": "-10",
             "time_scope_units": "day",
             "resource_scope": [],
         },
         "units": "byte",
     }
     serializer = QueryParamSerializer(data=query_params)
     self.assertTrue(serializer.is_valid())
示例#11
0
    def test_parse_filter_dates_valid(self):
        """Test parse of a filter date-based param should succeed."""
        dh = DateHelper()
        scenarios = [
            {
                "start_date": dh.yesterday.date(),
                "end_date": dh.today.date()
            },
            {
                "start_date": dh.last_month_end.date(),
                "end_date": dh.this_month_start.date(),
                "filter": {
                    "resolution": "daily"
                },
            },
            {
                "start_date": materialized_view_month_start().date(),
                "end_date": dh.today.date(),
                "filter": {
                    "resolution": "daily"
                },
            },
            {
                "start_date": dh.last_month_end.date(),
                "end_date": dh.this_month_start.date(),
                "filter": {
                    "resolution": "monthly"
                },
            },
            {
                "start_date": materialized_view_month_start().date(),
                "end_date": dh.today.date(),
                "filter": {
                    "resolution": "monthly"
                },
            },
        ]

        for params in scenarios:
            with self.subTest(params=params):
                serializer = QueryParamSerializer(
                    data=params, context=self.alt_request_context)
                self.assertTrue(serializer.is_valid(raise_exception=True))
示例#12
0
 def test_parse_query_params_success(self):
     """Test parse of a query params successfully."""
     query_params = {
         'group_by': {
             'account': ['account1']
         },
         'order_by': {
             'usage': 'asc'
         },
         'filter': {
             'resolution': 'daily',
             'time_scope_value': '-10',
             'time_scope_units': 'day',
             'resource_scope': []
         },
         'units': 'byte'
     }
     serializer = QueryParamSerializer(data=query_params)
     self.assertTrue(serializer.is_valid())
示例#13
0
    def test_parse_filter_dates_invalid(self):
        """Test parse of invalid data for filter date-based param should not succeed."""
        dh = DateHelper()
        scenarios = [
            {"start_date": dh.today.date()},
            {"end_date": dh.today.date()},
            {"start_date": dh.yesterday.date(), "end_date": dh.tomorrow.date()},
            {"start_date": dh.n_days_ago(materialized_view_month_start(), 1), "end_date": dh.today.date()},
            {"start_date": dh.today.date(), "end_date": dh.yesterday.date()},
            {"start_date": "llamas", "end_date": dh.yesterday.date()},
            {"start_date": dh.yesterday.date(), "end_date": "alpacas"},
            {"start_date": "llamas", "end_date": "alpacas"},
            {
                "start_date": materialized_view_month_start().date(),
                "end_date": dh.last_month_end.date(),
                "filter": {"time_scope_units": "day"},
            },
            {
                "start_date": materialized_view_month_start().date(),
                "end_date": dh.last_month_end.date(),
                "filter": {"time_scope_value": "-1"},
            },
            {
                "start_date": materialized_view_month_start().date(),
                "end_date": dh.last_month_end.date(),
                "filter": {"time_scope_units": "day", "time_scope_value": "-1"},
            },
            {
                "start_date": materialized_view_month_start().date() - relativedelta(months=1),
                "end_date": dh.last_month_end.date(),
                "filter": {"time_scope_units": "day", "time_scope_value": "-1"},
            },
        ]

        for params in scenarios:
            with self.subTest(params=params):
                serializer = QueryParamSerializer(data=params)
                self.assertFalse(serializer.is_valid())
示例#14
0
 def test_parse_units_failure(self):
     """Test failure while parsing units query params."""
     query_params = {"units": "bites"}
     serializer = QueryParamSerializer(data=query_params)
     with self.assertRaises(serializers.ValidationError):
         serializer.is_valid(raise_exception=True)