|
| 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 |
0 commit comments