Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compressed repeated to uncompressed property #772

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Next Next commit
fix: compressed repeated to uncompressed property
  • Loading branch information
zyzniewski committed Jul 5, 2023
commit 2ed9f8c9105854895e1303a9abac752970e561fa
6 changes: 5 additions & 1 deletion google/cloud/ndb/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2671,7 +2671,11 @@ def _from_datastore(self, ds_entity, value):
if self._name in ds_entity._meanings:
meaning = ds_entity._meanings[self._name][0]
if meaning == _MEANING_COMPRESSED and not self._compressed:
value.b_val = zlib.decompress(value.b_val)
if self._repeated:
for sub_value in value:
sub_value.b_val = zlib.decompress(sub_value.b_val)
else:
value.b_val = zlib.decompress(value.b_val)
return value

def _db_set_compressed_meaning(self, p):
Expand Down
31 changes: 29 additions & 2 deletions tests/unit/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1862,11 +1862,12 @@ class ThisKind(model.Model):
compressed_value_one = zlib.compress(uncompressed_value_one)
uncompressed_value_two = b"xyz" * 1000
compressed_value_two = zlib.compress(uncompressed_value_two)
datastore_entity.update({"foo": [compressed_value_one, compressed_value_two]})
compressed_value = [compressed_value_one, compressed_value_two]
datastore_entity.update({"foo": compressed_value})
meanings = {
"foo": (
model._MEANING_COMPRESSED,
[compressed_value_one, compressed_value_two],
compressed_value,
)
}
datastore_entity._meanings = meanings
Expand All @@ -1875,6 +1876,32 @@ class ThisKind(model.Model):
ds_entity = model._entity_to_ds_entity(entity)
assert ds_entity["foo"] == [compressed_value_one, compressed_value_two]

@staticmethod
@pytest.mark.usefixtures("in_context")
def test__from_datastore_compressed_repeated_to_uncompressed():
class ThisKind(model.Model):
foo = model.BlobProperty(compressed=False, repeated=True)

key = datastore.Key("ThisKind", 123, project="testing")
datastore_entity = datastore.Entity(key=key)
uncompressed_value_one = b"abc" * 1000
compressed_value_one = zlib.compress(uncompressed_value_one)
uncompressed_value_two = b"xyz" * 1000
compressed_value_two = zlib.compress(uncompressed_value_two)
compressed_value = [compressed_value_one, compressed_value_two]
datastore_entity.update({"foo": compressed_value})
meanings = {
"foo": (
model._MEANING_COMPRESSED,
compressed_value,
)
}
datastore_entity._meanings = meanings
protobuf = helpers.entity_to_protobuf(datastore_entity)
entity = model._entity_from_protobuf(protobuf)
ds_entity = model._entity_to_ds_entity(entity)
assert ds_entity["foo"] == [uncompressed_value_one, uncompressed_value_two]

@staticmethod
@pytest.mark.usefixtures("in_context")
def test__from_datastore_uncompressed_to_uncompressed():
Expand Down