Skip to content

Commit

Permalink
Dropping usage of _Monkey in favor of mock.patch.
Browse files Browse the repository at this point in the history
This was done only in bigquery, datastore and storage
packages. Still needs updates in bigtable, core, logging,
monitoring, pubsub and speech.
  • Loading branch information
dhermes committed Nov 11, 2016
1 parent eea4410 commit 85e8b5c
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 109 deletions.
1 change: 1 addition & 0 deletions bigquery/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ localdeps =
pip install --quiet --upgrade {toxinidir}/../core
deps =
pytest
mock
covercmd =
py.test --quiet \
--cov=google.cloud.bigquery \
Expand Down
12 changes: 6 additions & 6 deletions bigquery/unit_tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1640,11 +1640,10 @@ def test_upload_from_file_w_bound_client_multipart(self):
def test_upload_from_file_resumable_with_400(self):
import csv
import datetime
import mock
from six.moves.http_client import BAD_REQUEST
from google.cloud.bigquery import table as MUT
from google.cloud.exceptions import BadRequest
from google.cloud._helpers import UTC
from google.cloud._testing import _Monkey
from google.cloud._testing import _NamedTemporaryFile
WHEN_TS = 1437767599.006
WHEN = datetime.datetime.utcfromtimestamp(WHEN_TS).replace(
Expand All @@ -1665,7 +1664,8 @@ class _UploadConfig(object):
dataset = _Dataset(client)
table = self._make_one(self.TABLE_NAME, dataset=dataset)

with _Monkey(MUT, _UploadConfig=_UploadConfig):
with mock.patch('google.cloud.bigquery.table._UploadConfig',
new=_UploadConfig):
with _NamedTemporaryFile() as temp:
with open(temp.name, 'w') as file_obj:
writer = csv.writer(file_obj)
Expand All @@ -1680,11 +1680,10 @@ class _UploadConfig(object):
# pylint: disable=too-many-statements
def test_upload_from_file_w_explicit_client_resumable(self):
import json
import mock
from six.moves.http_client import OK
from six.moves.urllib.parse import parse_qsl
from six.moves.urllib.parse import urlsplit
from google.cloud._testing import _Monkey
from google.cloud.bigquery import table as MUT

UPLOAD_PATH = 'https://example.com/upload/test'
initial_response = {'status': OK, 'location': UPLOAD_PATH}
Expand All @@ -1703,7 +1702,8 @@ class _UploadConfig(object):
simple_multipart = True
simple_path = u'' # force resumable

with _Monkey(MUT, _UploadConfig=_UploadConfig):
with mock.patch('google.cloud.bigquery.table._UploadConfig',
new=_UploadConfig):
orig_requested, PATH, BODY = self._upload_from_file_helper(
allow_jagged_rows=False,
allow_quoted_newlines=False,
Expand Down
1 change: 1 addition & 0 deletions datastore/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ localdeps =
pip install --quiet --upgrade {toxinidir}/../core
deps =
pytest
mock
covercmd =
py.test --quiet \
--cov=google.cloud.datastore \
Expand Down
58 changes: 35 additions & 23 deletions datastore/unit_tests/test__http.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ def _get_target_class():
return _DatastoreAPIOverGRPC

def _make_one(self, stub, connection=None, secure=True, mock_args=None):
from google.cloud._testing import _Monkey
from google.cloud.datastore import _http as MUT
import mock

if connection is None:
connection = _Connection(None)
Expand All @@ -197,10 +196,15 @@ def mock_make_stub(*args):
return stub

if secure:
to_monkey = {'make_secure_stub': mock_make_stub}
patch = mock.patch(
'google.cloud.datastore._http.make_secure_stub',
new=mock_make_stub)
else:
to_monkey = {'make_insecure_stub': mock_make_stub}
with _Monkey(MUT, **to_monkey):
patch = mock.patch(
'google.cloud.datastore._http.make_insecure_stub',
new=mock_make_stub)

with patch:
return self._get_target_class()(connection, secure)

def test_constructor(self):
Expand Down Expand Up @@ -372,9 +376,10 @@ def _make_query_pb(self, kind):
return pb

def _make_one(self, credentials=None, http=None, use_grpc=False):
from google.cloud._testing import _Monkey
from google.cloud.datastore import _http as MUT
with _Monkey(MUT, _USE_GRPC=use_grpc):
import mock

with mock.patch('google.cloud.datastore._http._USE_GRPC',
new=use_grpc):
return self._get_target_class()(credentials=credentials, http=http)

def _verifyProtobufCall(self, called_with, URI, conn):
Expand All @@ -391,15 +396,14 @@ def test_default_url(self):
self.assertEqual(conn.api_base_url, klass.API_BASE_URL)

def test_custom_url_from_env(self):
import os
from google.cloud._testing import _Monkey
import mock
from google.cloud.connection import API_BASE_URL
from google.cloud.environment_vars import GCD_HOST

HOST = 'CURR_HOST'
fake_environ = {GCD_HOST: HOST}

with _Monkey(os, environ=fake_environ):
with mock.patch('os.environ', new=fake_environ):
conn = self._make_one()

self.assertNotEqual(conn.api_base_url, API_BASE_URL)
Expand All @@ -410,8 +414,7 @@ def test_ctor_defaults(self):
self.assertIsNone(conn.credentials)

def test_ctor_without_grpc(self):
from google.cloud._testing import _Monkey
from google.cloud.datastore import _http as MUT
import mock

connections = []
return_val = object()
Expand All @@ -420,16 +423,18 @@ def mock_api(connection):
connections.append(connection)
return return_val

with _Monkey(MUT, _DatastoreAPIOverHttp=mock_api):
patch = mock.patch(
'google.cloud.datastore._http._DatastoreAPIOverHttp',
new=mock_api)
with patch:
conn = self._make_one(use_grpc=False)

self.assertIsNone(conn.credentials)
self.assertIs(conn._datastore_api, return_val)
self.assertEqual(connections, [conn])

def test_ctor_with_grpc(self):
from google.cloud._testing import _Monkey
from google.cloud.datastore import _http as MUT
import mock

api_args = []
return_val = object()
Expand All @@ -438,7 +443,10 @@ def mock_api(connection, secure):
api_args.append((connection, secure))
return return_val

with _Monkey(MUT, _DatastoreAPIOverGRPC=mock_api):
patch = mock.patch(
'google.cloud.datastore._http._DatastoreAPIOverGRPC',
new=mock_api)
with patch:
conn = self._make_one(use_grpc=True)

self.assertIsNone(conn.credentials)
Expand Down Expand Up @@ -922,9 +930,8 @@ def test_begin_transaction(self):
request.ParseFromString(cw['body'])

def test_commit_wo_transaction(self):
from google.cloud._testing import _Monkey
import mock
from google.cloud.datastore._generated import datastore_pb2
from google.cloud.datastore import _http as MUT
from google.cloud.datastore.helpers import _new_value_pb

PROJECT = 'PROJECT'
Expand Down Expand Up @@ -953,7 +960,10 @@ def mock_parse(response):
_parsed.append(response)
return expected_result

with _Monkey(MUT, _parse_commit_response=mock_parse):
patch = mock.patch(
'google.cloud.datastore._http._parse_commit_response',
new=mock_parse)
with patch:
result = conn.commit(PROJECT, req_pb, None)

self.assertIs(result, expected_result)
Expand All @@ -968,9 +978,8 @@ def mock_parse(response):
self.assertEqual(_parsed, [rsp_pb])

def test_commit_w_transaction(self):
from google.cloud._testing import _Monkey
import mock
from google.cloud.datastore._generated import datastore_pb2
from google.cloud.datastore import _http as MUT
from google.cloud.datastore.helpers import _new_value_pb

PROJECT = 'PROJECT'
Expand Down Expand Up @@ -999,7 +1008,10 @@ def mock_parse(response):
_parsed.append(response)
return expected_result

with _Monkey(MUT, _parse_commit_response=mock_parse):
patch = mock.patch(
'google.cloud.datastore._http._parse_commit_response',
new=mock_parse)
with patch:
result = conn.commit(PROJECT, req_pb, b'xact')

self.assertIs(result, expected_result)
Expand Down
Loading

0 comments on commit 85e8b5c

Please sign in to comment.