def validate_end_date(self, value): """Validate that the end_date is within the expected range.""" dh = DateHelper() if value >= materialized_view_month_start( dh).date() and value <= dh.today.date(): return value error = "Parameter end_date must be from {} to {}".format( materialized_view_month_start(dh).date(), dh.today.date()) raise serializers.ValidationError(error)
def test_parse_filter_dates_invalid_delta_pairing(self): """Test parse of a filter date-based param with monthly presolution 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)
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(dh), 1).date(), "end_date": dh.today.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": dh.last_month_start.date(), "end_date": dh.last_month_end.date(), "filter": {"time_scope_units": "day"}, }, { "start_date": dh.last_month_start.date(), "end_date": dh.last_month_end.date(), "filter": {"time_scope_value": "-1"}, }, { "start_date": dh.last_month_start.date(), "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 = OrgQueryParamSerializer(data=params) self.assertFalse(serializer.is_valid())
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": "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) self.assertTrue(serializer.is_valid(raise_exception=True))
def validate_order_by(self, value): # noqa: C901 """Validate incoming order_by data. Args: value (Dict): data to be validated Returns: (Dict): Validated data Raises: (ValidationError): if order_by field inputs are invalid """ error = {} for key, val in value.items(): if key in self.order_by_allowlist: continue # fields that do not require a group-by if "or:" in key: error[key] = _( f'The order_by key "{key}" can not contain the or parameter.' ) raise serializers.ValidationError(error) if "group_by" in self.initial_data: group_keys = self.initial_data.get("group_by").keys() # check for or keys or_keys = [] for k in group_keys: if "or:" in k: field_value = k.split(":").pop() or_keys.append(field_value) if key in group_keys: continue # found matching group-by if key in or_keys: continue # found matching group-by with or # special case: we order by account_alias, but we group by account. if key == "account_alias" and ("account" in group_keys or "account" in or_keys): continue # sepcial case: we order by date, but we group by an allowed param. if key == "date" and group_keys: # Checks to make sure the orderby date is allowed dh = DateHelper() if (value.get("date") >= materialized_view_month_start(dh).date() and value.get("date") <= dh.today.date()): continue error[key] = _( f"Order-by date must be from {materialized_view_month_start(dh).date()} to {dh.today.date()}" ) raise serializers.ValidationError(error) error[key] = _(f'Order-by "{key}" requires matching Group-by.') raise serializers.ValidationError(error) return value
def validate_start_date(self, value): """Validate that the start_date is within the expected range.""" dh = DateHelper() if value >= materialized_view_month_start(dh).date() and value <= dh.today.date(): return value error = f"Parameter start_date must be from {dh.last_month_start.date()} to {dh.today.date()}" raise serializers.ValidationError(error)
def test_materialized_view_month_start(self): """Test materialized_view_month_start property.""" with patch.object(Config, "MASU_RETAIN_NUM_MONTHS", 5): today = timezone.now().replace(microsecond=0, second=0, minute=0, hour=0) retain_months_ago = today - relativedelta( months=Config.MASU_RETAIN_NUM_MONTHS - 1) expected = retain_months_ago.replace(day=1) self.assertEqual(materialized_view_month_start(), expected)
def test_materialized_view_month_start(self): """Test materialized_view_month_start property.""" today = timezone.now().replace(microsecond=0, second=0, minute=0, hour=0) retain_months_ago = today - relativedelta(months=settings.RETAIN_NUM_MONTHS - 1) expected = retain_months_ago.replace(day=1) self.assertEqual(materialized_view_month_start(), expected)
def test_ocp_aws_out_of_range_under_date(self): wrong_date = materialized_view_month_start() - timedelta(days=1) url = f"?order_by[cost]=desc&order_by[date]={wrong_date}&group_by[service]=*" with self.assertRaises(ValidationError): self.mocked_query_params(url, OCPAWSCostView)