Skip to content

Commit

Permalink
Replacing _implicit_environ module globals with a container.
Browse files Browse the repository at this point in the history
This is so that a container instance can have lazily loaded
properties.
  • Loading branch information
dhermes committed Feb 19, 2015
1 parent b3be71e commit b0cdca6
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 82 deletions.
4 changes: 2 additions & 2 deletions gcloud/datastore/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def set_default_dataset_id(dataset_id=None):
dataset_id = _implicit_environ.compute_engine_id()

if dataset_id is not None:
_implicit_environ.DATASET_ID = dataset_id
_implicit_environ._DEFAULTS.dataset_id = dataset_id
else:
raise EnvironmentError('No dataset ID could be inferred.')

Expand All @@ -111,7 +111,7 @@ def set_default_connection(connection=None):
:param connection: A connection provided to be the default.
"""
connection = connection or get_connection()
_implicit_environ.CONNECTION = connection
_implicit_environ._DEFAULTS.connection = connection


def set_defaults(dataset_id=None, connection=None):
Expand Down
23 changes: 17 additions & 6 deletions gcloud/datastore/_implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,19 @@
app_identity = None


DATASET_ID = None
"""Module global to allow persistent implied dataset ID from enviroment."""
class _DefaultsContainer(object):
"""Container for defaults.
CONNECTION = None
"""Module global to allow persistent implied connection from enviroment."""
:type connection: :class:`gcloud.datastore.connection.Connection`
:param connection: Persistent implied connection from environment.
:type dataset_id: string
:param dataset_id: Persistent implied dataset ID from environment.
"""

def __init__(self, connection=None, dataset_id=None):
self.connection = connection
self.dataset_id = dataset_id


def app_engine_id():
Expand Down Expand Up @@ -87,7 +95,7 @@ def get_default_connection():
:rtype: :class:`gcloud.datastore.connection.Connection` or ``NoneType``
:returns: The default connection if one has been set.
"""
return CONNECTION
return _DEFAULTS.connection


