Skip to content

Commit

Permalink
Updating unit tests are list_metrics() iterator update.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Oct 31, 2016
1 parent 940fce6 commit 5b33597
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 50 deletions.
52 changes: 34 additions & 18 deletions logging/unit_tests/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -843,56 +843,72 @@ 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)
self.assertEqual(page_size, 0)
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)
Expand Down
75 changes: 53 additions & 22 deletions logging/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,29 +485,49 @@ 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 = [{
'name': self.METRIC_NAME,
'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
Expand All @@ -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):
Expand All @@ -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
Expand Down
55 changes: 45 additions & 10 deletions logging/unit_tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': [{
Expand All @@ -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 = {
Expand All @@ -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
Expand Down

0 comments on commit 5b33597

Please sign in to comment.