Skip to content

Commit

Permalink
Moving set_default_connection() to _implicit_environ.
Browse files Browse the repository at this point in the history
Moved from datastore.__init__.
  • Loading branch information
dhermes committed Feb 19, 2015
1 parent 449fd97 commit 3872413
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 47 deletions.
12 changes: 1 addition & 11 deletions gcloud/datastore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
when race conditions may occur.
"""

from gcloud.datastore import _implicit_environ
from gcloud.datastore._implicit_environ import SCOPE
from gcloud.datastore._implicit_environ import get_connection
from gcloud.datastore._implicit_environ import get_default_connection
from gcloud.datastore._implicit_environ import get_default_dataset_id
from gcloud.datastore._implicit_environ import set_default_connection
from gcloud.datastore._implicit_environ import set_default_dataset_id
from gcloud.datastore.api import allocate_ids
from gcloud.datastore.api import delete
Expand All @@ -64,16 +64,6 @@
from gcloud.datastore.transaction import Transaction


def set_default_connection(connection=None):
"""Set default connection either explicitly or implicitly as fall-back.
:type connection: :class:`gcloud.datastore.connection.Connection`
:param connection: A connection provided to be the default.
"""
connection = connection or get_connection()
_implicit_environ._DEFAULTS.connection = connection


def set_defaults(dataset_id=None, connection=None):
"""Set defaults either explicitly or implicitly as fall-back.
Expand Down
10 changes: 10 additions & 0 deletions gcloud/datastore/_implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ def get_connection():
return Connection(credentials=scoped_credentials)


def set_default_connection(connection=None):
"""Set default connection either explicitly or implicitly as fall-back.
:type connection: :class:`gcloud.datastore.connection.Connection`
:param connection: A connection provided to be the default.
"""
connection = connection or get_connection()
_DEFAULTS.connection = connection


def get_default_connection():
"""Get default connection.
Expand Down
36 changes: 0 additions & 36 deletions gcloud/datastore/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,6 @@
import unittest2


class Test_set_default_connection(unittest2.TestCase):

def setUp(self):
from gcloud.datastore._testing import _setup_defaults
_setup_defaults(self)

def tearDown(self):
from gcloud.datastore._testing import _tear_down_defaults
_tear_down_defaults(self)

def _callFUT(self, connection=None):
from gcloud.datastore import set_default_connection
return set_default_connection(connection=connection)

def test_set_explicit(self):
from gcloud.datastore import _implicit_environ

self.assertEqual(_implicit_environ.get_default_connection(), None)
fake_cnxn = object()
self._callFUT(connection=fake_cnxn)
self.assertEqual(_implicit_environ.get_default_connection(), fake_cnxn)

def test_set_implicit(self):
from gcloud._testing import _Monkey
from gcloud import datastore
from gcloud.datastore import _implicit_environ

self.assertEqual(_implicit_environ.get_default_connection(), None)

fake_cnxn = object()
with _Monkey(datastore, get_connection=lambda: fake_cnxn):
self._callFUT()

self.assertEqual(_implicit_environ.get_default_connection(), fake_cnxn)


class Test_set_defaults(unittest2.TestCase):

def _callFUT(self, dataset_id=None, connection=None):
Expand Down
35 changes: 35 additions & 0 deletions gcloud/datastore/test__implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,41 @@ def test_it(self):
self.assertTrue(client._get_app_default_called)


class Test_set_default_connection(unittest2.TestCase):

def setUp(self):
from gcloud.datastore._testing import _setup_defaults
_setup_defaults(self)

def tearDown(self):
from gcloud.datastore._testing import _tear_down_defaults
_tear_down_defaults(self)

def _callFUT(self, connection=None):
from gcloud.datastore._implicit_environ import set_default_connection
return set_default_connection(connection=connection)

def test_set_explicit(self):
from gcloud.datastore import _implicit_environ

self.assertEqual(_implicit_environ.get_default_connection(), None)
fake_cnxn = object()
self._callFUT(connection=fake_cnxn)
self.assertEqual(_implicit_environ.get_default_connection(), fake_cnxn)

def test_set_implicit(self):
from gcloud._testing import _Monkey
from gcloud.datastore import _implicit_environ

self.assertEqual(_implicit_environ.get_default_connection(), None)

fake_cnxn = object()
with _Monkey(_implicit_environ, get_connection=lambda: fake_cnxn):
self._callFUT()

self.assertEqual(_implicit_environ.get_default_connection(), fake_cnxn)


class _AppIdentity(object):

def __init__(self, app_id):
Expand Down

0 comments on commit 3872413

Please sign in to comment.