Skip to content

Commit 7e3b7aa

Browse files
author
Chris Rossi
authored
fix: fix bug with compressed blob property (#615)
There was a bug when using a compressed blob property as a child of a structured property while using the legacy data format for structured properties. Fixes #602
1 parent cd8c39e commit 7e3b7aa

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,10 +2611,11 @@ def _to_datastore(self, entity, data, prefix="", repeated=False):
26112611
entity, data, prefix=prefix, repeated=repeated
26122612
)
26132613
if self._compressed:
2614-
value = data[self._name]
2614+
key = prefix + self._name
2615+
value = data[key]
26152616
if isinstance(value, _CompressedValue):
26162617
value = value.z_val
2617-
data[self._name] = value
2618+
data[key] = value
26182619

26192620
if self._repeated:
26202621
compressed_value = []
@@ -2623,14 +2624,14 @@ def _to_datastore(self, entity, data, prefix="", repeated=False):
26232624
rval = zlib.compress(rval)
26242625
compressed_value.append(rval)
26252626
value = compressed_value
2626-
data[self._name] = value
2627+
data[key] = value
26272628
if not self._repeated:
26282629
if value and not value.startswith(_ZLIB_COMPRESSION_MARKER):
26292630
value = zlib.compress(value)
2630-
data[self._name] = value
2631+
data[key] = value
26312632

26322633
if value:
2633-
data.setdefault("_meanings", {})[self._name] = (
2634+
data.setdefault("_meanings", {})[key] = (
26342635
_MEANING_COMPRESSED,
26352636
value,
26362637
)

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,3 +1699,25 @@ class Container(ndb.Model):
16991699

17001700
entity = entity.key.get()
17011701
assert isinstance(entity.children[0], Base)
1702+
1703+
1704+
def test_structured_property_with_nested_compressed_json_property_using_legacy_format(
1705+
client_context, dispose_of
1706+
):
1707+
"""Regression test for #602
1708+
1709+
https://github.com/googleapis/python-ndb/issues/602
1710+
"""
1711+
1712+
class OtherKind(ndb.Model):
1713+
data = ndb.JsonProperty(compressed=True)
1714+
1715+
class SomeKind(ndb.Model):
1716+
sub_model = ndb.StructuredProperty(OtherKind)
1717+
1718+
with client_context.new(legacy_data=True).use():
1719+
model = SomeKind(sub_model=OtherKind(data={"test": 1}))
1720+
key = model.put()
1721+
dispose_of(key._key)
1722+
1723+
assert key.get().sub_model.data["test"] == 1

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,6 +1764,28 @@ class ThisKind(model.Model):
17641764
assert ds_entity._meanings["foo"][0] == model._MEANING_COMPRESSED
17651765
assert ds_entity._meanings["foo"][1] == compressed_value
17661766

1767+
@staticmethod
1768+
def test__to_datastore_legacy_compressed_with_prefix(in_context):
1769+
"""Regression test for #602
1770+
1771+
https://github.com/googleapis/python-ndb/issues/602
1772+
"""
1773+
1774+
class ThisKind(model.Model):
1775+
bar = model.BlobProperty(compressed=True)
1776+
1777+
class ParentKind(model.Model):
1778+
foo = model.StructuredProperty(ThisKind)
1779+
1780+
with in_context.new(legacy_data=True).use():
1781+
uncompressed_value = b"abc" * 1000
1782+
compressed_value = zlib.compress(uncompressed_value)
1783+
entity = ParentKind(foo=ThisKind(bar=uncompressed_value))
1784+
ds_entity = model._entity_to_ds_entity(entity)
1785+
assert "foo.bar" in ds_entity._meanings
1786+
assert ds_entity._meanings["foo.bar"][0] == model._MEANING_COMPRESSED
1787+
assert ds_entity._meanings["foo.bar"][1] == compressed_value
1788+
17671789
@staticmethod
17681790
@pytest.mark.usefixtures("in_context")
17691791
def test__to_datastore_compressed_repeated():

0 commit comments

Comments
 (0)