1515import unittest2
1616
1717
18+ def _make_entity_pb (dataset_id , kind , integer_id , name = None , str_val = None ):
19+ from gcloud .datastore import _datastore_v1_pb2 as datastore_pb
20+
21+ entity_pb = datastore_pb .Entity ()
22+ entity_pb .key .partition_id .dataset_id = dataset_id
23+ path_element = entity_pb .key .path_element .add ()
24+ path_element .kind = kind
25+ path_element .id = integer_id
26+ if name is not None and str_val is not None :
27+ prop = entity_pb .property .add ()
28+ prop .name = name
29+ prop .value .string_value = str_val
30+
31+ return entity_pb
32+
33+
1834class Test__require_dataset_id (unittest2 .TestCase ):
1935
2036 _MARKER = object ()
@@ -158,7 +174,7 @@ def test_implicit_set_passed_explicitly(self):
158174 self .assertTrue (self ._callFUT (CONNECTION ) is CONNECTION )
159175
160176
161- class Test_get_function (unittest2 .TestCase ):
177+ class Test_get_multi_function (unittest2 .TestCase ):
162178
163179 def setUp (self ):
164180 from gcloud .datastore ._testing import _setup_defaults
@@ -170,25 +186,9 @@ def tearDown(self):
170186
171187 def _callFUT (self , keys , missing = None , deferred = None ,
172188 connection = None , dataset_id = None ):
173- from gcloud .datastore .api import get
174- return get (keys , missing = missing , deferred = deferred ,
175- connection = connection , dataset_id = dataset_id )
176-
177- def _make_entity_pb (self , dataset_id , kind , integer_id ,
178- name = None , str_val = None ):
179- from gcloud .datastore import _datastore_v1_pb2 as datastore_pb
180-
181- entity_pb = datastore_pb .Entity ()
182- entity_pb .key .partition_id .dataset_id = dataset_id
183- path_element = entity_pb .key .path_element .add ()
184- path_element .kind = kind
185- path_element .id = integer_id
186- if name is not None and str_val is not None :
187- prop = entity_pb .property .add ()
188- prop .name = name
189- prop .value .string_value = str_val
190-
191- return entity_pb
189+ from gcloud .datastore .api import get_multi
190+ return get_multi (keys , missing = missing , deferred = deferred ,
191+ connection = connection , dataset_id = dataset_id )
192192
193193 def test_wo_connection (self ):
194194 from gcloud .datastore .key import Key
@@ -398,8 +398,7 @@ def test_hit(self):
398398 PATH = [{'kind' : KIND , 'id' : ID }]
399399
400400 # Make a found entity pb to be returned from mock backend.
401- entity_pb = self ._make_entity_pb (DATASET_ID , KIND , ID ,
402- 'foo' , 'Foo' )
401+ entity_pb = _make_entity_pb (DATASET_ID , KIND , ID , 'foo' , 'Foo' )
403402
404403 # Make a connection to return the entity pb.
405404 connection = _Connection (entity_pb )
@@ -426,8 +425,8 @@ def test_hit_multiple_keys_same_dataset(self):
426425 ID2 = 2345
427426
428427 # Make a found entity pb to be returned from mock backend.
429- entity_pb1 = self . _make_entity_pb (DATASET_ID , KIND , ID1 )
430- entity_pb2 = self . _make_entity_pb (DATASET_ID , KIND , ID2 )
428+ entity_pb1 = _make_entity_pb (DATASET_ID , KIND , ID1 )
429+ entity_pb2 = _make_entity_pb (DATASET_ID , KIND , ID2 )
431430
432431 # Make a connection to return the entity pbs.
433432 connection = _Connection (entity_pb1 , entity_pb2 )
@@ -469,8 +468,7 @@ def test_implicit_wo_transaction(self):
469468 PATH = [{'kind' : KIND , 'id' : ID }]
470469
471470 # Make a found entity pb to be returned from mock backend.
472- entity_pb = self ._make_entity_pb (DATASET_ID , KIND , ID ,
473- 'foo' , 'Foo' )
471+ entity_pb = _make_entity_pb (DATASET_ID , KIND , ID , 'foo' , 'Foo' )
474472
475473 # Make a connection to return the entity pb.
476474 CUSTOM_CONNECTION = _Connection (entity_pb )
@@ -507,8 +505,7 @@ def test_w_transaction(self):
507505 TRANSACTION = 'TRANSACTION'
508506
509507 # Make a found entity pb to be returned from mock backend.
510- entity_pb = self ._make_entity_pb (DATASET_ID , KIND , ID ,
511- 'foo' , 'Foo' )
508+ entity_pb = _make_entity_pb (DATASET_ID , KIND , ID , 'foo' , 'Foo' )
512509
513510 # Make a connection to return the entity pb.
514511 CUSTOM_CONNECTION = _Connection (entity_pb )
@@ -545,8 +542,7 @@ def test_max_loops(self):
545542 ID = 1234
546543
547544 # Make a found entity pb to be returned from mock backend.
548- entity_pb = self ._make_entity_pb (DATASET_ID , KIND , ID ,
549- 'foo' , 'Foo' )
545+ entity_pb = _make_entity_pb (DATASET_ID , KIND , ID , 'foo' , 'Foo' )
550546
551547 # Make a connection to return the entity pb.
552548 connection = _Connection (entity_pb )
@@ -566,6 +562,61 @@ def test_max_loops(self):
566562 self .assertEqual (deferred , [])
567563
568564
565+ class Test_get_function (unittest2 .TestCase ):
566+
567+ def setUp (self ):
568+ from gcloud .datastore ._testing import _setup_defaults
569+ _setup_defaults (self )
570+
571+ def tearDown (self ):
572+ from gcloud .datastore ._testing import _tear_down_defaults
573+ _tear_down_defaults (self )
574+
575+ def _callFUT (self , key , missing = None , deferred = None ,
576+ connection = None , dataset_id = None ):
577+ from gcloud .datastore .api import get
578+ return get (key , missing = missing , deferred = deferred ,
579+ connection = connection , dataset_id = dataset_id )
580+
581+ def test_hit (self ):
582+ from gcloud .datastore .key import Key
583+ from gcloud .datastore .test_connection import _Connection
584+
585+ DATASET_ID = 'DATASET'
586+ KIND = 'Kind'
587+ ID = 1234
588+ PATH = [{'kind' : KIND , 'id' : ID }]
589+
590+ # Make a found entity pb to be returned from mock backend.
591+ entity_pb = _make_entity_pb (DATASET_ID , KIND , ID , 'foo' , 'Foo' )
592+
593+ # Make a connection to return the entity pb.
594+ connection = _Connection (entity_pb )
595+
596+ key = Key (KIND , ID , dataset_id = DATASET_ID )
597+ result = self ._callFUT (key , connection = connection ,
598+ dataset_id = DATASET_ID )
599+ new_key = result .key
600+
601+ # Check the returned value is as expected.
602+ self .assertFalse (new_key is key )
603+ self .assertEqual (new_key .dataset_id , DATASET_ID )
604+ self .assertEqual (new_key .path , PATH )
605+ self .assertEqual (list (result ), ['foo' ])
606+ self .assertEqual (result ['foo' ], 'Foo' )
607+
608+ def test_miss (self ):
609+ from gcloud .datastore .key import Key
610+ from gcloud .datastore .test_connection import _Connection
611+
612+ DATASET_ID = 'DATASET'
613+ connection = _Connection ()
614+ key = Key ('Kind' , 1234 , dataset_id = DATASET_ID )
615+ result = self ._callFUT (key , connection = connection ,
616+ dataset_id = DATASET_ID )
617+ self .assertTrue (result is None )
618+
619+
569620class Test_put_function (unittest2 .TestCase ):
570621
571622 def setUp (self ):
0 commit comments