1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
# Copyright 2019 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.
from unittest import mock
import warnings
import proto
from proto.marshal.rules.enums import EnumRule
__protobuf__ = proto.module(package="test.marshal.enum")
def test_to_proto():
class Foo(proto.Enum):
FOO_UNSPECIFIED = 0
BAR = 1
BAZ = 2
enum_rule = EnumRule(Foo)
foo_a = enum_rule.to_proto(Foo.BAR)
foo_b = enum_rule.to_proto(1)
foo_c = enum_rule.to_proto("BAR")
# We want to distinguish literal `1` from `Foo.BAR` here
# (they are equivalent but not identical).
assert foo_a is foo_b is foo_c is 1 # noqa: F632
def test_to_python():
class Foo(proto.Enum):
FOO_UNSPECIFIED = 0
BAR = 1
BAZ = 2
enum_rule = EnumRule(Foo)
foo_a = enum_rule.to_python(1)
foo_b = enum_rule.to_python(Foo.BAR)
assert foo_a is foo_b is Foo.BAR
def test_to_python_unknown_value():
class Foo(proto.Enum):
FOO_UNSPECIFIED = 0
BAR = 1
BAZ = 2
enum_rule = EnumRule(Foo)
with mock.patch.object(warnings, "warn") as warn:
assert enum_rule.to_python(4) == 4
warn.assert_called_once_with("Unrecognized Foo enum value: 4")
def test_enum_append():
class Bivalve(proto.Enum):
CLAM = 0
OYSTER = 1
class MolluscContainer(proto.Message):
bivalves = proto.RepeatedField(
proto.ENUM,
number=1,
enum=Bivalve,
)
mc = MolluscContainer()
clam = Bivalve.CLAM
mc.bivalves.append(clam)
mc.bivalves.append(1)
assert mc.bivalves == [clam, Bivalve.OYSTER]
def test_enum_map_insert():
class Bivalve(proto.Enum):
CLAM = 0
OYSTER = 1
class MolluscContainer(proto.Message):
bivalves = proto.MapField(
proto.STRING,
proto.ENUM,
number=1,
enum=Bivalve,
)
mc = MolluscContainer()
clam = Bivalve.CLAM
mc.bivalves["clam"] = clam
mc.bivalves["oyster"] = 1
assert dict(mc.bivalves) == {"clam": clam, "oyster": Bivalve.OYSTER}
|