|
| 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 | +import itertools |
| 16 | +import pickle |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +import proto |
| 21 | + |
| 22 | + |
| 23 | +class Squid(proto.Message): |
| 24 | + # Test primitives, enums, and repeated fields. |
| 25 | + class Chromatophore(proto.Message): |
| 26 | + class Color(proto.Enum): |
| 27 | + UNKNOWN = 0 |
| 28 | + RED = 1 |
| 29 | + BROWN = 2 |
| 30 | + WHITE = 3 |
| 31 | + BLUE = 4 |
| 32 | + |
| 33 | + color = proto.Field(Color, number=1) |
| 34 | + |
| 35 | + mass_kg = proto.Field(proto.INT32, number=1) |
| 36 | + chromatophores = proto.RepeatedField(Chromatophore, number=2) |
| 37 | + |
| 38 | + |
| 39 | +def test_pickling(): |
| 40 | + |
| 41 | + s = Squid(mass_kg=20) |
| 42 | + colors = ["RED", "BROWN", "WHITE", "BLUE"] |
| 43 | + s.chromatophores = [ |
| 44 | + {"color": c} for c in itertools.islice(itertools.cycle(colors), 10) |
| 45 | + ] |
| 46 | + |
| 47 | + pickled = pickle.dumps(s) |
| 48 | + |
| 49 | + unpickled = pickle.loads(pickled) |
| 50 | + |
| 51 | + assert unpickled == s |
0 commit comments