forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest___init__.py
More file actions
306 lines (228 loc) · 10.3 KB
/
Copy pathtest___init__.py
File metadata and controls
306 lines (228 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# Copyright 2014 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest2
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
def tearDown(self):
from gcloud.datastore import _implicit_environ
_implicit_environ.DATASET_ID = self._replaced_dataset_id
def _callFUT(self, dataset_id=None):
from gcloud.datastore import set_default_dataset_id
return set_default_dataset_id(dataset_id=dataset_id)
def _monkey(self, implicit_dataset_id):
import os
from gcloud.datastore import _DATASET_ENV_VAR_NAME
from gcloud._testing import _Monkey
environ = {_DATASET_ENV_VAR_NAME: implicit_dataset_id}
return _Monkey(os, getenv=environ.get)
def test_no_env_var_set(self):
from gcloud.datastore import _implicit_environ
with self._monkey(None):
self._callFUT()
self.assertEqual(_implicit_environ.DATASET_ID, None)
def test_set_from_env_var(self):
from gcloud.datastore import _implicit_environ
IMPLICIT_DATASET_ID = 'IMPLICIT'
with self._monkey(IMPLICIT_DATASET_ID):
self._callFUT()
self.assertEqual(_implicit_environ.DATASET_ID, IMPLICIT_DATASET_ID)
def test_set_explicit_w_env_var_set(self):
from gcloud.datastore import _implicit_environ
EXPLICIT_DATASET_ID = 'EXPLICIT'
with self._monkey(None):
self._callFUT(EXPLICIT_DATASET_ID)
self.assertEqual(_implicit_environ.DATASET_ID, EXPLICIT_DATASET_ID)
def test_set_explicit_no_env_var_set(self):
from gcloud.datastore import _implicit_environ
IMPLICIT_DATASET_ID = 'IMPLICIT'
EXPLICIT_DATASET_ID = 'EXPLICIT'
with self._monkey(IMPLICIT_DATASET_ID):
self._callFUT(EXPLICIT_DATASET_ID)
self.assertEqual(_implicit_environ.DATASET_ID, EXPLICIT_DATASET_ID)
def test_set_explicit_None_wo_env_var_set(self):
from gcloud.datastore import _implicit_environ
with self._monkey(None):
self._callFUT(None)
self.assertEqual(_implicit_environ.DATASET_ID, None)
def test_set_explicit_None_w_env_var_set(self):
from gcloud.datastore import _implicit_environ
IMPLICIT_DATASET_ID = 'IMPLICIT'
with self._monkey(IMPLICIT_DATASET_ID):
self._callFUT(None)
self.assertEqual(_implicit_environ.DATASET_ID, IMPLICIT_DATASET_ID)
def test_set_implicit_from_appengine(self):
from gcloud._testing import _Monkey
from gcloud.datastore import _implicit_environ
APP_ENGINE_ID = 'GAE'
APP_IDENTITY = _AppIdentity(APP_ENGINE_ID)
with self._monkey(None):
with _Monkey(_implicit_environ, app_identity=APP_IDENTITY):
self._callFUT()
self.assertEqual(_implicit_environ.DATASET_ID, APP_ENGINE_ID)
def test_set_implicit_both_env_and_appengine(self):
from gcloud._testing import _Monkey
from gcloud.datastore import _implicit_environ
IMPLICIT_DATASET_ID = 'IMPLICIT'
APP_IDENTITY = _AppIdentity('GAE')
with self._monkey(IMPLICIT_DATASET_ID):
with _Monkey(_implicit_environ, app_identity=APP_IDENTITY):
self._callFUT()
self.assertEqual(_implicit_environ.DATASET_ID, IMPLICIT_DATASET_ID)
def _implicit_compute_engine_helper(self, status):
from gcloud._testing import _Monkey
from gcloud.datastore import _implicit_environ
COMPUTE_ENGINE_ID = 'GCE'
HTTPLIB2 = _Httplib2(COMPUTE_ENGINE_ID, status=status)
if status == '200':
EXPECTED_ID = COMPUTE_ENGINE_ID
else:
EXPECTED_ID = None
with self._monkey(None):
with _Monkey(_implicit_environ, httplib2=HTTPLIB2):
self._callFUT()
self.assertEqual(_implicit_environ.DATASET_ID, EXPECTED_ID)
self.assertEqual(len(HTTPLIB2._http_instances), 1)
(timeout, http_instance), = HTTPLIB2._http_instances
self.assertEqual(timeout, 0.1)
self.assertEqual(
http_instance._called_uris,
['http://169.254.169.254/computeMetadata/v1/project/project-id'])
expected_kwargs = {
'method': 'GET',
'headers': {
'Metadata-Flavor': 'Google',
},
}
self.assertEqual(http_instance._called_kwargs, [expected_kwargs])
def test_set_implicit_from_compute_engine(self):
self._implicit_compute_engine_helper('200')
def test_set_implicit_from_compute_engine_bad_status(self):
self._implicit_compute_engine_helper('404')
def test_set_implicit_from_compute_engine_raise_timeout(self):
self._implicit_compute_engine_helper('RAISE')
def test_set_implicit_both_appengine_and_compute(self):
from gcloud._testing import _Monkey
from gcloud.datastore import _implicit_environ
APP_ENGINE_ID = 'GAE'
APP_IDENTITY = _AppIdentity(APP_ENGINE_ID)
HTTPLIB2 = _Httplib2('GCE')
with self._monkey(None):
with _Monkey(_implicit_environ, app_identity=APP_IDENTITY,
httplib2=HTTPLIB2):
self._callFUT()
self.assertEqual(_implicit_environ.DATASET_ID, APP_ENGINE_ID)
self.assertEqual(len(HTTPLIB2._http_instances), 0)
def test_set_implicit_three_env_appengine_and_compute(self):
from gcloud._testing import _Monkey
from gcloud.datastore import _implicit_environ
IMPLICIT_DATASET_ID = 'IMPLICIT'
APP_IDENTITY = _AppIdentity('GAE')
HTTPLIB2 = _Httplib2('GCE')
with self._monkey(IMPLICIT_DATASET_ID):
with _Monkey(_implicit_environ, app_identity=APP_IDENTITY,
httplib2=HTTPLIB2):
self._callFUT()
self.assertEqual(_implicit_environ.DATASET_ID, IMPLICIT_DATASET_ID)
self.assertEqual(len(HTTPLIB2._http_instances), 0)
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
def tearDown(self):
from gcloud.datastore import _implicit_environ
_implicit_environ.CONNECTION = self._replaced_connection
def _callFUT(self, connection=None):
from gcloud.datastore import set_default_connection
return set_default_connection(connection=connection)
def test_set_explicit(self):
from gcloud.datastore import _implicit_environ
self.assertEqual(_implicit_environ.CONNECTION, None)
fake_cnxn = object()
self._callFUT(connection=fake_cnxn)
self.assertEqual(_implicit_environ.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)
fake_cnxn = object()
with _Monkey(datastore, get_connection=lambda: fake_cnxn):
self._callFUT()
self.assertEqual(_implicit_environ.CONNECTION, fake_cnxn)
class Test_set_defaults(unittest2.TestCase):
def _callFUT(self, dataset_id=None, connection=None):
from gcloud.datastore import set_defaults
return set_defaults(dataset_id=dataset_id, connection=connection)
def test_it(self):
from gcloud._testing import _Monkey
from gcloud import datastore
DATASET_ID = object()
CONNECTION = object()
SET_DATASET_CALLED = []
def call_set_dataset(dataset_id=None):
SET_DATASET_CALLED.append(dataset_id)
SET_CONNECTION_CALLED = []
def call_set_connection(connection=None):
SET_CONNECTION_CALLED.append(connection)
with _Monkey(datastore, set_default_dataset_id=call_set_dataset,
set_default_connection=call_set_connection):
self._callFUT(dataset_id=DATASET_ID, connection=CONNECTION)
self.assertEqual(SET_DATASET_CALLED, [DATASET_ID])
self.assertEqual(SET_CONNECTION_CALLED, [CONNECTION])
class Test_get_connection(unittest2.TestCase):
def _callFUT(self):
from gcloud.datastore import get_connection
return get_connection()
def test_it(self):
from gcloud import credentials
from gcloud.datastore.connection import Connection
from gcloud.test_credentials import _Client
from gcloud._testing import _Monkey
client = _Client()
with _Monkey(credentials, client=client):
found = self._callFUT()
self.assertTrue(isinstance(found, Connection))
self.assertTrue(found._credentials is client._signed)
self.assertTrue(client._get_app_default_called)
class _AppIdentity(object):
def __init__(self, app_id):
self.app_id = app_id
def get_application_id(self):
return self.app_id
class _Http(object):
def __init__(self, parent):
self.parent = parent
self._called_uris = []
self._called_kwargs = []
def request(self, uri, **kwargs):
import socket
self._called_uris.append(uri)
self._called_kwargs.append(kwargs)
if self.parent.status == 'RAISE':
raise socket.timeout('timed out')
else:
return {'status': self.parent.status}, self.parent.project_id
class _Httplib2(object):
def __init__(self, project_id, status='200'):
self.project_id = project_id
self.status = status
self._http_instances = []
def Http(self, timeout=None):
result = _Http(self)
self._http_instances.append((timeout, result))
return result