Skip to content

Commit

Permalink
Removing get_credentials() from core. (googleapis#3667)
Browse files Browse the repository at this point in the history
* Removing `get_credentials()` from `core`.

In the process also:

- Slight re-org on `nox.py` config (to pass posargs) for `core`
  and `datastore`
- Getting rid of last usage of `_Monkey` in datastore

This is part of `@jonparrott`'s effort to slim down / stabilize
`core`.

* Removing `google.cloud.credentials` module from docs.
  • Loading branch information
dhermes authored and landrito committed Aug 22, 2017
1 parent bdb612e commit d38fa8d
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 175 deletions.
4 changes: 2 additions & 2 deletions bigtable/google/cloud/bigtable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import os

import google.auth
import google.auth.credentials
from google.gax.utils import metrics
from google.longrunning import operations_grpc
Expand All @@ -40,7 +41,6 @@
from google.cloud._http import DEFAULT_USER_AGENT
from google.cloud.client import _ClientFactoryMixin
from google.cloud.client import _ClientProjectMixin
from google.cloud.credentials import get_credentials
from google.cloud.environment_vars import BIGTABLE_EMULATOR

from google.cloud.bigtable import __version__
Expand Down Expand Up @@ -211,7 +211,7 @@ def __init__(self, project=None, credentials=None,
read_only=False, admin=False, user_agent=DEFAULT_USER_AGENT):
_ClientProjectMixin.__init__(self, project=project)
if credentials is None:
credentials = get_credentials()
credentials, _ = google.auth.default()

if read_only and admin:
raise ValueError('A read-only client cannot also perform'
Expand Down
13 changes: 6 additions & 7 deletions bigtable/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,20 +360,19 @@ def test_constructor_both_admin_and_read_only(self):
read_only=True)

def test_constructor_implicit_credentials(self):
from google.cloud._testing import _Monkey
from google.cloud.bigtable import client as MUT
from google.cloud.bigtable.client import DATA_SCOPE

creds = _make_credentials()
expected_scopes = [MUT.DATA_SCOPE]

def mock_get_credentials():
return creds
expected_scopes = [DATA_SCOPE]

with _Monkey(MUT, get_credentials=mock_get_credentials):
patch = mock.patch(
'google.auth.default', return_value=(creds, None))
with patch as default:
self._constructor_test_helper(
None, None,
expected_creds=creds.with_scopes.return_value)

default.assert_called_once_with()
creds.with_scopes.assert_called_once_with(expected_scopes)

def test_constructor_credentials_wo_create_scoped(self):
Expand Down
4 changes: 2 additions & 2 deletions core/google/cloud/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import google_auth_httplib2
import six

import google.auth
import google.auth.credentials
from google.cloud._helpers import _determine_default_project
from google.cloud.credentials import get_credentials
from google.oauth2 import service_account


Expand Down Expand Up @@ -135,7 +135,7 @@ def __init__(self, credentials=None, _http=None):
credentials, google.auth.credentials.Credentials)):
raise ValueError(_GOOGLE_AUTH_CREDENTIALS_HELP)
if credentials is None and _http is None:
credentials = get_credentials()
credentials, _ = google.auth.default()
self._credentials = google.auth.credentials.with_scopes_if_required(
credentials, self.SCOPE)
self._http_internal = _http
Expand Down
30 changes: 0 additions & 30 deletions core/google/cloud/credentials.py

This file was deleted.

23 changes: 17 additions & 6 deletions core/nox.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

from __future__ import absolute_import
import os

import nox

Expand All @@ -29,16 +30,26 @@ def unit_tests(session, python_version):
session.virtualenv_dirname = 'unit-' + python_version

# Install all test dependencies, then install this package in-place.
session.install('mock', 'pytest', 'pytest-cov',
'grpcio >= 1.0.2')
session.install(
'mock',
'pytest',
'pytest-cov',
'grpcio >= 1.0.2',
)
session.install('-e', '.')

