@@ -199,7 +199,7 @@ def _call_fut(self, entity):
199199
200200 return entity_to_protobuf (entity )
201201
202- def _compareEntityProto (self , entity_pb1 , entity_pb2 ):
202+ def _compare_entity_proto (self , entity_pb1 , entity_pb2 ):
203203 from google .cloud .datastore .helpers import _property_tuples
204204
205205 self .assertEqual (entity_pb1 .key , entity_pb2 .key )
@@ -212,8 +212,8 @@ def _compareEntityProto(self, entity_pb1, entity_pb2):
212212 self .assertEqual (name1 , name2 )
213213 if val1 .HasField ('entity_value' ): # Message field (Entity)
214214 self .assertEqual (val1 .meaning , val2 .meaning )
215- self ._compareEntityProto ( val1 . entity_value ,
216- val2 .entity_value )
215+ self ._compare_entity_proto (
216+ val1 . entity_value , val2 .entity_value )
217217 else :
218218 self .assertEqual (val1 , val2 )
219219
@@ -223,7 +223,7 @@ def test_empty(self):
223223
224224 entity = Entity ()
225225 entity_pb = self ._call_fut (entity )
226- self ._compareEntityProto (entity_pb , entity_pb2 .Entity ())
226+ self ._compare_entity_proto (entity_pb , entity_pb2 .Entity ())
227227
228228 def test_key_only (self ):
229229 from google .cloud .proto .datastore .v1 import entity_pb2
@@ -242,7 +242,7 @@ def test_key_only(self):
242242 path_elt .kind = kind
243243 path_elt .name = name
244244
245- self ._compareEntityProto (entity_pb , expected_pb )
245+ self ._compare_entity_proto (entity_pb , expected_pb )
246246
247247 def test_simple_fields (self ):
248248 from google .cloud .proto .datastore .v1 import entity_pb2
@@ -262,7 +262,7 @@ def test_simple_fields(self):
262262 val_pb2 = _new_value_pb (expected_pb , name2 )
263263 val_pb2 .string_value = value2
264264
265- self ._compareEntityProto (entity_pb , expected_pb )
265+ self ._compare_entity_proto (entity_pb , expected_pb )
266266
267267 def test_with_empty_list (self ):
268268 from google .cloud .proto .datastore .v1 import entity_pb2
@@ -272,7 +272,7 @@ def test_with_empty_list(self):
272272 entity ['foo' ] = []
273273 entity_pb = self ._call_fut (entity )
274274
275- self ._compareEntityProto (entity_pb , entity_pb2 .Entity ())
275+ self ._compare_entity_proto (entity_pb , entity_pb2 .Entity ())
276276
277277 def test_inverts_to_protobuf (self ):
278278 from google .cloud .proto .datastore .v1 import entity_pb2
@@ -325,7 +325,7 @@ def test_inverts_to_protobuf(self):
325325
326326 # NOTE: entity_to_protobuf() strips the project so we "cheat".
327327 new_pb .key .partition_id .project_id = project
328- self ._compareEntityProto (original_pb , new_pb )
328+ self ._compare_entity_proto (original_pb , new_pb )
329329
330330 def test_meaning_with_change (self ):
331331 from google .cloud .proto .datastore .v1 import entity_pb2
@@ -343,7 +343,7 @@ def test_meaning_with_change(self):
343343 value_pb .integer_value = value
344344 # NOTE: No meaning is used since the value differs from the
345345 # value stored.
346- self ._compareEntityProto (entity_pb , expected_pb )
346+ self ._compare_entity_proto (entity_pb , expected_pb )
347347
348348 def test_variable_meanings (self ):
349349 from google .cloud .proto .datastore .v1 import entity_pb2
@@ -369,7 +369,78 @@ def test_variable_meanings(self):
369369 value2 = value_pb .array_value .values .add ()
370370 value2 .integer_value = values [2 ]
371371
372- self ._compareEntityProto (entity_pb , expected_pb )
372+ self ._compare_entity_proto (entity_pb , expected_pb )
373+
374+ def test_dict_to_entity (self ):
375+ from google .cloud .proto .datastore .v1 import entity_pb2
376+ from google .cloud .datastore .entity import Entity
377+
378+ entity = Entity ()
379+ entity ['a' ] = {'b' : u'c' }
380+ entity_pb = self ._call_fut (entity )
381+
382+ expected_pb = entity_pb2 .Entity (
383+ properties = {
384+ 'a' : entity_pb2 .Value (
385+ entity_value = entity_pb2 .Entity (
386+ properties = {
387+ 'b' : entity_pb2 .Value (
388+ string_value = 'c' ,
389+ ),
390+ },
391+ ),
392+ ),
393+ },
394+ )
395+ self .assertEqual (entity_pb , expected_pb )
396+
397+ def test_dict_to_entity_recursive (self ):
398+ from google .cloud .proto .datastore .v1 import entity_pb2
399+ from google .cloud .datastore .entity import Entity
400+
401+ entity = Entity ()
402+ entity ['a' ] = {
403+ 'b' : {
404+ 'c' : {
405+ 'd' : 1.25 ,
406+ },
407+ 'e' : True ,
408+ },
409+ 'f' : 10 ,
410+ }
411+ entity_pb = self ._call_fut (entity )
412+
413+ b_entity_pb = entity_pb2 .Entity (
414+ properties = {
415+ 'c' : entity_pb2 .Value (
416+ entity_value = entity_pb2 .Entity (
417+ properties = {
418+ 'd' : entity_pb2 .Value (
419+ double_value = 1.25 ,
420+ ),
421+ },
422+ ),
423+ ),
424+ 'e' : entity_pb2 .Value (boolean_value = True ),
425+ }
426+ )
427+ expected_pb = entity_pb2 .Entity (
428+ properties = {
429+ 'a' : entity_pb2 .Value (
430+ entity_value = entity_pb2 .Entity (
431+ properties = {
432+ 'b' : entity_pb2 .Value (
433+ entity_value = b_entity_pb ,
434+ ),
435+ 'f' : entity_pb2 .Value (
436+ integer_value = 10 ,
437+ ),
438+ },
439+ ),
440+ ),
441+ },
442+ )
443+ self .assertEqual (entity_pb , expected_pb )
373444
374445
375446class Test_key_from_protobuf (unittest .TestCase ):
@@ -516,6 +587,18 @@ def test_entity(self):
516587 self .assertEqual (name , 'entity_value' )
517588 self .assertIs (value , entity )
518589
590+ def test_dict (self ):
591+ from google .cloud .datastore .entity import Entity
592+
593+ orig_value = {'richard' : b'feynman' }
594+ name , value = self ._call_fut (orig_value )
595+ self .assertEqual (name , 'entity_value' )
596+ self .assertIsInstance (value , Entity )
597+ self .assertIsNone (value .key )
598+ self .assertEqual (value ._meanings , {})
599+ self .assertEqual (value .exclude_from_indexes , set ())
600+ self .assertEqual (dict (value ), orig_value )
601+
519602 def test_array (self ):
520603 values = ['a' , 0 , 3.14 ]
521604 name , value = self ._call_fut (values )
0 commit comments