diff --git a/logging/unit_tests/test__gax.py b/logging/unit_tests/test__gax.py index ed5f1f12c9f0e..90206e26a388c 100644 --- a/logging/unit_tests/test__gax.py +++ b/logging/unit_tests/test__gax.py @@ -843,27 +843,36 @@ def test_ctor(self): self.assertIs(api._gax_api, gax_api) def test_list_metrics_no_paging(self): + import six from google.gax import INITIAL_PAGE - from google.cloud._testing import _GAXPageIterator from google.logging.v2.logging_metrics_pb2 import LogMetric + from google.cloud._testing import _GAXPageIterator + from google.cloud.logging.metric import Metric TOKEN = 'TOKEN' - METRICS = [{ - 'name': self.METRIC_PATH, - 'filter': self.FILTER, - 'description': self.DESCRIPTION, - }] metric_pb = LogMetric(name=self.METRIC_PATH, description=self.DESCRIPTION, filter=self.FILTER) response = _GAXPageIterator([metric_pb], page_token=TOKEN) gax_api = _GAXMetricsAPI(_list_log_metrics_response=response) - api = self._makeOne(gax_api, None) + client = object() + api = self._makeOne(gax_api, client) - metrics, token = api.list_metrics(self.PROJECT) + iterator = api.list_metrics(self.PROJECT) + page = six.next(iterator.pages) + metrics = list(page) + token = iterator.next_page_token - self.assertEqual(metrics, METRICS) + # First check the token. self.assertEqual(token, TOKEN) + # Then check the metrics returned. + self.assertEqual(len(metrics), 1) + metric = metrics[0] + self.assertIsInstance(metric, Metric) + self.assertEqual(metric.name, self.METRIC_PATH) + self.assertEqual(metric.filter_, self.FILTER) + self.assertEqual(metric.description, self.DESCRIPTION) + self.assertIs(metric.client, client) project, page_size, options = gax_api._list_log_metrics_called_with self.assertEqual(project, self.PROJECT_PATH) @@ -871,28 +880,35 @@ def test_list_metrics_no_paging(self): self.assertEqual(options.page_token, INITIAL_PAGE) def test_list_metrics_w_paging(self): - from google.cloud._testing import _GAXPageIterator from google.logging.v2.logging_metrics_pb2 import LogMetric + from google.cloud._testing import _GAXPageIterator + from google.cloud.logging.metric import Metric TOKEN = 'TOKEN' PAGE_SIZE = 42 - METRICS = [{ - 'name': self.METRIC_PATH, - 'filter': self.FILTER, - 'description': self.DESCRIPTION, - }] metric_pb = LogMetric(name=self.METRIC_PATH, description=self.DESCRIPTION, filter=self.FILTER) response = _GAXPageIterator([metric_pb]) gax_api = _GAXMetricsAPI(_list_log_metrics_response=response) - api = self._makeOne(gax_api, None) + client = object() + api = self._makeOne(gax_api, client) - metrics, token = api.list_metrics( + iterator = api.list_metrics( self.PROJECT, page_size=PAGE_SIZE, page_token=TOKEN) + metrics = list(iterator) + token = iterator.next_page_token - self.assertEqual(metrics, METRICS) + # First check the token. self.assertIsNone(token) + # Then check the metrics returned. + self.assertEqual(len(metrics), 1) + metric = metrics[0] + self.assertIsInstance(metric, Metric) + self.assertEqual(metric.name, self.METRIC_PATH) + self.assertEqual(metric.filter_, self.FILTER) + self.assertEqual(metric.description, self.DESCRIPTION) + self.assertIs(metric.client, client) project, page_size, options = gax_api._list_log_metrics_called_with self.assertEqual(project, self.PROJECT_PATH) diff --git a/logging/unit_tests/test_client.py b/logging/unit_tests/test_client.py index 8c94a088bbccd..490372f03a961 100644 --- a/logging/unit_tests/test_client.py +++ b/logging/unit_tests/test_client.py @@ -485,7 +485,9 @@ def test_metric_explicit(self): self.assertEqual(metric.project, self.PROJECT) def test_list_metrics_no_paging(self): + import six from google.cloud.logging.metric import Metric + PROJECT = 'PROJECT' TOKEN = 'TOKEN' METRICS = [{ @@ -493,21 +495,39 @@ def test_list_metrics_no_paging(self): 'filter': self.FILTER, 'description': self.DESCRIPTION, }] - client = self._makeOne(project=PROJECT, credentials=_Credentials()) - api = client._metrics_api = _DummyMetricsAPI() - api._list_metrics_response = METRICS, TOKEN + client = self._makeOne(project=PROJECT, credentials=_Credentials(), + use_gax=False) + returned = { + 'metrics': METRICS, + 'nextPageToken': TOKEN, + } + client.connection = _Connection(returned) - metrics, token = client.list_metrics() + # Execute request. + iterator = client.list_metrics() + page = six.next(iterator.pages) + metrics = list(page) + token = iterator.next_page_token + # First check the token. + self.assertEqual(token, TOKEN) + # Then check the metrics returned. self.assertEqual(len(metrics), 1) metric = metrics[0] self.assertIsInstance(metric, Metric) self.assertEqual(metric.name, self.METRIC_NAME) self.assertEqual(metric.filter_, self.FILTER) self.assertEqual(metric.description, self.DESCRIPTION) - self.assertEqual(token, TOKEN) - self.assertEqual(api._list_metrics_called_with, - (PROJECT, None, None)) + self.assertIs(metric.client, client) + + # Verify mocked transport. + called_with = client.connection._called_with + path = '/projects/%s/metrics' % (self.PROJECT,) + self.assertEqual(called_with, { + 'method': 'GET', + 'path': path, + 'query_params': {}, + }) def test_list_metrics_with_paging(self): from google.cloud.logging.metric import Metric @@ -519,22 +539,40 @@ def test_list_metrics_with_paging(self): 'filter': self.FILTER, 'description': self.DESCRIPTION, }] - client = self._makeOne(project=PROJECT, credentials=_Credentials()) - api = client._metrics_api = _DummyMetricsAPI() - api._list_metrics_response = METRICS, None + client = self._makeOne(project=PROJECT, credentials=_Credentials(), + use_gax=False) + returned = { + 'metrics': METRICS, + } + client.connection = _Connection(returned) # Execute request. - metrics, token = client.list_metrics(PAGE_SIZE, TOKEN) - # Test values are correct. + iterator = client.list_metrics(PAGE_SIZE, TOKEN) + metrics = list(iterator) + token = iterator.next_page_token + + # First check the token. + self.assertIsNone(token) + # Then check the metrics returned. self.assertEqual(len(metrics), 1) metric = metrics[0] self.assertIsInstance(metric, Metric) self.assertEqual(metric.name, self.METRIC_NAME) self.assertEqual(metric.filter_, self.FILTER) self.assertEqual(metric.description, self.DESCRIPTION) - self.assertIsNone(token) - self.assertEqual(api._list_metrics_called_with, - (PROJECT, PAGE_SIZE, TOKEN)) + self.assertIs(metric.client, client) + + # Verify mocked transport. + called_with = client.connection._called_with + path = '/projects/%s/metrics' % (self.PROJECT,) + self.assertEqual(called_with, { + 'method': 'GET', + 'path': path, + 'query_params': { + 'pageSize': PAGE_SIZE, + 'pageToken': TOKEN, + }, + }) class _Credentials(object): @@ -550,13 +588,6 @@ def create_scoped(self, scope): return self -class _DummyMetricsAPI(object): - - def list_metrics(self, project, page_size, page_token): - self._list_metrics_called_with = (project, page_size, page_token) - return self._list_metrics_response - - class _Connection(object): _called_with = None diff --git a/logging/unit_tests/test_connection.py b/logging/unit_tests/test_connection.py index ec25c185e7594..5d41877476d86 100644 --- a/logging/unit_tests/test_connection.py +++ b/logging/unit_tests/test_connection.py @@ -540,6 +540,9 @@ def _makeOne(self, *args, **kw): return self._getTargetClass()(*args, **kw) def test_list_metrics_no_paging(self): + import six + from google.cloud.logging.metric import Metric + TOKEN = 'TOKEN' RETURNED = { 'metrics': [{ @@ -552,16 +555,33 @@ def test_list_metrics_no_paging(self): client = _Client(conn) api = self._makeOne(client) - metrics, token = api.list_metrics(self.PROJECT) + iterator = api.list_metrics(self.PROJECT) + page = six.next(iterator.pages) + metrics = list(page) + token = iterator.next_page_token - self.assertEqual(metrics, RETURNED['metrics']) + # First check the token. self.assertEqual(token, TOKEN) + # Then check the metrics returned. + self.assertEqual(len(metrics), 1) + metric = metrics[0] + self.assertIsInstance(metric, Metric) + self.assertEqual(metric.name, self.METRIC_PATH) + self.assertEqual(metric.filter_, self.FILTER) + self.assertEqual(metric.description, '') + self.assertIs(metric.client, client) - self.assertEqual(conn._called_with['method'], 'GET') + called_with = conn._called_with path = '/%s' % (self.LIST_METRICS_PATH,) - self.assertEqual(conn._called_with['path'], path) + self.assertEqual(called_with, { + 'method': 'GET', + 'path': path, + 'query_params': {}, + }) def test_list_metrics_w_paging(self): + from google.cloud.logging.metric import Metric + TOKEN = 'TOKEN' PAGE_SIZE = 42 RETURNED = { @@ -574,17 +594,32 @@ def test_list_metrics_w_paging(self): client = _Client(conn) api = self._makeOne(client) - metrics, token = api.list_metrics( + iterator = api.list_metrics( self.PROJECT, page_size=PAGE_SIZE, page_token=TOKEN) + metrics = list(iterator) + token = iterator.next_page_token - self.assertEqual(metrics, RETURNED['metrics']) + # First check the token. self.assertIsNone(token) + # Then check the metrics returned. + self.assertEqual(len(metrics), 1) + metric = metrics[0] + self.assertIsInstance(metric, Metric) + self.assertEqual(metric.name, self.METRIC_PATH) + self.assertEqual(metric.filter_, self.FILTER) + self.assertEqual(metric.description, '') + self.assertIs(metric.client, client) - self.assertEqual(conn._called_with['method'], 'GET') + called_with = conn._called_with path = '/%s' % (self.LIST_METRICS_PATH,) - self.assertEqual(conn._called_with['path'], path) - self.assertEqual(conn._called_with['query_params'], - {'pageSize': PAGE_SIZE, 'pageToken': TOKEN}) + self.assertEqual(called_with, { + 'method': 'GET', + 'path': path, + 'query_params': { + 'pageSize': PAGE_SIZE, + 'pageToken': TOKEN, + }, + }) def test_metric_create_conflict(self): from google.cloud.exceptions import Conflict