Skip to content

Commit

Permalink
fix: correctly decode falsy values in legacy protocol buffers (#628)
Browse files Browse the repository at this point in the history
Fixes #625
  • Loading branch information
Chris Rossi authored Apr 19, 2021
1 parent 3d8ecdf commit 69a9f63
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion google/cloud/ndb/_legacy_entity_pb.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ def entity_props(self):
for prop in self.property_list():
name = prop.name().decode("utf-8")
entity_props[name] = (
prop.has_value() and self._get_property_value(prop.value()) or None
self._get_property_value(prop.value()) if prop.has_value() else None
)
return entity_props

Expand Down
23 changes: 17 additions & 6 deletions tests/system/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,20 +459,24 @@ def test_query():

@pytest.mark.usefixtures("client_context")
def test_legacy_local_structured_property_with_boolean(ds_entity):
"""Regression test for #623
"""Regression test for #623, #625
https://github.com/googleapis/python-ndb/issues/623
https://github.com/googleapis/python-ndb/issues/625
"""
children = [
b"x\x9c\xab\xe2\x96bNJ,R`\xd0b\x12`\xac\x12\xe1\xe0\x97bN\xcb\xcf\x07r9\xa5"
b"\xd832\x15r\xf3s\x15\x01u_\x07\n"
b"\xd832\x15r\xf3s\x15\x01u_\x07\n",
b"x\x9c\xab\xe2\x96bNJ,R`\xd0b\x12`\xa8\x12\xe7\xe0\x97bN\xcb\xcf\x07ry\xa4"
b"\xb82Rsr\xf2\x15R\x12S\x14\x01\x8e\xbf\x085",
]

entity_id = test_utils.system.unique_resource_id()
ds_entity(KIND, entity_id, children=children)

class OtherKind(ndb.Model):
foo = ndb.StringProperty()
bar = ndb.BooleanProperty(default=True)
bar = ndb.BooleanProperty(required=True, default=True)

class SomeKind(ndb.Model):
children = ndb.LocalStructuredProperty(
Expand All @@ -481,12 +485,19 @@ class SomeKind(ndb.Model):

entity = SomeKind.get_by_id(entity_id)

assert len(entity.children) == 1
assert len(entity.children) == 2
assert entity.children[0].foo == "hi mom!"
assert entity.children[0].bar is True
assert entity.children[1].foo == "hello dad!"
assert entity.children[1].bar is False

entity.children[0].foo = "hello dad!"
entity.children.append(OtherKind(foo="i'm in jail!", bar=False))
entity.put()

entity = SomeKind.get_by_id(entity_id)
assert entity.children[0].foo == "hello dad!"
assert entity.children[0].foo == "hi mom!"
assert entity.children[0].bar is True
assert entity.children[1].foo == "hello dad!"
assert entity.children[1].bar is False
assert entity.children[2].foo == "i'm in jail!"
assert entity.children[2].bar is False

0 comments on commit 69a9f63

Please sign in to comment.