File tree Expand file tree Collapse file tree
packages/google-cloud-ndb Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2494,9 +2494,14 @@ def _from_base_type(self, value):
24942494 indicate that the value didn't need to be unwrapped and
24952495 decompressed.
24962496 """
2497+ # First, check for legacy compressed LocalStructuredProperty values.
2498+ # See https://github.com/googleapis/python-ndb/issues/359
2499+ if self ._compressed and isinstance (value , ds_entity_module .Entity ):
2500+ return
2501+
24972502 if self ._compressed and not isinstance (value , _CompressedValue ):
24982503 if not value .startswith (_ZLIB_COMPRESSION_MARKER ):
2499- value = zlib . compress ( value )
2504+ return value
25002505 value = _CompressedValue (value )
25012506
25022507 if isinstance (value , _CompressedValue ):
Original file line number Diff line number Diff line change @@ -421,6 +421,28 @@ class House(ndb.Model):
421421 assert retrieved .dogs == dogs
422422
423423
424+ def test_get_by_id_with_compressed_repeated_local_structured_property (
425+ client_context , dispose_of , ds_client
426+ ):
427+ class Dog (ndb .Model ):
428+ name = ndb .TextProperty ()
429+
430+ class House (ndb .Model ):
431+ dogs = ndb .LocalStructuredProperty (Dog , repeated = True , compressed = True )
432+
433+ with client_context .new (legacy_data = True ).use ():
434+ entity = House ()
435+ dogs = [Dog (name = "Mika" ), Dog (name = "Mocha" )]
436+ entity .dogs = dogs
437+
438+ key = entity .put ()
439+ house_id = key .id ()
440+ dispose_of (key ._key )
441+
442+ retrieved = House .get_by_id (house_id )
443+ assert retrieved .dogs == dogs
444+
445+
424446@pytest .mark .usefixtures ("client_context" )
425447def test_retrieve_entity_with_legacy_compressed_property (
426448 ds_entity_with_meanings ,
Original file line number Diff line number Diff line change @@ -3744,6 +3744,18 @@ class SomeKind(model.Model):
37443744 ds_entity = model ._entity_to_ds_entity (entity .foo , set_key = False )
37453745 assert data == {"foo" : ds_entity }
37463746
3747+ @staticmethod
3748+ def test_legacy_repeated_compressed_local_structured_property ():
3749+ class SubKind (model .Model ):
3750+ bar = model .TextProperty ()
3751+
3752+ prop = model .LocalStructuredProperty (
3753+ SubKind , repeated = True , compressed = True
3754+ )
3755+ entity = SubKind (bar = "baz" )
3756+ ds_entity = model ._entity_to_ds_entity (entity , set_key = False )
3757+ assert prop ._call_from_base_type (ds_entity ) == entity
3758+
37473759
37483760class TestGenericProperty :
37493761 @staticmethod
You can’t perform that action at this time.
0 commit comments