@@ -33,6 +33,50 @@ def test_it(self):
3333 self .assertEqual (client ._called_with , expected_called_with )
3434
3535
36+ class Test_get_local_dataset_settings (unittest2 .TestCase ):
37+
38+ def _callFUT (self ):
39+ from gcloud .datastore import get_local_dataset_settings
40+ return get_local_dataset_settings ()
41+
42+ def _test_with_environ (self , environ , expected_result ):
43+ import os
44+ from gcloud ._testing import _Monkey
45+
46+ def custom_getenv (key ):
47+ return environ .get (key )
48+
49+ with _Monkey (os , getenv = custom_getenv ):
50+ result = self ._callFUT ()
51+
52+ self .assertEqual (result , expected_result )
53+
54+ def test_all_set (self ):
55+ # Fake auth variables.
56+ DATASET = 'dataset'
57+ CLIENT_EMAIL = '[email protected] ' 58+ TEMP_PATH = 'fakepath'
59+
60+ # Make a custom getenv function to Monkey.
61+ VALUES = {
62+ 'GCLOUD_DATASET_ID' : DATASET ,
63+ 'GCLOUD_CLIENT_EMAIL' : CLIENT_EMAIL ,
64+ 'GCLOUD_KEY_FILE' : TEMP_PATH ,
65+ }
66+ expected_result = (DATASET , CLIENT_EMAIL , TEMP_PATH )
67+ self ._test_with_environ (VALUES , expected_result )
68+
69+ def test_partial_set (self ):
70+ # Fake auth variables.
71+ DATASET = 'dataset'
72+
73+ # Make a custom getenv function to Monkey.
74+ VALUES = {
75+ 'GCLOUD_DATASET_ID' : DATASET ,
76+ }
77+ self ._test_with_environ (VALUES , None )
78+
79+
3680class Test_get_dataset (unittest2 .TestCase ):
3781
3882 def _callFUT (self , dataset_id , client_email , private_key_path ):
@@ -66,3 +110,160 @@ def test_it(self):
66110 'scope' : SCOPE ,
67111 }
68112 self .assertEqual (client ._called_with , expected_called_with )
113+
114+
115+ class Test_implicit_behavior (unittest2 .TestCase ):
116+
117+ def test__require_dataset (self ):
118+ import gcloud .datastore
119+ original_dataset = gcloud .datastore .DATASET
120+
121+ try :
122+ gcloud .datastore .DATASET = None
123+ self .assertRaises (EnvironmentError ,
124+ gcloud .datastore ._require_dataset )
125+ gcloud .datastore .DATASET = object ()
126+ self .assertEqual (gcloud .datastore ._require_dataset (), None )
127+ finally :
128+ gcloud .datastore .DATASET = original_dataset
129+
130+ def test_get_entity (self ):
131+ import gcloud .datastore
132+ from gcloud .datastore .test_entity import _Dataset
133+ from gcloud ._testing import _Monkey
134+
135+ CUSTOM_DATASET = _Dataset ()
136+ DUMMY_KEY = object ()
137+ DUMMY_VAL = object ()
138+ CUSTOM_DATASET [DUMMY_KEY ] = DUMMY_VAL
139+ with _Monkey (gcloud .datastore , DATASET = CUSTOM_DATASET ):
140+ result = gcloud .datastore .get_entity (DUMMY_KEY )
141+ self .assertTrue (result is DUMMY_VAL )
142+
143+ def test_get_entities (self ):
144+ import gcloud .datastore
145+ from gcloud .datastore .test_entity import _Dataset
146+ from gcloud ._testing import _Monkey
147+
148+ class _ExtendedDataset (_Dataset ):
149+ def get_entities (self , keys ):
150+ return [self .get (key ) for key in keys ]
151+
152+ CUSTOM_DATASET = _ExtendedDataset ()
153+ DUMMY_KEYS = [object (), object ()]
154+ DUMMY_VALS = [object (), object ()]
155+ for key , val in zip (DUMMY_KEYS , DUMMY_VALS ):
156+ CUSTOM_DATASET [key ] = val
157+
158+ with _Monkey (gcloud .datastore , DATASET = CUSTOM_DATASET ):
159+ result = gcloud .datastore .get_entities (DUMMY_KEYS )
160+ self .assertTrue (result == DUMMY_VALS )
161+
162+ def test_allocate_ids (self ):
163+ import gcloud .datastore
164+ from gcloud .datastore .test_entity import _Connection
165+ from gcloud .datastore .test_entity import _DATASET_ID
166+ from gcloud .datastore .test_entity import _Dataset
167+ from gcloud .datastore .test_entity import _Key
168+ from gcloud ._testing import _Monkey
169+
170+ class _PathElementProto (object ):
171+ COUNTER = 0
172+
173+ def __init__ (self ):
174+ _PathElementProto .COUNTER += 1
175+ self .id = _PathElementProto .COUNTER
176+
177+ class _KeyProto (object ):
178+
179+ def __init__ (self ):
180+ self .path_element = [_PathElementProto ()]
181+
182+ class _ExtendedKey (_Key ):
183+ def id (self , id_to_set ):
184+ self ._called_id = id_to_set
185+ return id_to_set
186+
187+ INCOMPLETE_KEY = _ExtendedKey ()
188+ INCOMPLETE_KEY ._key = _KeyProto ()
189+ INCOMPLETE_KEY ._partial = True
190+ NUM_IDS = 2
191+
192+ class _ExtendedConnection (_Connection ):
193+ def allocate_ids (self , dataset_id , key_pbs ):
194+ self ._called_dataset_id = dataset_id
195+ self ._called_key_pbs = key_pbs
196+ return key_pbs
197+
198+ CUSTOM_CONNECTION = _ExtendedConnection ()
199+ CUSTOM_DATASET = _Dataset (connection = CUSTOM_CONNECTION )
200+ with _Monkey (gcloud .datastore , DATASET = CUSTOM_DATASET ):
201+ result = gcloud .datastore .allocate_ids (INCOMPLETE_KEY , NUM_IDS )
202+
203+ self .assertEqual (_PathElementProto .COUNTER , 1 )
204+ self .assertEqual (result , [1 , 1 ])
205+ self .assertEqual (CUSTOM_CONNECTION ._called_dataset_id , _DATASET_ID )
206+ self .assertEqual (len (CUSTOM_CONNECTION ._called_key_pbs ), 2 )
207+ key_paths = [key_pb .path_element [- 1 ].id
208+ for key_pb in CUSTOM_CONNECTION ._called_key_pbs ]
209+ self .assertEqual (key_paths , [1 , 1 ])
210+
211+ def test_allocate_ids_with_complete (self ):
212+ import gcloud .datastore
213+ from gcloud .datastore .test_entity import _Connection
214+ from gcloud .datastore .test_entity import _Dataset
215+ from gcloud .datastore .test_entity import _Key
216+ from gcloud ._testing import _Monkey
217+
218+ COMPLETE_KEY = _Key ()
219+ NUM_IDS = 2
220+ CUSTOM_CONNECTION = _Connection ()
221+ CUSTOM_DATASET = _Dataset (connection = CUSTOM_CONNECTION )
222+ with _Monkey (gcloud .datastore , DATASET = CUSTOM_DATASET ):
223+ self .assertRaises (ValueError , gcloud .datastore .allocate_ids ,
224+ COMPLETE_KEY , NUM_IDS )
225+
226+ def test_set_DATASET (self ):
227+ import os
228+ import tempfile
229+ from gcloud import credentials
230+ from gcloud .test_credentials import _Client
231+ from gcloud ._testing import _Monkey
232+
233+ # Make custom client for doing auth.
234+ client = _Client ()
235+
236+ # Fake auth variables.
237+ CLIENT_EMAIL = '[email protected] ' 238+ PRIVATE_KEY = 'SEEkR1t'
239+ DATASET = 'dataset'
240+
241+ # Write the fake key to a temp file.
242+ TEMP_PATH = tempfile .mktemp ()
243+ with open (TEMP_PATH , 'w' ) as file_obj :
244+ file_obj .write (PRIVATE_KEY )
245+ file_obj .flush ()
246+
247+ # Make a custom getenv function to Monkey.
248+ VALUES = {
249+ 'GCLOUD_DATASET_ID' : DATASET ,
250+ 'GCLOUD_CLIENT_EMAIL' : CLIENT_EMAIL ,
251+ 'GCLOUD_KEY_FILE' : TEMP_PATH ,
252+ }
253+
254+ def custom_getenv (key ):
255+ return VALUES .get (key )
256+
257+ # Perform the import again with our test patches.
258+ with _Monkey (credentials , client = client ):
259+ with _Monkey (os , getenv = custom_getenv ):
260+ import gcloud .datastore
261+ reload (gcloud .datastore )
262+
263+ # Check that the DATASET was correctly implied from the environ.
264+ implicit_dataset = gcloud .datastore .DATASET
265+ self .assertEqual (implicit_dataset .id (), DATASET )
266+ # Check that the credentials on the implicit DATASET was set on the
267+ # fake client.
268+ credentials = implicit_dataset .connection ().credentials
269+ self .assertTrue (credentials is client ._signed )
0 commit comments