[go: up one dir, main page]

Skip to content

Commit

Permalink
fix: compressed repeated to uncompressed property (#772)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyzniewski committed Feb 27, 2024
1 parent 7e8481d commit dab9edf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
6 changes: 5 additions & 1 deletion google/cloud/ndb/model.py
Expand Up @@ -2672,7 +2672,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
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

0 comments on commit dab9edf

Please sign in to comment.