# Run py.test against the unit tests.
session.run(
'py.test', '--quiet',
'--cov=google.cloud', '--cov=tests.unit', '--cov-append',
'--cov-config=.coveragerc', '--cov-report=', '--cov-fail-under=97',
'tests/unit',
'py.test',
'--quiet',
'--cov=google.cloud',
'--cov=tests.unit',
'--cov-append',
'--cov-config=.coveragerc',
'--cov-report=',
'--cov-fail-under=97',
os.path.join('tests', 'unit'),
*session.posargs
)


Expand Down
83 changes: 35 additions & 48 deletions core/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,37 +59,31 @@ def test_unpickleable(self):
with self.assertRaises(pickle.PicklingError):
pickle.dumps(client_obj)

def test_ctor_defaults(self):
from google.cloud._testing import _Monkey
from google.cloud import client

CREDENTIALS = _make_credentials()
FUNC_CALLS = []

def mock_get_credentials():
FUNC_CALLS.append('get_credentials')
return CREDENTIALS
def test_constructor_defaults(self):
credentials = _make_credentials()

with _Monkey(client, get_credentials=mock_get_credentials):
patch = mock.patch(
'google.auth.default', return_value=(credentials, None))
with patch as default:
client_obj = self._make_one()

self.assertIs(client_obj._credentials, CREDENTIALS)
self.assertIs(client_obj._credentials, credentials)
self.assertIsNone(client_obj._http_internal)
self.assertEqual(FUNC_CALLS, ['get_credentials'])
default.assert_called_once_with()

def test_ctor_explicit(self):
CREDENTIALS = _make_credentials()
HTTP = object()
client_obj = self._make_one(credentials=CREDENTIALS, _http=HTTP)
def test_constructor_explicit(self):
credentials = _make_credentials()
http = mock.sentinel.http
client_obj = self._make_one(credentials=credentials, _http=http)

self.assertIs(client_obj._credentials, CREDENTIALS)
self.assertIs(client_obj._http_internal, HTTP)
self.assertIs(client_obj._credentials, credentials)
self.assertIs(client_obj._http_internal, http)

def test_ctor_bad_credentials(self):
CREDENTIALS = object()
def test_constructor_bad_credentials(self):
credentials = mock.sentinel.credentials

with self.assertRaises(ValueError):
self._make_one(credentials=CREDENTIALS)
self._make_one(credentials=credentials)

def test_from_service_account_json(self):
from google.cloud import _helpers
Expand Down Expand Up @@ -162,34 +156,27 @@ def _get_target_class():
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_ctor_defaults(self):
from google.cloud._testing import _Monkey
from google.cloud import client

PROJECT = 'PROJECT'
CREDENTIALS = _make_credentials()
FUNC_CALLS = []

def mock_determine_proj(project):
FUNC_CALLS.append((project, '_determine_default_project'))
return PROJECT
def test_constructor_defaults(self):
credentials = _make_credentials()
patch1 = mock.patch(
'google.auth.default', return_value=(credentials, None))

def mock_get_credentials():
FUNC_CALLS.append('get_credentials')
return CREDENTIALS
project = 'prahj-ekt'
patch2 = mock.patch(
'google.cloud.client._determine_default_project',
return_value=project)

with _Monkey(client, get_credentials=mock_get_credentials,
_determine_default_project=mock_determine_proj):
client_obj = self._make_one()
with patch1 as default:
with patch2 as _determine_default_project:
client_obj = self._make_one()

self.assertEqual(client_obj.project, PROJECT)
self.assertIs(client_obj._credentials, CREDENTIALS)
self.assertEqual(client_obj.project, project)
self.assertIs(client_obj._credentials, credentials)
self.assertIsNone(client_obj._http_internal)
self.assertEqual(
FUNC_CALLS,
[(None, '_determine_default_project'), 'get_credentials'])
default.assert_called_once_with()
_determine_default_project.assert_called_once_with(None)

def test_ctor_missing_project(self):
def test_constructor_missing_project(self):
from google.cloud._testing import _Monkey
from google.cloud import client

