Skip to content

Commit 799dd77

Browse files
author
Chris Rossi
authored
fix: don't set key on structured property entities (#312)
Fixes #281.
1 parent df9d672 commit 799dd77

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4026,7 +4026,7 @@ def _to_base_type(self, value):
40264026
"Cannot convert to protocol buffer. Expected {} value; "
40274027
"received {}".format(self._model_class.__name__, value)
40284028
)
4029-
return _entity_to_ds_entity(value)
4029+
return _entity_to_ds_entity(value, set_key=False)
40304030

40314031
def _from_base_type(self, value):
40324032
"""Convert a value from the "base" value type for this property.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
Difficult to classify regression tests.
17+
"""
18+
import pickle
19+
20+
import pytest
21+
import six
22+
23+
from google.cloud import ndb
24+
25+
26+
# Pickle can only pickle/unpickle global classes
27+
class PickleOtherKind(ndb.Model):
28+
foo = ndb.IntegerProperty()
29+
30+
@classmethod
31+
def _get_kind(cls):
32+
return "OtherKind"
33+
34+
35+
class PickleSomeKind(ndb.Model):
36+
other = ndb.StructuredProperty(PickleOtherKind)
37+
38+
@classmethod
39+
def _get_kind(cls):
40+
return "SomeKind"
41+
42+
43+
@pytest.mark.skipif(
44+
six.PY2, reason="Pickling doesn't work in Python 2. See: Issue #311"
45+
)
46+
@pytest.mark.usefixtures("client_context")
47+
def test_pickle_roundtrip_structured_property(dispose_of):
48+
"""Regression test for Issue #281.
49+
50+
https://github.com/googleapis/python-ndb/issues/281
51+
"""
52+
ndb.Model._kind_map["SomeKind"] = PickleSomeKind
53+
ndb.Model._kind_map["OtherKind"] = PickleOtherKind
54+
55+
entity = PickleSomeKind(other=PickleOtherKind(foo=1))
56+
key = entity.put()
57+
dispose_of(key._key)
58+
59+
entity = key.get(use_cache=False)
60+
assert entity.other.key is None or entity.other.key.id() is None
61+
entity = pickle.loads(pickle.dumps(entity))
62+
assert entity.other.foo == 1

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3320,7 +3320,7 @@ class MineToo(model.Model):
33203320
ds_bar = MineToo.bar._to_base_type(minetoo.bar)
33213321
assert isinstance(ds_bar, entity_module.Entity)
33223322
assert ds_bar["foo"] == "bar"
3323-
assert ds_bar.kind == "Mine"
3323+
assert ds_bar.key is None
33243324

33253325
@staticmethod
33263326
@pytest.mark.usefixtures("in_context")

0 commit comments

Comments
 (0)