Skip to content

Commit 8886ad7

Browse files
authored
fix: check for legacy local structured property values (#365)
* fix: check for legacy local structured property values when converting from base type refs #359 * fix: avoid compressing and immediately decompressing text property value when converting
1 parent b5cf45f commit 8886ad7

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

packages/google-cloud-ndb/google/cloud/ndb/model.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff 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):

packages/google-cloud-ndb/tests/system/test_crud.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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")
425447
def test_retrieve_entity_with_legacy_compressed_property(
426448
ds_entity_with_meanings,

packages/google-cloud-ndb/tests/unit/test_model.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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

37483760
class TestGenericProperty:
37493761
@staticmethod

0 commit comments

Comments
 (0)