Skip to content

Commit

Permalink
TO SQUASH: Fixing b0rken unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Sep 16, 2016
1 parent 8cefe3a commit 7e81466
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 112 deletions.
6 changes: 5 additions & 1 deletion unit_tests/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ class _GAXBaseAPI(object):
def __init__(self, **kw):
self.__dict__.update(kw)

def _make_grpc_error(self, status_code):
def _make_grpc_error(self, status_code=None):
from grpc._channel import _Rendezvous
from grpc._channel import _RPCState
from grpc import StatusCode

if status_code is None:
status_code = StatusCode.UNKNOWN

details = 'Some error details.'
exc_state = _RPCState((), None, None, status_code, details)
Expand Down
90 changes: 45 additions & 45 deletions unit_tests/logging/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,12 +423,13 @@ def test_logger_delete_not_found(self):
self.assertEqual(options, None)

def test_logger_delete_error(self):
from google.gax.errors import GaxError
from grpc._channel import _Rendezvous

LOG_PATH = 'projects/%s/logs/%s' % (self.PROJECT, self.LOG_NAME)
gax_api = _GAXLoggingAPI(_random_gax_error=True)
api = self._makeOne(gax_api)

with self.assertRaises(GaxError):
with self.assertRaises(_Rendezvous):
api.logger_delete(self.PROJECT, self.LOG_NAME)

log_name, options = gax_api._delete_log_called_with
Expand Down Expand Up @@ -503,11 +504,12 @@ def test_list_sinks_w_paging(self):
self.assertEqual(options.page_token, TOKEN)

def test_sink_create_error(self):
from google.gax.errors import GaxError
from grpc._channel import _Rendezvous

gax_api = _GAXSinksAPI(_random_gax_error=True)
api = self._makeOne(gax_api)

with self.assertRaises(GaxError):
with self.assertRaises(_Rendezvous):
api.sink_create(
self.PROJECT, self.SINK_NAME, self.FILTER,
self.DESTINATION_URI)
Expand Down Expand Up @@ -548,11 +550,12 @@ def test_sink_get_error(self):
api.sink_get(self.PROJECT, self.SINK_NAME)

def test_sink_get_miss(self):
from google.gax.errors import GaxError
from grpc._channel import _Rendezvous

gax_api = _GAXSinksAPI(_random_gax_error=True)
api = self._makeOne(gax_api)

with self.assertRaises(GaxError):
with self.assertRaises(_Rendezvous):
api.sink_get(self.PROJECT, self.SINK_NAME)

def test_sink_get_hit(self):
Expand All @@ -575,11 +578,12 @@ def test_sink_get_hit(self):
self.assertEqual(options, None)

def test_sink_update_error(self):
from google.gax.errors import GaxError
from grpc._channel import _Rendezvous

gax_api = _GAXSinksAPI(_random_gax_error=True)
api = self._makeOne(gax_api)

with self.assertRaises(GaxError):
with self.assertRaises(_Rendezvous):
api.sink_update(
self.PROJECT, self.SINK_NAME, self.FILTER,
self.DESTINATION_URI)
Expand Down Expand Up @@ -614,11 +618,12 @@ def test_sink_update_hit(self):
self.assertEqual(options, None)

def test_sink_delete_error(self):
from google.gax.errors import GaxError
from grpc._channel import _Rendezvous

gax_api = _GAXSinksAPI(_random_gax_error=True)
api = self._makeOne(gax_api)

with self.assertRaises(GaxError):
with self.assertRaises(_Rendezvous):
api.sink_delete(self.PROJECT, self.SINK_NAME)

def test_sink_delete_miss(self):
Expand Down Expand Up @@ -707,11 +712,12 @@ def test_list_metrics_w_paging(self):
self.assertEqual(options.page_token, TOKEN)

def test_metric_create_error(self):
from google.gax.errors import GaxError
from grpc._channel import _Rendezvous

gax_api = _GAXMetricsAPI(_random_gax_error=True)
api = self._makeOne(gax_api)

with self.assertRaises(GaxError):
with self.assertRaises(_Rendezvous):
api.metric_create(
self.PROJECT, self.METRIC_NAME, self.FILTER,
self.DESCRIPTION)
Expand Down Expand Up @@ -752,11 +758,12 @@ def test_metric_get_error(self):
api.metric_get(self.PROJECT, self.METRIC_NAME)

def test_metric_get_miss(self):
from google.gax.errors import GaxError
from grpc._channel import _Rendezvous

gax_api = _GAXMetricsAPI(_random_gax_error=True)
api = self._makeOne(gax_api)

with self.assertRaises(GaxError):
with self.assertRaises(_Rendezvous):
api.metric_get(self.PROJECT, self.METRIC_NAME)

def test_metric_get_hit(self):
Expand All @@ -779,11 +786,12 @@ def test_metric_get_hit(self):
self.assertEqual(options, None)

