Skip to content

Commit

Permalink
Adding lazy loading property for dataset ID in Environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Feb 17, 2015
1 parent 83e3d9f commit 662ad4a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
27 changes: 22 additions & 5 deletions gcloud/datastore/_implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,31 @@
_DATASET_ENV_VAR_NAME = 'GCLOUD_DATASET_ID'


class _DatasetIDProperty(object):
"""Descriptor for lazy loaded dataset ID."""

def __get__(self, obj, objtype):
if obj is None or objtype is not Environment:
return self

obj.dataset_id = get_default_dataset_id()
return obj.dataset_id


class Environment(object):
"""Container for environment settings."""
"""Container for environment settings.
:type dataset_id: string
:param dataset_id: Persistent implied dataset ID from environment.
:type connection: :class:`gcloud.datastore.connection.Connection`
:param connection: Persistent implied connection from environment.
"""

dataset_id = None
"""Attribute to allow persistent implied dataset ID from environment."""
dataset_id = _DatasetIDProperty()

connection = None
"""Attribute to allow persistent implied connection from environment."""
def __init__(self):
self.connection = None


def app_engine_id():
Expand Down
50 changes: 50 additions & 0 deletions gcloud/datastore/test__implicit_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,56 @@ def test_set_implicit_three_env_appengine_and_compute(self):
self.assertEqual(connection.timeout, None)


class Test_lazy_loaded_dataset_id(unittest2.TestCase):

def setUp(self):
from gcloud.datastore import _implicit_environ
self._replaced_default = _implicit_environ.DEFAULT_ENVIRON
_implicit_environ.DEFAULT_ENVIRON = _implicit_environ.Environment()

def tearDown(self):
from gcloud.datastore import _implicit_environ
_implicit_environ.DEFAULT_ENVIRON = self._replaced_default

def test_prop_default(self):
from gcloud.datastore import _implicit_environ
from gcloud.datastore._implicit_environ import Environment

self.assertTrue(isinstance(Environment.dataset_id,
_implicit_environ._DatasetIDProperty))
self.assertEqual(_implicit_environ.DEFAULT_ENVIRON.dataset_id, None)

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

data_prop = _implicit_environ._DatasetIDProperty()

class FakeEnv(object):
dataset_id = data_prop

self.assertTrue(FakeEnv.dataset_id is data_prop)
self.assertTrue(FakeEnv().dataset_id is data_prop)

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

self.assertFalse(
'dataset_id' in _implicit_environ.DEFAULT_ENVIRON.__dict__)

DEFAULT = object()

def mock_default():
return DEFAULT

with _Monkey(_implicit_environ, get_default_dataset_id=mock_default):
lazy_loaded = _implicit_environ.DEFAULT_ENVIRON.dataset_id

self.assertEqual(lazy_loaded, DEFAULT)
self.assertTrue(
'dataset_id' in _implicit_environ.DEFAULT_ENVIRON.__dict__)


class _AppIdentity(object):

def __init__(self, app_id):
Expand Down

0 comments on commit 662ad4a

Please sign in to comment.