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(model): Ensure repeated props have same kind when converting from ds #824

Merged
merged 1 commit into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions google/cloud/ndb/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ def _entity_from_ds_entity(ds_entity, model_class=None):
Args:
ds_entity (google.cloud.datastore_v1.types.Entity): An entity to be
deserialized.
model_class (class): Optional; ndb Model class type.

Returns:
.Model: The deserialized entity.
Expand Down Expand Up @@ -623,10 +624,14 @@ def new_entity(key):
# different lengths for the subproperties, which was a
# bug. We work around this when reading out such values
# by making sure our repeated property is the same
# length as the longest suproperty.
# length as the longest subproperty.
# Make sure to create a key of the same kind as
# the other entries in the value list
while len(subvalue) > len(value):
# Need to make some more subentities
value.append(new_entity(key._key))
expando_kind = structprop._model_class._get_kind()
expando_key = key_module.Key(expando_kind, None)
value.append(new_entity(expando_key._key))

# Branch coverage bug,
# See: https://github.com/nedbat/coveragepy/issues/817
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5606,6 +5606,40 @@ class ThisKind(model.Model):
assert entity.baz[2].bar == "iminjail"
assert entity.copacetic is True

@staticmethod
@pytest.mark.usefixtures("in_context")
def test_legacy_repeated_structured_property_uneven_expandos():
class Expando1(model.Expando):
bar = model.StringProperty()

class Expando2(model.Expando):
qux = model.StringProperty()

class ThisKind(model.Model):
foo = model.StructuredProperty(model_class=Expando1, repeated=True)
baz = model.StructuredProperty(model_class=Expando2, repeated=True)

key = datastore.Key("ThisKind", 123, project="testing")
datastore_entity = datastore.Entity(key=key)
datastore_entity.items = mock.Mock(
return_value=(
# Order matters here
("foo.bar", ["foo_bar_1"]),
("baz.qux", ["baz_qux_1", "baz_qux_2"]),
("foo.custom_1", ["foo_c1_1", "foo_c1_2"]), # longer than foo.bar
)
)
entity = model._entity_from_ds_entity(datastore_entity)
assert isinstance(entity, ThisKind)
assert len(entity.foo) == 2
assert len(entity.baz) == 2
assert entity.foo[0].bar == "foo_bar_1"
assert entity.foo[0].custom_1 == "foo_c1_1"
assert entity.foo[1].bar is None
assert entity.foo[1].custom_1 == "foo_c1_2"
assert entity.baz[0].qux == "baz_qux_1"
assert entity.baz[1].qux == "baz_qux_2"

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