Skip to content

Commit

Permalink
Nuke used-only-once '_get_connection'.
Browse files Browse the repository at this point in the history
Addresses:
#979 (comment)
  • Loading branch information
tseaver committed Jul 13, 2015
1 parent e212d9b commit 1b0b19c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 33 deletions.
11 changes: 1 addition & 10 deletions gcloud/datastore/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,6 @@ def _determine_default_dataset_id(dataset_id=None):
return dataset_id


def _get_connection():
"""Shortcut method to establish a connection to the Cloud Datastore.
:rtype: :class:`gcloud.datastore.connection.Connection`
:returns: A connection defined with the proper credentials.
"""
return Connection.from_environment()


def _extended_lookup(connection, dataset_id, key_pbs,
missing=None, deferred=None,
eventual=False, transaction_id=None):
Expand Down Expand Up @@ -186,7 +177,7 @@ def __init__(self, dataset_id=None, namespace=None, connection=None):
raise EnvironmentError('Dataset ID could not be inferred.')
self.dataset_id = dataset_id
if connection is None:
connection = _get_connection()
connection = Connection.from_environment()
self.connection = connection
self._batch_stack = _LocalStack()
self.namespace = namespace
Expand Down
30 changes: 7 additions & 23 deletions gcloud/datastore/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,28 +165,6 @@ def test_gce(self):
['prod_mock', 'gcd_mock', 'gae_mock', 'gce_mock'])


class Test__get_connection(unittest2.TestCase):

def _callFUT(self):
from gcloud.datastore.client import _get_connection
return _get_connection()

def test_it(self):
from gcloud import credentials
from gcloud.datastore.connection import SCOPE
from gcloud.datastore.connection import Connection
from gcloud.test_credentials import _Client
from gcloud._testing import _Monkey

client = _Client()
with _Monkey(credentials, client=client):
found = self._callFUT()
self.assertTrue(isinstance(found, Connection))
self.assertTrue(found._credentials is client._signed)
self.assertEqual(found._credentials._scopes, SCOPE)
self.assertTrue(client._get_app_default_called)


class TestClient(unittest2.TestCase):

DATASET_ID = 'DATASET'
Expand All @@ -211,10 +189,16 @@ def test_ctor_w_implicit_inputs(self):

OTHER = 'other'
conn = object()

class _Connection(object):
@classmethod
def from_environment(cls):
return conn

klass = self._getTargetClass()
with _Monkey(_MUT,
_determine_default_dataset_id=lambda x: x or OTHER,
_get_connection=lambda: conn):
Connection=_Connection):
client = klass()
self.assertEqual(client.dataset_id, OTHER)
self.assertEqual(client.namespace, None)
Expand Down

0 comments on commit 1b0b19c

Please sign in to comment.