|
| 1 | +# Copyright 2014 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest2 |
| 16 | + |
| 17 | + |
| 18 | +class Test__get_production_project(unittest2.TestCase): |
| 19 | + |
| 20 | + def _callFUT(self): |
| 21 | + from gcloud.storage import _implicit_environ |
| 22 | + return _implicit_environ._get_production_project() |
| 23 | + |
| 24 | + def test_no_value(self): |
| 25 | + import os |
| 26 | + from gcloud._testing import _Monkey |
| 27 | + |
| 28 | + environ = {} |
| 29 | + with _Monkey(os, getenv=environ.get): |
| 30 | + project = self._callFUT() |
| 31 | + self.assertEqual(project, None) |
| 32 | + |
| 33 | + def test_value_set(self): |
| 34 | + import os |
| 35 | + from gcloud._testing import _Monkey |
| 36 | + from gcloud.storage._implicit_environ import _PROJECT_ENV_VAR_NAME |
| 37 | + |
| 38 | + MOCK_PROJECT = object() |
| 39 | + environ = {_PROJECT_ENV_VAR_NAME: MOCK_PROJECT} |
| 40 | + with _Monkey(os, getenv=environ.get): |
| 41 | + project = self._callFUT() |
| 42 | + self.assertEqual(project, MOCK_PROJECT) |
| 43 | + |
| 44 | + |
| 45 | +class Test__determine_default_project(unittest2.TestCase): |
| 46 | + |
| 47 | + def _callFUT(self, project=None): |
| 48 | + from gcloud.storage._implicit_environ import _determine_default_project |
| 49 | + return _determine_default_project(project=project) |
| 50 | + |
| 51 | + def _determine_default_helper(self, prod=None, project=None): |
| 52 | + from gcloud._testing import _Monkey |
| 53 | + from gcloud.storage import _implicit_environ |
| 54 | + |
| 55 | + _callers = [] |
| 56 | + |
| 57 | + def prod_mock(): |
| 58 | + _callers.append('prod_mock') |
| 59 | + return prod |
| 60 | + |
| 61 | + patched_methods = { |
| 62 | + '_get_production_project': prod_mock, |
| 63 | + } |
| 64 | + |
| 65 | + with _Monkey(_implicit_environ, **patched_methods): |
| 66 | + returned_project = self._callFUT(project) |
| 67 | + |
| 68 | + return returned_project, _callers |
| 69 | + |
| 70 | + def test_no_value(self): |
| 71 | + project, callers = self._determine_default_helper() |
| 72 | + self.assertEqual(project, None) |
| 73 | + self.assertEqual(callers, ['prod_mock']) |
| 74 | + |
| 75 | + def test_explicit(self): |
| 76 | + PROJECT = object() |
| 77 | + project, callers = self._determine_default_helper(project=PROJECT) |
| 78 | + self.assertEqual(project, PROJECT) |
| 79 | + self.assertEqual(callers, []) |
| 80 | + |
| 81 | + def test_prod(self): |
| 82 | + PROJECT = object() |
| 83 | + project, callers = self._determine_default_helper(prod=PROJECT) |
| 84 | + self.assertEqual(project, PROJECT) |
| 85 | + self.assertEqual(callers, ['prod_mock']) |
| 86 | + |
| 87 | + |
| 88 | +class Test_set_default_project(unittest2.TestCase): |
| 89 | + |
| 90 | + def setUp(self): |
| 91 | + from gcloud.storage._testing import _setup_defaults |
| 92 | + _setup_defaults(self) |
| 93 | + |
| 94 | + def tearDown(self): |
| 95 | + from gcloud.storage._testing import _tear_down_defaults |
| 96 | + _tear_down_defaults(self) |
| 97 | + |
| 98 | + def _callFUT(self, project=None): |
| 99 | + from gcloud.storage._implicit_environ import set_default_project |
| 100 | + return set_default_project(project=project) |
| 101 | + |
| 102 | + def test_raises(self): |
| 103 | + from gcloud._testing import _Monkey |
| 104 | + from gcloud.storage import _implicit_environ |
| 105 | + |
| 106 | + _called_project = [] |
| 107 | + |
| 108 | + def mock_determine(project): |
| 109 | + _called_project.append(project) |
| 110 | + return None |
| 111 | + |
| 112 | + with _Monkey(_implicit_environ, |
| 113 | + _determine_default_project=mock_determine): |
| 114 | + self.assertRaises(EnvironmentError, self._callFUT) |
| 115 | + |
| 116 | + self.assertEqual(_called_project, [None]) |
| 117 | + |
| 118 | + def test_set_correctly(self): |
| 119 | + from gcloud._testing import _Monkey |
| 120 | + from gcloud.storage import _implicit_environ |
| 121 | + |
| 122 | + self.assertEqual(_implicit_environ._DEFAULTS.project, None) |
| 123 | + |
| 124 | + PROJECT = object() |
| 125 | + _called_project = [] |
| 126 | + |
| 127 | + def mock_determine(project): |
| 128 | + _called_project.append(project) |
| 129 | + return PROJECT |
| 130 | + |
| 131 | + with _Monkey(_implicit_environ, |
| 132 | + _determine_default_project=mock_determine): |
| 133 | + self._callFUT() |
| 134 | + |
| 135 | + self.assertEqual(_implicit_environ._DEFAULTS.project, PROJECT) |
| 136 | + self.assertEqual(_called_project, [None]) |
| 137 | + |
| 138 | + |
| 139 | +class Test_lazy_loading(unittest2.TestCase): |
| 140 | + |
| 141 | + def setUp(self): |
| 142 | + from gcloud.storage._testing import _setup_defaults |
| 143 | + _setup_defaults(self, implicit=True) |
| 144 | + |
| 145 | + def tearDown(self): |
| 146 | + from gcloud.storage._testing import _tear_down_defaults |
| 147 | + _tear_down_defaults(self) |
| 148 | + |
| 149 | + def test_descriptor_for_project(self): |
| 150 | + from gcloud._testing import _Monkey |
| 151 | + from gcloud.storage import _implicit_environ |
| 152 | + |
| 153 | + self.assertFalse('project' in _implicit_environ._DEFAULTS.__dict__) |
| 154 | + |
| 155 | + DEFAULT = object() |
| 156 | + |
| 157 | + with _Monkey(_implicit_environ, |
| 158 | + _determine_default_project=lambda: DEFAULT): |
| 159 | + lazy_loaded = _implicit_environ._DEFAULTS.project |
| 160 | + |
| 161 | + self.assertEqual(lazy_loaded, DEFAULT) |
| 162 | + self.assertTrue('project' in _implicit_environ._DEFAULTS.__dict__) |
0 commit comments