Skip to content

Commit d156d3e

Browse files
authored
fix: use correct name when reading legacy structured properties with names (#347)
Refs #345.
1 parent ae8f533 commit d156d3e

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,9 @@ def new_entity(key):
578578

579579
if prop is None and "." in name:
580580
supername, subname = name.split(".", 1)
581+
# Code name for structured property could be different than stored
582+
# name if ``name`` was set when defined.
583+
supername = model_class._code_name_from_stored_name(supername)
581584
structprop = getattr(model_class, supername, None)
582585
if isinstance(structprop, StructuredProperty):
583586
subvalue = value

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,38 @@ class SomeKind(ndb.Model):
14441444
assert results[0].foo == 1
14451445

14461446

1447+
@pytest.mark.usefixtures("client_context")
1448+
def test_query_legacy_repeated_structured_property_with_name(ds_entity):
1449+
class OtherKind(ndb.Model):
1450+
one = ndb.StringProperty()
1451+
two = ndb.StringProperty()
1452+
three = ndb.StringProperty()
1453+
1454+
class SomeKind(ndb.Model):
1455+
foo = ndb.IntegerProperty()
1456+
bar = ndb.StructuredProperty(OtherKind, "b", repeated=True)
1457+
1458+
entity_id = test_utils.system.unique_resource_id()
1459+
ds_entity(
1460+
KIND,
1461+
entity_id,
1462+
**{
1463+
"foo": 1,
1464+
"b.one": [u"pish", u"bish"],
1465+
"b.two": [u"posh", u"bosh"],
1466+
"b.three": [u"pash", u"bash"],
1467+
}
1468+
)
1469+
1470+
eventually(SomeKind.query().fetch, _length_equals(1))
1471+
1472+
query = SomeKind.query()
1473+
1474+
results = query.fetch()
1475+
assert len(results) == 1
1476+
assert results[0].bar[0].one == u"pish"
1477+
1478+
14471479
@pytest.mark.usefixtures("client_context")
14481480
def test_fetch_page_with_repeated_structured_property(dispose_of):
14491481
"""Regression test for Issue #254.

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5321,6 +5321,38 @@ class ThisKind(model.Model):
53215321
assert entity.baz[2].bar == "iminjail"
53225322
assert entity.copacetic is True
53235323

5324+
@staticmethod
5325+
@pytest.mark.usefixtures("in_context")
5326+
def test_legacy_repeated_structured_property_with_name():
5327+
class OtherKind(model.Model):
5328+
foo = model.IntegerProperty()
5329+
bar = model.StringProperty()
5330+
5331+
class ThisKind(model.Model):
5332+
baz = model.StructuredProperty(OtherKind, "b", repeated=True)
5333+
copacetic = model.BooleanProperty()
5334+
5335+
key = datastore.Key("ThisKind", 123, project="testing")
5336+
datastore_entity = datastore.Entity(key=key)
5337+
datastore_entity.items = mock.Mock(
5338+
return_value=(
5339+
# Order counts for coverage
5340+
("b.foo", [42, 144]),
5341+
("b.bar", ["himom", "hellodad", "iminjail"]),
5342+
("copacetic", True),
5343+
)
5344+
)
5345+
5346+
entity = model._entity_from_ds_entity(datastore_entity)
5347+
assert isinstance(entity, ThisKind)
5348+
assert entity.baz[0].foo == 42
5349+
assert entity.baz[0].bar == "himom"
5350+
assert entity.baz[1].foo == 144
5351+
assert entity.baz[1].bar == "hellodad"
5352+
assert entity.baz[2].foo is None
5353+
assert entity.baz[2].bar == "iminjail"
5354+
assert entity.copacetic is True
5355+
53245356
@staticmethod
53255357
@pytest.mark.usefixtures("in_context")
53265358
def test_polymodel():

0 commit comments

Comments
 (0)