def test_metric_update_error(self):
from google.gax.errors import GaxError
from grpc._channel import _Rendezvous

gax_api = _GAXMetricsAPI(_random_gax_error=True)
api = self._makeOne(gax_api)

with self.assertRaises(GaxError):
with self.assertRaises(_Rendezvous):
api.metric_update(
self.PROJECT, self.METRIC_NAME, self.FILTER,
self.DESCRIPTION)
Expand Down Expand Up @@ -818,11 +826,12 @@ def test_metric_update_hit(self):
self.assertEqual(options, None)

def test_metric_delete_error(self):
from google.gax.errors import GaxError
from grpc._channel import _Rendezvous

gax_api = _GAXMetricsAPI(_random_gax_error=True)
api = self._makeOne(gax_api)

with self.assertRaises(GaxError):
with self.assertRaises(_Rendezvous):
api.metric_delete(self.PROJECT, self.METRIC_NAME)

def test_metric_delete_miss(self):
Expand Down Expand Up @@ -929,12 +938,11 @@ def write_log_entries(self, entries, log_name, resource, labels,
entries, log_name, resource, labels, partial_success, options)

def delete_log(self, log_name, options):
from google.gax.errors import GaxError
self._delete_log_called_with = log_name, options
if self._random_gax_error:
raise GaxError('error')
raise self._make_grpc_error()
if self._delete_not_found:
raise GaxError('notfound', self._make_grpc_not_found())
raise self._make_grpc_not_found()


class _GAXSinksAPI(_GAXBaseAPI):
Expand All @@ -947,40 +955,36 @@ def list_sinks(self, parent, page_size, options):
return self._list_sinks_response

def create_sink(self, parent, sink, options):
from google.gax.errors import GaxError
self._create_sink_called_with = parent, sink, options
if self._random_gax_error:
raise GaxError('error')
raise self._make_grpc_error()
if self._create_sink_conflict:
raise GaxError('conflict', self._make_grpc_failed_precondition())
raise self._make_grpc_failed_precondition()

def get_sink(self, sink_name, options):
from google.gax.errors import GaxError
self._get_sink_called_with = sink_name, options
if self._random_gax_error:
raise GaxError('error')
raise self._make_grpc_error()
try:
return self._get_sink_response
except AttributeError:
raise GaxError('notfound', self._make_grpc_not_found())
raise self._make_grpc_not_found()

def update_sink(self, sink_name, sink, options=None):
from google.gax.errors import GaxError
self._update_sink_called_with = sink_name, sink, options
if self._random_gax_error:
raise GaxError('error')
raise self._make_grpc_error()
try:
return self._update_sink_response
except AttributeError:
raise GaxError('notfound', self._make_grpc_not_found())
raise self._make_grpc_not_found()

def delete_sink(self, sink_name, options=None):
from google.gax.errors import GaxError
self._delete_sink_called_with = sink_name, options
if self._random_gax_error:
raise GaxError('error')
raise self._make_grpc_error()
if self._sink_not_found:
raise GaxError('notfound', self._make_grpc_not_found())
raise self._make_grpc_not_found()


class _GAXMetricsAPI(_GAXBaseAPI):
Expand All @@ -993,40 +997,36 @@ def list_log_metrics(self, parent, page_size, options):
return self._list_log_metrics_response

def create_log_metric(self, parent, metric, options):
from google.gax.errors import GaxError
self._create_log_metric_called_with = parent, metric, options
if self._random_gax_error:
raise GaxError('error')
raise self._make_grpc_error()
if self._create_log_metric_conflict:
raise GaxError('conflict', self._make_grpc_failed_precondition())
raise self._make_grpc_failed_precondition()

def get_log_metric(self, metric_name, options):
from google.gax.errors import GaxError
self._get_log_metric_called_with = metric_name, options
if self._random_gax_error:
raise GaxError('error')
raise self._make_grpc_error()
try:
return self._get_log_metric_response
except AttributeError:
raise GaxError('notfound', self._make_grpc_not_found())
raise self._make_grpc_not_found()

def update_log_metric(self, metric_name, metric, options=None):
from google.gax.errors import GaxError
self._update_log_metric_called_with = metric_name, metric, options
if self._random_gax_error:
raise GaxError('error')
raise self._make_grpc_error()
try:
return self._update_log_metric_response
except AttributeError:
raise GaxError('notfound', self._make_grpc_not_found())
raise self._make_grpc_not_found()

def delete_log_metric(self, metric_name, options=None):
from google.gax.errors import GaxError
self._delete_log_metric_called_with = metric_name, options
if self._random_gax_error:
raise GaxError('error')
raise self._make_grpc_error()
if self._log_metric_not_found:
raise GaxError('notfound', self._make_grpc_not_found())
raise self._make_grpc_not_found()


class _HTTPRequestPB(object):
Expand Down
Loading

0 comments on commit 7e81466

Please sign in to comment.