def test_as_dataframe_w_max_rows(self, mock_query_iter): mock_query_iter.return_value = self._query_iter_get_result() MAX_ROWS = 1 query_metadata = gcm.QueryMetadata(self.query) dataframe = query_metadata.as_dataframe(max_rows=MAX_ROWS) self.assertEqual(dataframe.shape, (MAX_ROWS, 5)) expected_values = [ [ RESOURCE_TYPE, PROJECT, INSTANCE_ZONES[0], INSTANCE_IDS[0], INSTANCE_NAMES[0] ], ] self.assertEqual(dataframe.values.tolist(), expected_values) expected_headers = [('resource.type', ''), ('resource.labels', 'project_id'), ('resource.labels', 'zone'), ('resource.labels', 'instance_id'), ('metric.labels', 'instance_name')] self.assertEqual(dataframe.columns.tolist(), expected_headers) self.assertEqual(dataframe.columns.names, [None, None]) self.assertEqual(dataframe.index.tolist(), list(range(MAX_ROWS))) self.assertEqual(dataframe.index.names, [None])
def test_as_dataframe(self, mock_query_iter): mock_query_iter.return_value = self._query_iter_get_result() query_metadata = gcm.QueryMetadata(self.query) dataframe = query_metadata.as_dataframe() NUM_INSTANCES = len(INSTANCE_IDS) self.assertEqual(dataframe.shape, (NUM_INSTANCES, 5)) expected_values = [[ RESOURCE_TYPE, PROJECT, zone, instance_id, instance_name ] for zone, instance_id, instance_name in zip( INSTANCE_ZONES, INSTANCE_IDS, INSTANCE_NAMES)] self.assertEqual(dataframe.values.tolist(), expected_values) expected_headers = [('resource.type', ''), ('resource.labels', 'project_id'), ('resource.labels', 'zone'), ('resource.labels', 'instance_id'), ('metric.labels', 'instance_name')] self.assertEqual(dataframe.columns.tolist(), expected_headers) self.assertEqual(dataframe.columns.names, [None, None]) self.assertEqual(dataframe.index.tolist(), list(range(NUM_INSTANCES))) self.assertEqual(dataframe.index.names, [None])
def test_as_dataframe_w_no_data(self, mock_query_iter): query_metadata = gcm.QueryMetadata(self.query) dataframe = query_metadata.as_dataframe() self.assertEqual(dataframe.shape, (0, 0)) self.assertIsNone(dataframe.columns.name) self.assertIsNone(dataframe.index.name)
def test_iteration(self, mock_query_iter): time_series_iterable = list(self._query_iter_get_result()) mock_query_iter.return_value = self._query_iter_get_result() query_metadata = gcm.QueryMetadata(self.query) response = list(query_metadata) self.assertEqual(len(response), len(time_series_iterable)) self.assertEqual(response, time_series_iterable)
def test_constructor(self, mock_query_iter): time_series_iterable = list(self._query_iter_get_result()) mock_query_iter.return_value = self._query_iter_get_result() query_metadata = gcm.QueryMetadata(self.query) mock_query_iter.assert_called_once_with(headers_only=True) self.assertEqual(query_metadata.metric_type, METRIC_TYPE) self.assertEqual(query_metadata.resource_types, set([RESOURCE_TYPE])) self.assertEqual(query_metadata._timeseries_list, time_series_iterable)