Skip to content

Commit 93d6048

Browse files
cguardiacrwilcox
authored andcommitted
fix error retrieving values for properties with different stored name (#187)
1 parent 151e250 commit 93d6048

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ def _entity_from_ds_entity(ds_entity, model_class=None):
547547
entity._key = key_module.Key._from_ds_key(ds_entity.key)
548548

549549
for name, value in ds_entity.items():
550+
# If ``name`` was used to define the property, ds_entity name will not
551+
# match model property name.
552+
name = model_class._code_name_from_stored_name(name)
553+
550554
prop = getattr(model_class, name, None)
551555

552556
# Backwards compatibility shim. NDB previously stored structured
@@ -5678,6 +5682,15 @@ def _to_dict(self, include=None, *, exclude=None):
56785682

56795683
to_dict = _to_dict
56805684

5685+
@classmethod
5686+
def _code_name_from_stored_name(cls, name):
5687+
"""Return the code name from a property when it's different from the
5688+
stored name. Used in deserialization from datastore."""
5689+
if name in cls._properties:
5690+
if name != cls._properties[name]._code_name:
5691+
name = cls._properties[name]._code_name
5692+
return name
5693+
56815694
@classmethod
56825695
def _pre_allocate_ids_hook(cls, size, max, parent):
56835696
pass

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,25 @@ class SomeKind(ndb.Model):
228228
dispose_of(key._key)
229229

230230

231+
@pytest.mark.usefixtures("client_context")
232+
def test_insert_entity_with_stored_name_property(dispose_of, ds_client):
233+
class SomeKind(ndb.Model):
234+
foo = ndb.StringProperty()
235+
bar = ndb.StringProperty(name="notbar")
236+
237+
entity = SomeKind(foo="something", bar="or other")
238+
key = entity.put()
239+
240+
retrieved = key.get()
241+
assert retrieved.foo == "something"
242+
assert retrieved.bar == "or other"
243+
244+
ds_entity = ds_client.get(key._key)
245+
assert ds_entity["notbar"] == "or other"
246+
247+
dispose_of(key._key)
248+
249+
231250
@pytest.mark.usefixtures("client_context")
232251
def test_insert_roundtrip_naive_datetime(dispose_of, ds_client):
233252
class SomeKind(ndb.Model):

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4717,6 +4717,15 @@ class Simple(model.Model):
47174717
entity = Simple(foo=3, bar="baz", projection=("foo",))
47184718
assert entity.to_dict() == {"foo": 3}
47194719

4720+
@staticmethod
4721+
def test__code_name_from_stored_name():
4722+
class Simple(model.Model):
4723+
foo = model.StringProperty()
4724+
bar = model.StringProperty(name="notbar")
4725+
4726+
assert Simple._code_name_from_stored_name("foo") == "foo"
4727+
assert Simple._code_name_from_stored_name("notbar") == "bar"
4728+
47204729

47214730
class Test_entity_from_protobuf:
47224731
@staticmethod

0 commit comments

Comments
 (0)