@@ -45,22 +45,22 @@ def test_no_env_var_set(self):
4545
4646 def test_set_from_env_var (self ):
4747 from gcloud .datastore import _implicit_environ
48- IMPLICIT_DATASET_ID = 'IMPLICIT'
48+ IMPLICIT_DATASET_ID = 's~ IMPLICIT'
4949 with self ._monkey (IMPLICIT_DATASET_ID ):
5050 self ._callFUT ()
5151 self .assertEqual (_implicit_environ .DATASET_ID , IMPLICIT_DATASET_ID )
5252
5353 def test_set_explicit_w_env_var_set (self ):
5454 from gcloud .datastore import _implicit_environ
55- EXPLICIT_DATASET_ID = 'EXPLICIT'
55+ EXPLICIT_DATASET_ID = 's~ EXPLICIT'
5656 with self ._monkey (None ):
5757 self ._callFUT (EXPLICIT_DATASET_ID )
5858 self .assertEqual (_implicit_environ .DATASET_ID , EXPLICIT_DATASET_ID )
5959
6060 def test_set_explicit_no_env_var_set (self ):
6161 from gcloud .datastore import _implicit_environ
62- IMPLICIT_DATASET_ID = 'IMPLICIT'
63- EXPLICIT_DATASET_ID = 'EXPLICIT'
62+ IMPLICIT_DATASET_ID = 's~ IMPLICIT'
63+ EXPLICIT_DATASET_ID = 's~ EXPLICIT'
6464 with self ._monkey (IMPLICIT_DATASET_ID ):
6565 self ._callFUT (EXPLICIT_DATASET_ID )
6666 self .assertEqual (_implicit_environ .DATASET_ID , EXPLICIT_DATASET_ID )
@@ -73,7 +73,7 @@ def test_set_explicit_None_wo_env_var_set(self):
7373
7474 def test_set_explicit_None_w_env_var_set (self ):
7575 from gcloud .datastore import _implicit_environ
76- IMPLICIT_DATASET_ID = 'IMPLICIT'
76+ IMPLICIT_DATASET_ID = 's~ IMPLICIT'
7777 with self ._monkey (IMPLICIT_DATASET_ID ):
7878 self ._callFUT (None )
7979 self .assertEqual (_implicit_environ .DATASET_ID , IMPLICIT_DATASET_ID )
@@ -82,7 +82,7 @@ def test_set_implicit_from_appengine(self):
8282 from gcloud ._testing import _Monkey
8383 from gcloud .datastore import _implicit_environ
8484
85- APP_ENGINE_ID = 'GAE'
85+ APP_ENGINE_ID = 's~ GAE'
8686 APP_IDENTITY = _AppIdentity (APP_ENGINE_ID )
8787
8888 with self ._monkey (None ):
@@ -95,8 +95,8 @@ def test_set_implicit_both_env_and_appengine(self):
9595 from gcloud ._testing import _Monkey
9696 from gcloud .datastore import _implicit_environ
9797
98- IMPLICIT_DATASET_ID = 'IMPLICIT'
99- APP_IDENTITY = _AppIdentity ('GAE' )
98+ IMPLICIT_DATASET_ID = 's~ IMPLICIT'
99+ APP_IDENTITY = _AppIdentity ('s~ GAE' )
100100
101101 with self ._monkey (IMPLICIT_DATASET_ID ):
102102 with _Monkey (_implicit_environ , app_identity = APP_IDENTITY ):
@@ -108,7 +108,7 @@ def _implicit_compute_engine_helper(self, status):
108108 from gcloud ._testing import _Monkey
109109 from gcloud .datastore import _implicit_environ
110110
111- COMPUTE_ENGINE_ID = 'GCE'
111+ COMPUTE_ENGINE_ID = 's~ GCE'
112112 HTTPLIB2 = _Httplib2 (COMPUTE_ENGINE_ID , status = status )
113113 if status == '200' :
114114 EXPECTED_ID = COMPUTE_ENGINE_ID
@@ -148,9 +148,9 @@ def test_set_implicit_both_appengine_and_compute(self):
148148 from gcloud ._testing import _Monkey
149149 from gcloud .datastore import _implicit_environ
150150
151- APP_ENGINE_ID = 'GAE'
151+ APP_ENGINE_ID = 's~ GAE'
152152 APP_IDENTITY = _AppIdentity (APP_ENGINE_ID )
153- HTTPLIB2 = _Httplib2 ('GCE' )
153+ HTTPLIB2 = _Httplib2 ('s~ GCE' )
154154
155155 with self ._monkey (None ):
156156 with _Monkey (_implicit_environ , app_identity = APP_IDENTITY ,
@@ -164,9 +164,9 @@ def test_set_implicit_three_env_appengine_and_compute(self):
164164 from gcloud ._testing import _Monkey
165165 from gcloud .datastore import _implicit_environ
166166
167- IMPLICIT_DATASET_ID = 'IMPLICIT'
168- APP_IDENTITY = _AppIdentity ('GAE' )
169- HTTPLIB2 = _Httplib2 ('GCE' )
167+ IMPLICIT_DATASET_ID = 's~ IMPLICIT'
168+ APP_IDENTITY = _AppIdentity ('s~ GAE' )
169+ HTTPLIB2 = _Httplib2 ('s~ GCE' )
170170
171171 with self ._monkey (IMPLICIT_DATASET_ID ):
172172 with _Monkey (_implicit_environ , app_identity = APP_IDENTITY ,
@@ -177,6 +177,71 @@ def test_set_implicit_three_env_appengine_and_compute(self):
177177 self .assertEqual (len (HTTPLIB2 ._http_instances ), 0 )
178178
179179
180+ class Test__find_true_dataset_id (unittest2 .TestCase ):
181+
182+ def _callFUT (self , dataset_id , connection = None ):
183+ from gcloud .datastore import _find_true_dataset_id
184+ return _find_true_dataset_id (dataset_id , connection = connection )
185+
186+ def test_prefixed (self ):
187+ PREFIXED = 's~DATASET'
188+ result = self ._callFUT (PREFIXED )
189+ self .assertEqual (PREFIXED , result )
190+
191+ def test_unprefixed_no_connection (self ):
192+ from gcloud .datastore import _implicit_environ
193+
194+ UNPREFIXED = 'DATASET'
195+ self .assertEqual (_implicit_environ .CONNECTION , None )
196+ with self .assertRaises (AttributeError ):
197+ self ._callFUT (UNPREFIXED )
198+
199+ def test_unprefixed_bogus_key_miss (self ):
200+ UNPREFIXED = 'DATASET'
201+ PREFIX = 's~'
202+ CONNECTION = _Connection (PREFIX , from_missing = False )
203+ result = self ._callFUT (UNPREFIXED , connection = CONNECTION )
204+
205+ self .assertEqual (CONNECTION ._called_dataset_id , UNPREFIXED )
206+
207+ self .assertEqual (len (CONNECTION ._lookup_result ), 1 )
208+ self .assertEqual (CONNECTION ._called_missing , [])
209+
210+ # Make sure just one.
211+ called_key_pb , = CONNECTION ._called_key_pbs
212+ path_element = called_key_pb .path_element
213+ self .assertEqual (len (path_element ), 1 )
214+ self .assertEqual (path_element [0 ].kind , '__MissingLookupKind' )
215+ self .assertEqual (path_element [0 ].id , 1 )
216+ self .assertFalse (path_element [0 ].HasField ('name' ))
217+
218+ PREFIXED = PREFIX + UNPREFIXED
219+ self .assertEqual (result , PREFIXED )
220+
221+ def test_unprefixed_bogus_key_hit (self ):
222+ UNPREFIXED = 'DATASET'
223+ PREFIX = 'e~'
224+ CONNECTION = _Connection (PREFIX , from_missing = True )
225+ result = self ._callFUT (UNPREFIXED , connection = CONNECTION )
226+
227+ self .assertEqual (CONNECTION ._called_dataset_id , UNPREFIXED )
228+
229+ self .assertEqual (CONNECTION ._lookup_result , [])
230+ # Though missing=[] was called, it is copied in place.
231+ self .assertEqual (len (CONNECTION ._called_missing ), 1 )
232+
233+ # Make sure just one.
234+ called_key_pb , = CONNECTION ._called_key_pbs
235+ path_element = called_key_pb .path_element
236+ self .assertEqual (len (path_element ), 1 )
237+ self .assertEqual (path_element [0 ].kind , '__MissingLookupKind' )
238+ self .assertEqual (path_element [0 ].id , 1 )
239+ self .assertFalse (path_element [0 ].HasField ('name' ))
240+
241+ PREFIXED = PREFIX + UNPREFIXED
242+ self .assertEqual (result , PREFIXED )
243+
244+
180245class Test_set_default_connection (unittest2 .TestCase ):
181246
182247 def setUp (self ):
@@ -304,3 +369,32 @@ def Http(self, timeout=None):
304369 result = _Http (self )
305370 self ._http_instances .append ((timeout , result ))
306371 return result
372+
373+
374+ class _Connection (object ):
375+
376+ def __init__ (self , prefix , from_missing = False ):
377+ self .prefix = prefix
378+ self .from_missing = from_missing
379+
380+ def lookup (self , dataset_id , key_pbs , missing = None ):
381+ from gcloud .datastore import _datastore_v1_pb2 as datastore_pb
382+
383+ # Store the arguments called with.
384+ self ._called_dataset_id = dataset_id
385+ self ._called_key_pbs = key_pbs
386+ self ._called_missing = missing
387+
388+ key_pb , = key_pbs
389+
390+ response = datastore_pb .Entity ()
391+ response .key .CopyFrom (key_pb )
392+ response .key .partition_id .dataset_id = self .prefix + dataset_id
393+
394+ if self .from_missing :
395+ missing [:] = [response ]
396+ self ._lookup_result = []
397+ else :
398+ self ._lookup_result = [response ]
399+
400+ return self ._lookup_result
0 commit comments