Expand All @@ -204,7 +191,7 @@ def mock_determine_proj(project):

self.assertEqual(FUNC_CALLS, [(None, '_determine_default_project')])

def test_ctor_w_invalid_project(self):
def test_constructor_w_invalid_project(self):
CREDENTIALS = _make_credentials()
HTTP = object()
with self.assertRaises(ValueError):
Expand All @@ -227,11 +214,11 @@ def _explicit_ctor_helper(self, project):
self.assertIs(client_obj._credentials, CREDENTIALS)
self.assertIs(client_obj._http_internal, HTTP)

def test_ctor_explicit_bytes(self):
def test_constructor_explicit_bytes(self):
PROJECT = b'PROJECT'
self._explicit_ctor_helper(PROJECT)

def test_ctor_explicit_unicode(self):
def test_constructor_explicit_unicode(self):
PROJECT = u'PROJECT'
self._explicit_ctor_helper(PROJECT)

Expand Down
34 changes: 0 additions & 34 deletions core/tests/unit/test_credentials.py

This file was deleted.

15 changes: 11 additions & 4 deletions datastore/nox.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,17 @@ def unit_tests(session, python_version):
session.install('-e', '.')

# Run py.test against the unit tests.
session.run('py.test', '--quiet',
'--cov=google.cloud.datastore', '--cov=tests.unit', '--cov-append',
'--cov-config=.coveragerc', '--cov-report=', '--cov-fail-under=97',
'tests/unit',
session.run(
'py.test',
'--quiet',
'--cov=google.cloud.datastore',
'--cov=tests.unit',
'--cov-append',
'--cov-config=.coveragerc',
'--cov-report=',
'--cov-fail-under=97',
os.path.join('tests', 'unit'),
*session.posargs
)


Expand Down
18 changes: 7 additions & 11 deletions datastore/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,16 @@ def test_constructor_w_implicit_inputs(self):

other = 'other'
creds = _make_credentials()
default_called = []

def fallback_mock(project):
default_called.append(project)
return project or other

klass = self._get_target_class()
patch1 = mock.patch(
'google.cloud.datastore.client._determine_default_project',
new=fallback_mock)
return_value=other)
patch2 = mock.patch(
'google.cloud.client.get_credentials',
return_value=creds)
'google.auth.default', return_value=(creds, None))

with patch1:
with patch2:
with patch1 as _determine_default_project:
with patch2 as default:
client = klass()

self.assertEqual(client.project, other)
Expand All @@ -174,7 +168,9 @@ def fallback_mock(project):

self.assertIsNone(client.current_batch)
self.assertIsNone(client.current_transaction)
self.assertEqual(default_called, [None])

default.assert_called_once_with()
_determine_default_project.assert_called_once_with(None)

def test_constructor_w_explicit_inputs(self):
from google.cloud.datastore.client import _DATASTORE_BASE_URL
Expand Down
23 changes: 8 additions & 15 deletions datastore/tests/unit/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,21 +550,14 @@ def _call_fut(self, iterator, entity_pb):
return _item_to_entity(iterator, entity_pb)

def test_it(self):
from google.cloud._testing import _Monkey
from google.cloud.datastore import helpers

result = object()
entities = []

def mocked(entity_pb):
entities.append(entity_pb)
return result

entity_pb = object()
with _Monkey(helpers, entity_from_protobuf=mocked):
self.assertIs(result, self._call_fut(None, entity_pb))

self.assertEqual(entities, [entity_pb])
entity_pb = mock.sentinel.entity_pb
patch = mock.patch(
'google.cloud.datastore.helpers.entity_from_protobuf')
with patch as entity_from_protobuf:
result = self._call_fut(None, entity_pb)
self.assertIs(result, entity_from_protobuf.return_value)

entity_from_protobuf.assert_called_once_with(entity_pb)


class Test__pb_from_query(unittest.TestCase):
Expand Down
Loading

0 comments on commit d38fa8d

Please sign in to comment.