From 662ad4a2c93defd48b845d2ebb6c06f704707d24 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Mon, 16 Feb 2015 22:26:54 -0800 Subject: [PATCH] Adding lazy loading property for dataset ID in Environment. --- gcloud/datastore/_implicit_environ.py | 27 +++++++++--- gcloud/datastore/test__implicit_environ.py | 50 ++++++++++++++++++++++ 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/gcloud/datastore/_implicit_environ.py b/gcloud/datastore/_implicit_environ.py index 78783f8cb735..05982910ea08 100644 --- a/gcloud/datastore/_implicit_environ.py +++ b/gcloud/datastore/_implicit_environ.py @@ -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(): diff --git a/gcloud/datastore/test__implicit_environ.py b/gcloud/datastore/test__implicit_environ.py index 6fd9b314dce3..29292c954f9c 100644 --- a/gcloud/datastore/test__implicit_environ.py +++ b/gcloud/datastore/test__implicit_environ.py @@ -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):