def test_composed_string_table_op(self): """Test composed_query_string() method using table and operation parameters.""" table = self.fake.word() operation = self.fake.word() filt = QueryFilter(table=table, operation=operation) expected = f'{table}__{operation}' self.assertEqual(filt.composed_query_string(), expected)
def test_execute_query_current_month_filter_service(self): """Test execute_query for current month on monthly filtered by service.""" self.generator = OCPAzureReportDataGenerator(self.tenant, self.provider, current_month_only=True) self.generator.add_data_to_tenant( fixed_fields=["subscription_guid", "resource_location", "tags", "service_name"] ) service = self.generator.config.service_name url = f"?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=monthly&filter[service_name]={service}" # noqa: E501 query_params = self.mocked_query_params(url, OCPAzureCostView) handler = OCPAzureReportQueryHandler(query_params) query_output = handler.execute_query() data = query_output.get("data") self.assertIsNotNone(data) self.assertIsNotNone(query_output.get("total")) total = query_output.get("total") aggregates = handler._mapper.report_type_map.get("aggregates") filters = {**self.this_month_filter, "service_name__icontains": service} for filt in handler._mapper.report_type_map.get("filter"): if filt: qf = QueryFilter(**filt) filters.update({qf.composed_query_string(): qf.parameter}) current_totals = self.get_totals_by_time_scope(aggregates, filters) self.assertIsNotNone(total.get("cost")) self.assertEqual(total.get("cost", {}).get("value", 0), current_totals.get("cost", 1)) cmonth_str = self.dh.this_month_start.strftime("%Y-%m") for data_item in data: month_val = data_item.get("date", "not-a-date") month_data = data_item.get("values") self.assertEqual(month_val, cmonth_str) self.assertIsInstance(month_data, list)
def test_execute_query_by_filtered_cluster(self): """Test execute_query monthly breakdown by filtered cluster.""" self.generator.add_data_to_tenant() cluster = self.generator.cluster_id url = f"?filter[time_scope_units]=month&filter[time_scope_value]=-1&filter[resolution]=monthly&group_by[cluster]={cluster}" # noqa: E501 query_params = self.mocked_query_params(url, OCPAzureCostView) handler = OCPAzureReportQueryHandler(query_params) query_output = handler.execute_query() data = query_output.get("data") self.assertIsNotNone(data) self.assertIsNotNone(query_output.get("total")) total = query_output.get("total") aggregates = handler._mapper.report_type_map.get("aggregates") filters = {**self.this_month_filter, "cluster_id__icontains": cluster} for filt in handler._mapper.report_type_map.get("filter"): if filt: qf = QueryFilter(**filt) filters.update({qf.composed_query_string(): qf.parameter}) current_totals = self.get_totals_by_time_scope(aggregates, filters) self.assertIsNotNone(total.get("cost")) self.assertEqual(total.get("cost", {}).get("value", 0), current_totals.get("cost", 1)) cmonth_str = self.dh.this_month_start.strftime("%Y-%m") for data_item in data: month_val = data_item.get("date", "not-a-date") month_data = data_item.get("clusters") self.assertEqual(month_val, cmonth_str) self.assertIsInstance(month_data, list) for month_item in month_data: self.assertIsInstance(month_item.get("cluster"), str) self.assertIsInstance(month_item.get("values"), list) self.assertIsNotNone(month_item.get("values")[0].get("cost"))
def test_composed_string_all(self): """Test composed_query_string() method using all parameters.""" table = self.fake.word() field = self.fake.word() operation = self.fake.word() parameter = self.fake.word() filt = QueryFilter(table, field, operation, parameter) expected = f'{table}__{field}__{operation}' self.assertEqual(filt.composed_query_string(), expected)
def test_from_string_two_parts(self): """Test from_string() method with two parts.""" table = self.fake.word() operation = self.fake.word() SEP = QueryFilter.SEP test_string = table + SEP + operation filt = QueryFilter().from_string(test_string) self.assertEqual(filt.table, table) self.assertEqual(filt.operation, operation) self.assertEqual(filt.composed_query_string(), test_string)