def get_default_dataset_id():
Expand All @@ -96,4 +104,7 @@ def get_default_dataset_id():
:rtype: string or ``NoneType``
:returns: The default dataset ID if one has been set.
"""
return DATASET_ID
return _DEFAULTS.dataset_id


_DEFAULTS = _DefaultsContainer()
64 changes: 38 additions & 26 deletions gcloud/datastore/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ class Test_set_default_dataset_id(unittest2.TestCase):

def setUp(self):
from gcloud.datastore import _implicit_environ
self._replaced_dataset_id = _implicit_environ.DATASET_ID
_implicit_environ.DATASET_ID = None
self._replaced_defaults = _implicit_environ._DEFAULTS
_implicit_environ._DEFAULTS = _implicit_environ._DefaultsContainer(
None, None)

def tearDown(self):
from gcloud.datastore import _implicit_environ
_implicit_environ.DATASET_ID = self._replaced_dataset_id
_implicit_environ._DEFAULTS = self._replaced_defaults

def _callFUT(self, dataset_id=None):
from gcloud.datastore import set_default_dataset_id
Expand Down Expand Up @@ -60,7 +61,7 @@ def test_no_env_var_set(self):
with self._monkeyImplicit():
self.assertRaises(EnvironmentError, self._callFUT)

self.assertEqual(_implicit_environ.DATASET_ID, None)
self.assertEqual(_implicit_environ.get_default_dataset_id(), None)

def test_set_from_env_var(self):
from gcloud.datastore import _implicit_environ
Expand All @@ -70,7 +71,8 @@ def test_set_from_env_var(self):
with self._monkeyImplicit():
self._callFUT()

self.assertEqual(_implicit_environ.DATASET_ID, IMPLICIT_DATASET_ID)
self.assertEqual(_implicit_environ.get_default_dataset_id(),
IMPLICIT_DATASET_ID)

def test_set_explicit_w_env_var_set(self):
from gcloud.datastore import _implicit_environ
Expand All @@ -80,7 +82,8 @@ def test_set_explicit_w_env_var_set(self):
with self._monkeyImplicit():
self._callFUT(EXPLICIT_DATASET_ID)

self.assertEqual(_implicit_environ.DATASET_ID, EXPLICIT_DATASET_ID)
self.assertEqual(_implicit_environ.get_default_dataset_id(),
EXPLICIT_DATASET_ID)

def test_set_explicit_no_env_var_set(self):
from gcloud.datastore import _implicit_environ
Expand All @@ -91,7 +94,8 @@ def test_set_explicit_no_env_var_set(self):
with self._monkeyImplicit():
self._callFUT(EXPLICIT_DATASET_ID)

self.assertEqual(_implicit_environ.DATASET_ID, EXPLICIT_DATASET_ID)
self.assertEqual(_implicit_environ.get_default_dataset_id(),
EXPLICIT_DATASET_ID)

def test_set_explicit_None_wo_env_var_set(self):
from gcloud.datastore import _implicit_environ
Expand All @@ -100,7 +104,7 @@ def test_set_explicit_None_wo_env_var_set(self):
with self._monkeyImplicit():
self.assertRaises(EnvironmentError, self._callFUT, None)

self.assertEqual(_implicit_environ.DATASET_ID, None)
self.assertEqual(_implicit_environ.get_default_dataset_id(), None)

def test_set_explicit_None_w_env_var_set(self):
from gcloud.datastore import _implicit_environ
Expand All @@ -110,7 +114,8 @@ def test_set_explicit_None_w_env_var_set(self):
with self._monkeyImplicit():
self._callFUT(None)

self.assertEqual(_implicit_environ.DATASET_ID, IMPLICIT_DATASET_ID)
self.assertEqual(_implicit_environ.get_default_dataset_id(),
IMPLICIT_DATASET_ID)

def test_set_from_gcd_env_var(self):
from gcloud.datastore import _GCD_DATASET_ENV_VAR_NAME
Expand All @@ -123,7 +128,8 @@ def test_set_from_gcd_env_var(self):
with self._monkeyImplicit():
self._callFUT()

self.assertEqual(_implicit_environ.DATASET_ID, GCD_DATASET_ID)
self.assertEqual(_implicit_environ.get_default_dataset_id(),
GCD_DATASET_ID)

def test_set_gcd_and_production_env_vars(self):
from gcloud.datastore import _DATASET_ENV_VAR_NAME
Expand All @@ -141,8 +147,8 @@ def test_set_gcd_and_production_env_vars(self):
with self._monkeyImplicit():
self._callFUT()

self.assertNotEqual(_implicit_environ.DATASET_ID, GCD_DATASET_ID)
self.assertEqual(_implicit_environ.DATASET_ID, IMPLICIT_DATASET_ID)
self.assertEqual(_implicit_environ.get_default_dataset_id(),
IMPLICIT_DATASET_ID)

def test_set_gcd_env_vars_and_appengine(self):
from gcloud.datastore import _GCD_DATASET_ENV_VAR_NAME
Expand All @@ -158,8 +164,8 @@ def test_set_gcd_env_vars_and_appengine(self):
with self._monkeyImplicit(app_identity=APP_IDENTITY):
self._callFUT()

self.assertNotEqual(_implicit_environ.DATASET_ID, APP_ENGINE_ID)
self.assertEqual(_implicit_environ.DATASET_ID, GCD_DATASET_ID)
self.assertEqual(_implicit_environ.get_default_dataset_id(),
GCD_DATASET_ID)

def test_set_implicit_from_appengine(self):
from gcloud.datastore import _implicit_environ
Expand All @@ -171,7 +177,8 @@ def test_set_implicit_from_appengine(self):
with self._monkeyImplicit(app_identity=APP_IDENTITY):
self._callFUT()

self.assertEqual(_implicit_environ.DATASET_ID, APP_ENGINE_ID)
self.assertEqual(_implicit_environ.get_default_dataset_id(),
APP_ENGINE_ID)

def test_set_implicit_both_env_and_appengine(self):
from gcloud.datastore import _implicit_environ
Expand All @@ -183,7 +190,8 @@ def test_set_implicit_both_env_and_appengine(self):
with self._monkeyImplicit(app_identity=APP_IDENTITY):
self._callFUT()

self.assertEqual(_implicit_environ.DATASET_ID, IMPLICIT_DATASET_ID)
self.assertEqual(_implicit_environ.get_default_dataset_id(),
IMPLICIT_DATASET_ID)

def _implicit_compute_engine_helper(self, status):
from gcloud.datastore import _implicit_environ
Expand All @@ -206,7 +214,8 @@ def _implicit_compute_engine_helper(self, status):
else:
self._callFUT()

self.assertEqual(_implicit_environ.DATASET_ID, EXPECTED_ID)
self.assertEqual(_implicit_environ.get_default_dataset_id(),
EXPECTED_ID)
self.assertEqual(connection.host, '169.254.169.254')
self.assertEqual(connection.timeout, 0.1)
self.assertEqual(
Expand Down Expand Up @@ -241,7 +250,8 @@ def test_set_implicit_both_appengine_and_compute(self):
app_identity=APP_IDENTITY):
self._callFUT()

self.assertEqual(_implicit_environ.DATASET_ID, APP_ENGINE_ID)
self.assertEqual(_implicit_environ.get_default_dataset_id(),
APP_ENGINE_ID)
self.assertEqual(connection.host, None)
self.assertEqual(connection.timeout, None)

Expand All @@ -257,7 +267,8 @@ def test_set_implicit_three_env_appengine_and_compute(self):
app_identity=APP_IDENTITY):
self._callFUT()

self.assertEqual(_implicit_environ.DATASET_ID, IMPLICIT_DATASET_ID)
self.assertEqual(_implicit_environ.get_default_dataset_id(),
IMPLICIT_DATASET_ID)
self.assertEqual(connection.host, None)
self.assertEqual(connection.timeout, None)

Expand All @@ -266,12 +277,13 @@ class Test_set_default_connection(unittest2.TestCase):

def setUp(self):
from gcloud.datastore import _implicit_environ
self._replaced_connection = _implicit_environ.CONNECTION
_implicit_environ.CONNECTION = None
self._replaced_defaults = _implicit_environ._DEFAULTS
_implicit_environ._DEFAULTS = _implicit_environ._DefaultsContainer(
None, None)

def tearDown(self):
from gcloud.datastore import _implicit_environ
_implicit_environ.CONNECTION = self._replaced_connection
_implicit_environ._DEFAULTS = self._replaced_defaults

def _callFUT(self, connection=None):
from gcloud.datastore import set_default_connection
Expand All @@ -280,23 +292,23 @@ def _callFUT(self, connection=None):
def test_set_explicit(self):
from gcloud.datastore import _implicit_environ

self.assertEqual(_implicit_environ.CONNECTION, None)
self.assertEqual(_implicit_environ.get_default_connection(), None)
fake_cnxn = object()
self._callFUT(connection=fake_cnxn)
self.assertEqual(_implicit_environ.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.CONNECTION, None)
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.CONNECTION, fake_cnxn)
self.assertEqual(_implicit_environ.get_default_connection(), fake_cnxn)


class Test_set_defaults(unittest2.TestCase):
Expand Down
6 changes: 4 additions & 2 deletions gcloud/datastore/test__implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def test_preset(self):
from gcloud.datastore import _implicit_environ

SENTINEL = object()
with _Monkey(_implicit_environ, CONNECTION=SENTINEL):
MOCK_DEFAULTS = _implicit_environ._DefaultsContainer(SENTINEL, None)
with _Monkey(_implicit_environ, _DEFAULTS=MOCK_DEFAULTS):
self.assertEqual(self._callFUT(), SENTINEL)


Expand All @@ -47,5 +48,6 @@ def test_preset(self):
from gcloud.datastore import _implicit_environ

SENTINEL = object()
with _Monkey(_implicit_environ, DATASET_ID=SENTINEL):
MOCK_DEFAULTS = _implicit_environ._DefaultsContainer(None, SENTINEL)
with _Monkey(_implicit_environ, _DEFAULTS=MOCK_DEFAULTS):
self.assertEqual(self._callFUT(), SENTINEL)
Loading

0 comments on commit b0cdca6

Please sign in to comment.