File size: 10,660 Bytes
065fee7 |
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# 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.
import os
import proto
import pytest
import sys
def test_outer_enum_init():
class Foo(proto.Message):
color = proto.Field(proto.ENUM, number=1, enum="Color")
class Color(proto.Enum):
COLOR_UNSPECIFIED = 0
RED = 1
GREEN = 2
BLUE = 3
foo = Foo(color=Color.RED)
assert foo.color == Color.RED
assert foo.color == 1
assert foo.color
assert isinstance(foo.color, Color)
assert Foo.pb(foo).color == 1
def test_outer_enum_init_int():
class Foo(proto.Message):
color = proto.Field(proto.ENUM, number=1, enum="Color")
class Color(proto.Enum):
COLOR_UNSPECIFIED = 0
RED = 1
GREEN = 2
BLUE = 3
foo = Foo(color=1)
assert foo.color == Color.RED
assert foo.color == 1
assert foo.color
assert isinstance(foo.color, Color)
assert Foo.pb(foo).color == 1
def test_outer_enum_init_str():
class Foo(proto.Message):
color = proto.Field(proto.ENUM, number=1, enum="Color")
class Color(proto.Enum):
COLOR_UNSPECIFIED = 0
RED = 1
GREEN = 2
BLUE = 3
foo = Foo(color="RED")
assert foo.color == Color.RED
assert foo.color == 1
assert foo.color
assert isinstance(foo.color, Color)
assert Foo.pb(foo).color == 1
def test_outer_enum_init_dict():
class Foo(proto.Message):
color = proto.Field(proto.ENUM, number=1, enum="Color")
class Color(proto.Enum):
COLOR_UNSPECIFIED = 0
RED = 1
GREEN = 2
BLUE = 3
foo = Foo({"color": 1})
assert foo.color == Color.RED
assert foo.color == 1
assert foo.color
assert isinstance(foo.color, Color)
assert Foo.pb(foo).color == 1
def test_outer_enum_init_dict_str():
class Foo(proto.Message):
color = proto.Field(proto.ENUM, number=1, enum="Color")
class Color(proto.Enum):
COLOR_UNSPECIFIED = 0
RED = 1
GREEN = 2
BLUE = 3
foo = Foo({"color": "BLUE"})
assert foo.color == Color.BLUE
assert foo.color == 3
assert foo.color
assert isinstance(foo.color, Color)
assert Foo.pb(foo).color == 3
def test_outer_enum_init_pb2():
class Foo(proto.Message):
color = proto.Field(proto.ENUM, number=1, enum="Color")
class Color(proto.Enum):
COLOR_UNSPECIFIED = 0
RED = 1
GREEN = 2
BLUE = 3
foo = Foo(Foo.pb()(color=Color.RED))
assert foo.color == Color.RED
assert foo.color == 1
assert foo.color
assert isinstance(foo.color, Color)
assert Foo.pb(foo).color == 1
def test_outer_enum_unset():
class Foo(proto.Message):
color = proto.Field(proto.ENUM, number=1, enum="Color")
class Color(proto.Enum):
COLOR_UNSPECIFIED = 0
RED = 1
GREEN = 2
BLUE = 3
foo = Foo()
assert foo.color == Color.COLOR_UNSPECIFIED
assert foo.color == 0
assert "color" not in foo
assert not foo.color
assert Foo.pb(foo).color == 0
assert Foo.serialize(foo) == b""
def test_outer_enum_write():
class Foo(proto.Message):
color = proto.Field(proto.ENUM, number=1, enum="Color")
class Color(proto.Enum):
COLOR_UNSPECIFIED = 0
RED = 1
GREEN = 2
BLUE = 3
foo = Foo()
foo.color = Color.GREEN
assert foo.color == Color.GREEN
assert foo.color == 2
assert Foo.pb(foo).color == 2
assert foo.color
def test_outer_enum_write_int():
class Foo(proto.Message):
color = proto.Field(proto.ENUM, number=1, enum="Color")
class Color(proto.Enum):
COLOR_UNSPECIFIED = 0
RED = 1
GREEN = 2
BLUE = 3
foo = Foo()
foo.color = 3
assert foo.color == Color.BLUE
assert foo.color == 3
assert isinstance(foo.color, Color)
assert Foo.pb(foo).color == 3
assert foo.color
def test_outer_enum_write_str():
class Foo(proto.Message):
color = proto.Field(proto.ENUM, number=1, enum="Color")
class Color(proto.Enum):
COLOR_UNSPECIFIED = 0
RED = 1
GREEN = 2
BLUE = 3
foo = Foo()
foo.color = "BLUE"
assert foo.color == Color.BLUE
assert foo.color == 3
assert isinstance(foo.color, Color)
assert Foo.pb(foo).color == 3
assert foo.color
def test_inner_enum_init():
class Foo(proto.Message):
class Color(proto.Enum):
COLOR_UNSPECIFIED = 0
RED = 1
GREEN = 2
BLUE = 3
color = proto.Field(Color, number=1)
foo = Foo(color=Foo.Color.RED)
assert foo.color == Foo.Color.RED
assert foo.color == 1
assert foo.color
assert Foo.pb(foo).color == 1
def test_inner_enum_write():
class Foo(proto.Message):
class Color(proto.Enum):
COLOR_UNSPECIFIED = 0
RED = 1
GREEN = 2
BLUE = 3
color = proto.Field(Color, number=1)
foo = Foo()
foo.color = Foo.Color.GREEN
assert foo.color == Foo.Color.GREEN
assert foo.color == 2
assert isinstance(foo.color, Foo.Color)
assert Foo.pb(foo).color == 2
assert foo.color
def test_enum_del():
class Foo(proto.Message):
color = proto.Field(proto.ENUM, number=1, enum="Color")
class Color(proto.Enum):
COLOR_UNSPECIFIED = 0
RED = 1
GREEN = 2
BLUE = 3
foo = Foo(color=Color.BLUE)
del foo.color
assert foo.color == Color.COLOR_UNSPECIFIED
assert foo.color == 0
assert isinstance(foo.color, Color)
assert "color" not in foo
assert not foo.color
assert Foo.pb(foo).color == 0
def test_nested_enum_from_string():
class Trawl(proto.Message):
# Note: this indirection with the nested field
# is necessary to trigger the exception for testing.
# Setting the field in an existing message accepts strings AND
# checks for valid variants.
# Similarly, constructing a message directly with a top level
# enum field kwarg passed as a string is also handled correctly, i.e.
# s = Squid(zone="ABYSSOPELAGIC")
# does NOT raise an exception.
squids = proto.RepeatedField("Squid", number=1)
class Squid(proto.Message):
zone = proto.Field(proto.ENUM, number=1, enum="Zone")
class Zone(proto.Enum):
EPIPELAGIC = 0
MESOPELAGIC = 1
BATHYPELAGIC = 2
ABYSSOPELAGIC = 3
t = Trawl(squids=[{"zone": "MESOPELAGIC"}])
assert t.squids[0] == Squid(zone=Zone.MESOPELAGIC)
def test_enum_field_by_string():
class Squid(proto.Message):
zone = proto.Field(proto.ENUM, number=1, enum="Zone")
class Zone(proto.Enum):
EPIPELAGIC = 0
MESOPELAGIC = 1
BATHYPELAGIC = 2
ABYSSOPELAGIC = 3
s = Squid(zone=Zone.BATHYPELAGIC)
assert s.zone == Zone.BATHYPELAGIC
def test_enum_field_by_string_with_package():
sys.modules[__name__].__protobuf__ = proto.module(package="mollusca.cephalopoda")
try:
class Octopus(proto.Message):
zone = proto.Field(proto.ENUM, number=1, enum="mollusca.cephalopoda.Zone")
class Zone(proto.Enum):
EPIPELAGIC = 0
MESOPELAGIC = 1
BATHYPELAGIC = 2
ABYSSOPELAGIC = 3
finally:
del sys.modules[__name__].__protobuf__
o = Octopus(zone="MESOPELAGIC")
assert o.zone == Zone.MESOPELAGIC
def test_enums_in_different_files():
import mollusc
import zone
m = mollusc.Mollusc(zone="BATHYPELAGIC")
assert m.zone == zone.Zone.BATHYPELAGIC
def test_enums_in_one_file():
import clam
c = clam.Clam(species=clam.Species.DURASA)
assert c.species == clam.Species.DURASA
def test_unwrapped_enum_fields():
# The dayofweek_pb2 module apparently does some things that are deprecated
# in the protobuf API.
# There's nothing we can do about that, so ignore it.
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
from google.type import dayofweek_pb2 as dayofweek
class Event(proto.Message):
weekday = proto.Field(proto.ENUM, number=1, enum=dayofweek.DayOfWeek)
e = Event(weekday="WEDNESDAY")
e2 = Event.deserialize(Event.serialize(e))
assert e == e2
class Task(proto.Message):
weekday = proto.Field(dayofweek.DayOfWeek, number=1)
t = Task(weekday="TUESDAY")
t2 = Task.deserialize(Task.serialize(t))
assert t == t2
if os.environ.get("PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION", "python") == "cpp":
# This test only works, and is only relevant, with the cpp runtime.
# Python just doesn't give a care and lets it work anyway.
def test_enum_alias_bad():
# Certain enums may shadow the different enum monikers with the same value.
# This is generally discouraged, and protobuf will object by default,
# but will explicitly allow this behavior if the enum is defined with
# the `allow_alias` option set.
with pytest.raises(TypeError):
# The wrapper message is a hack to avoid manifest wrangling to
# define the enum.
class BadMessage(proto.Message):
class BadEnum(proto.Enum):
UNKNOWN = 0
DEFAULT = 0
bad_dup_enum = proto.Field(proto.ENUM, number=1, enum=BadEnum)
def test_enum_alias_good():
# Have to split good and bad enum alias into two tests so that the generated
# file descriptor is properly created.
# For the python runtime, aliases are allowed by default, but we want to
# make sure that the options don't cause problems.
# For the cpp runtime, we need to verify that we can in fact define aliases.
class GoodMessage(proto.Message):
class GoodEnum(proto.Enum):
_pb_options = {"allow_alias": True}
UNKNOWN = 0
DEFAULT = 0
good_dup_enum = proto.Field(proto.ENUM, number=1, enum=GoodEnum)
assert GoodMessage.GoodEnum.UNKNOWN == GoodMessage.GoodEnum.DEFAULT == 0
|