| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -95,49 +95,6 @@ def _ensure_tuple_or_list(arg_name, tuple_or_list): | |||
| 95 | 95 | return list(tuple_or_list) | |
| 96 | 96 | ||
| 97 | 97 | ||
| 98 | - class _LazyProperty(object): | ||
| 99 | - """Descriptor for lazy loaded property. | ||
| 100 | - | ||
| 101 | - This follows the reify pattern: lazy evaluation and then replacement | ||
| 102 | - after evaluation. | ||
| 103 | - | ||
| 104 | - :type name: string | ||
| 105 | - :param name: The name of the attribute / property being evaluated. | ||
| 106 | - | ||
| 107 | - :type deferred_callable: callable that takes no arguments | ||
| 108 | - :param deferred_callable: The function / method used to evaluate the | ||
| 109 | - property. | ||
| 110 | - """ | ||
| 111 | - | ||
| 112 | - def __init__(self, name, deferred_callable): | ||
| 113 | - self._name = name | ||
| 114 | - self._deferred_callable = deferred_callable | ||
| 115 | - | ||
| 116 | - def __get__(self, obj, objtype): | ||
| 117 | - if obj is None: | ||
| 118 | - return self | ||
| 119 | - | ||
| 120 | - setattr(obj, self._name, self._deferred_callable()) | ||
| 121 | - return getattr(obj, self._name) | ||
| 122 | - | ||
| 123 | - | ||
| 124 | - def _lazy_property_deco(deferred_callable): | ||
| 125 | - """Decorator a method to create a :class:`_LazyProperty`. | ||
| 126 | - | ||
| 127 | - :type deferred_callable: callable that takes no arguments | ||
| 128 | - :param deferred_callable: The function / method used to evaluate the | ||
| 129 | - property. | ||
| 130 | - | ||
| 131 | - :rtype: :class:`_LazyProperty`. | ||
| 132 | - :returns: A lazy property which defers the deferred_callable. | ||
| 133 | - """ | ||
| 134 | - if isinstance(deferred_callable, staticmethod): | ||
| 135 | - # H/T: http://stackoverflow.com/a/9527450/1068170 | ||
| 136 | - # For Python2.7+ deferred_callable.__func__ would suffice. | ||
| 137 | - deferred_callable = deferred_callable.__get__(True) | ||
| 138 | - return _LazyProperty(deferred_callable.__name__, deferred_callable) | ||
| 139 | - | ||
| 140 | - | ||
| 141 | 98 | def _app_engine_id(): | |
| 142 | 99 | """Gets the App Engine application ID if it can be inferred. | |
| 143 | 100 | ||
@@ -211,44 +168,3 @@ def _determine_default_project(project=None): | |||
| 211 | 168 | project = _get_production_project() | |
| 212 | 169 | ||
| 213 | 170 | return project | |
| 214 | - | ||
| 215 | - | ||
| 216 | - def set_default_project(project=None): | ||
| 217 | - """Set default project either explicitly or implicitly as fall-back. | ||
| 218 | - | ||
| 219 | - :type project: string | ||
| 220 | - :param project: Optional. The project name to use as default. | ||
| 221 | - | ||
| 222 | - :raises: :class:`EnvironmentError` if no project was found. | ||
| 223 | - """ | ||
| 224 | - project = _determine_default_project(project=project) | ||
| 225 | - if project is not None: | ||
| 226 | - _DEFAULTS.project = project | ||
| 227 | - else: | ||
| 228 | - raise EnvironmentError('No project could be inferred.') | ||
| 229 | - | ||
| 230 | - | ||
| 231 | - class _DefaultsContainer(object): | ||
| 232 | - """Container for defaults. | ||
| 233 | - | ||
| 234 | - :type project: string | ||
| 235 | - :param project: Persistent implied project from environment. | ||
| 236 | - | ||
| 237 | - :type implicit: boolean | ||
| 238 | - :param implicit: if False, assign the instance's ``project`` attribute | ||
| 239 | - unconditionally; otherwise, assign it only if the | ||
| 240 | - value is not None. | ||
| 241 | - """ | ||
| 242 | - | ||
| 243 | - @_lazy_property_deco | ||
| 244 | - @staticmethod | ||
| 245 | - def project(): | ||
| 246 | - """Return the implicit default project.""" | ||
| 247 | - return _determine_default_project() | ||
| 248 | - | ||
| 249 | - def __init__(self, project=None, implicit=False): | ||
| 250 | - if project is not None or not implicit: | ||
| 251 | - self.project = project | ||
| 252 | - | ||
| 253 | - | ||
| 254 | - _DEFAULTS = _DefaultsContainer(implicit=True) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,9 +14,6 @@ | |||
| 14 | 14 | ||
| 15 | 15 | """Shared testing utilities.""" | |
| 16 | 16 | ||
| 17 | - from gcloud import _helpers | ||
| 18 | - from gcloud._helpers import _DefaultsContainer | ||
| 19 | - | ||
| 20 | 17 | ||
| 21 | 18 | class _Monkey(object): | |
| 22 | 19 | # context-manager for replacing module names in the scope of a test. | |
@@ -33,12 +30,3 @@ def __enter__(self): | |||
| 33 | 30 | def __exit__(self, exc_type, exc_val, exc_tb): | |
| 34 | 31 | for key, value in self.to_restore.items(): | |
| 35 | 32 | setattr(self.module, key, value) | |
| 36 | - | ||
| 37 | - | ||
| 38 | - def _setup_defaults(test_case, *args, **kwargs): | ||
| 39 | - test_case._replaced_defaults = _helpers._DEFAULTS | ||
| 40 | - _helpers._DEFAULTS = _DefaultsContainer(*args, **kwargs) | ||
| 41 | - | ||
| 42 | - | ||
| 43 | - def _tear_down_defaults(test_case): | ||
| 44 | - _helpers._DEFAULTS = test_case._replaced_defaults | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -70,58 +70,6 @@ def test_invalid_iterable(self): | |||
| 70 | 70 | self._callFUT('ARGNAME', invalid_tuple_or_list) | |
| 71 | 71 | ||
| 72 | 72 | ||
| 73 | - class Test__LazyProperty(unittest2.TestCase): | ||
| 74 | - | ||
| 75 | - def _getTargetClass(self): | ||
| 76 | - from gcloud._helpers import _LazyProperty | ||
| 77 | - return _LazyProperty | ||
| 78 | - | ||
| 79 | - def _makeOne(self, *args, **kwargs): | ||
| 80 | - return self._getTargetClass()(*args, **kwargs) | ||
| 81 | - | ||
| 82 | - def test_prop_on_class(self): | ||
| 83 | - # Don't actually need a callable for ``method`` since | ||
| 84 | - # __get__ will just return ``self`` in this test. | ||
| 85 | - data_prop = self._makeOne('dataset_id', None) | ||
| 86 | - | ||
| 87 | - class FakeEnv(object): | ||
| 88 | - dataset_id = data_prop | ||
| 89 | - | ||
| 90 | - self.assertTrue(FakeEnv.dataset_id is data_prop) | ||
| 91 | - | ||
| 92 | - def test_prop_on_instance(self): | ||
| 93 | - RESULT = object() | ||
| 94 | - data_prop = self._makeOne('dataset_id', lambda: RESULT) | ||
| 95 | - | ||
| 96 | - class FakeEnv(object): | ||
| 97 | - dataset_id = data_prop | ||
| 98 | - | ||
| 99 | - self.assertTrue(FakeEnv().dataset_id is RESULT) | ||
| 100 | - | ||
| 101 | - | ||
| 102 | - class Test__lazy_property_deco(unittest2.TestCase): | ||
| 103 | - | ||
| 104 | - def _callFUT(self, deferred_callable): | ||
| 105 | - from gcloud._helpers import _lazy_property_deco | ||
| 106 | - return _lazy_property_deco(deferred_callable) | ||
| 107 | - | ||
| 108 | - def test_on_function(self): | ||
| 109 | - def test_func(): | ||
| 110 | - pass # pragma: NO COVER never gets called | ||
| 111 | - | ||
| 112 | - lazy_prop = self._callFUT(test_func) | ||
| 113 | - self.assertTrue(lazy_prop._deferred_callable is test_func) | ||
| 114 | - self.assertEqual(lazy_prop._name, 'test_func') | ||
| 115 | - | ||
| 116 | - def test_on_staticmethod(self): | ||
| 117 | - def test_func(): | ||
| 118 | - pass # pragma: NO COVER never gets called | ||
| 119 | - | ||
| 120 | - lazy_prop = self._callFUT(staticmethod(test_func)) | ||
| 121 | - self.assertTrue(lazy_prop._deferred_callable is test_func) | ||
| 122 | - self.assertEqual(lazy_prop._name, 'test_func') | ||
| 123 | - | ||
| 124 | - | ||
| 125 | 73 | class Test__app_engine_id(unittest2.TestCase): | |
| 126 | 74 | ||
| 127 | 75 | def _callFUT(self): | |
@@ -254,81 +202,6 @@ def test_prod(self): | |||
| 254 | 202 | self.assertEqual(callers, ['prod_mock']) | |
| 255 | 203 | ||
| 256 | 204 | ||
| 257 | - class Test_set_default_project(unittest2.TestCase): | ||
| 258 | - | ||
| 259 | - def setUp(self): | ||
| 260 | - from gcloud._testing import _setup_defaults | ||
| 261 | - _setup_defaults(self) | ||
| 262 | - | ||
| 263 | - def tearDown(self): | ||
| 264 | - from gcloud._testing import _tear_down_defaults | ||
| 265 | - _tear_down_defaults(self) | ||
| 266 | - | ||
| 267 | - def _callFUT(self, project=None): | ||
| 268 | - from gcloud._helpers import set_default_project | ||
| 269 | - return set_default_project(project=project) | ||
| 270 | - | ||
| 271 | - def test_raises(self): | ||
| 272 | - from gcloud._testing import _Monkey | ||
| 273 | - from gcloud import _helpers | ||
| 274 | - _called_project = [] | ||
| 275 | - | ||
| 276 | - def mock_determine(project): | ||
| 277 | - _called_project.append(project) | ||
| 278 | - return None | ||
| 279 | - | ||
| 280 | - with _Monkey(_helpers, _determine_default_project=mock_determine): | ||
| 281 | - self.assertRaises(EnvironmentError, self._callFUT) | ||
| 282 | - | ||
| 283 | - self.assertEqual(_called_project, [None]) | ||
| 284 | - | ||
| 285 | - def test_set_correctly(self): | ||
| 286 | - from gcloud._testing import _Monkey | ||
| 287 | - from gcloud import _helpers | ||
| 288 | - | ||
| 289 | - self.assertEqual(_helpers._DEFAULTS.project, None) | ||
| 290 | - | ||
| 291 | - PROJECT = object() | ||
| 292 | - _called_project = [] | ||
| 293 | - | ||
| 294 | - def mock_determine(project): | ||
| 295 | - _called_project.append(project) | ||
| 296 | - return PROJECT | ||
| 297 | - | ||
| 298 | - with _Monkey(_helpers, | ||
| 299 | - _determine_default_project=mock_determine): | ||
| 300 | - self._callFUT() | ||
| 301 | - | ||
| 302 | - self.assertEqual(_helpers._DEFAULTS.project, PROJECT) | ||
| 303 | - self.assertEqual(_called_project, [None]) | ||
| 304 | - | ||
| 305 | - | ||
| 306 | - class Test_lazy_loading(unittest2.TestCase): | ||
| 307 | - | ||
| 308 | - def setUp(self): | ||
| 309 | - from gcloud._testing import _setup_defaults | ||
| 310 | - _setup_defaults(self, implicit=True) | ||
| 311 | - | ||
| 312 | - def tearDown(self): | ||
| 313 | - from gcloud._testing import _tear_down_defaults | ||
| 314 | - _tear_down_defaults(self) | ||
| 315 | - | ||
| 316 | - def test_descriptor_for_project(self): | ||
| 317 | - from gcloud._testing import _Monkey | ||
| 318 | - from gcloud import _helpers | ||
| 319 | - | ||
| 320 | - self.assertFalse('project' in _helpers._DEFAULTS.__dict__) | ||
| 321 | - | ||
| 322 | - DEFAULT = object() | ||
| 323 | - | ||
| 324 | - with _Monkey(_helpers, | ||
| 325 | - _determine_default_project=lambda: DEFAULT): | ||
| 326 | - lazy_loaded = _helpers._DEFAULTS.project | ||
| 327 | - | ||
| 328 | - self.assertEqual(lazy_loaded, DEFAULT) | ||
| 329 | - self.assertTrue('project' in _helpers._DEFAULTS.__dict__) | ||
| 330 | - | ||
| 331 | - | ||
| 332 | 205 | class _AppIdentity(object): | |
| 333 | 206 | ||
| 334 | 207 | def __init__(self, app_id): | |
| Back | FazBrowse Home | New Git URL |
0 commit comments