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: Don't set key on structured property entities. #312

Merged
merged 1 commit into from
Feb 5, 2020
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
2 changes: 1 addition & 1 deletion google/cloud/ndb/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4026,7 +4026,7 @@ def _to_base_type(self, value):
"Cannot convert to protocol buffer. Expected {} value; "
"received {}".format(self._model_class.__name__, value)
)
return _entity_to_ds_entity(value)
return _entity_to_ds_entity(value, set_key=False)

def _from_base_type(self, value):
"""Convert a value from the "base" value type for this property.
Expand Down
62 changes: 62 additions & 0 deletions tests/system/test_misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Difficult to classify regression tests.
"""
import pickle

import pytest
import six

from google.cloud import ndb


# Pickle can only pickle/unpickle global classes
class PickleOtherKind(ndb.Model):
foo = ndb.IntegerProperty()

@classmethod
def _get_kind(cls):
return "OtherKind"


class PickleSomeKind(ndb.Model):
other = ndb.StructuredProperty(PickleOtherKind)

@classmethod
def _get_kind(cls):
return "SomeKind"


@pytest.mark.skipif(
six.PY2, reason="Pickling doesn't work in Python 2. See: Issue #311"
)
@pytest.mark.usefixtures("client_context")
def test_pickle_roundtrip_structured_property(dispose_of):
"""Regression test for Issue #281.

https://github.com/googleapis/python-ndb/issues/281
"""
ndb.Model._kind_map["SomeKind"] = PickleSomeKind
ndb.Model._kind_map["OtherKind"] = PickleOtherKind

entity = PickleSomeKind(other=PickleOtherKind(foo=1))
key = entity.put()
dispose_of(key._key)

entity = key.get(use_cache=False)
assert entity.other.key is None or entity.other.key.id() is None
entity = pickle.loads(pickle.dumps(entity))
assert entity.other.foo == 1
2 changes: 1 addition & 1 deletion tests/unit/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3320,7 +3320,7 @@ class MineToo(model.Model):
ds_bar = MineToo.bar._to_base_type(minetoo.bar)
assert isinstance(ds_bar, entity_module.Entity)
assert ds_bar["foo"] == "bar"
assert ds_bar.kind == "Mine"
assert ds_bar.key is None

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