|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import pytest |
|
|
|
import proto |
|
|
|
|
|
def test_int_init(): |
|
class Foo(proto.Message): |
|
bar = proto.Field(proto.INT32, number=1) |
|
baz = proto.Field(proto.INT32, number=2) |
|
|
|
foo = Foo(bar=42) |
|
assert foo.bar == 42 |
|
assert foo.baz == 0 |
|
assert not foo.baz |
|
assert Foo.pb(foo).bar == 42 |
|
assert Foo.pb(foo).baz == 0 |
|
|
|
|
|
def test_int_rmw(): |
|
class Foo(proto.Message): |
|
spam = proto.Field(proto.INT32, number=1) |
|
eggs = proto.Field(proto.INT32, number=2) |
|
|
|
foo = Foo(spam=42) |
|
foo.eggs = 76 |
|
assert foo.spam == 42 |
|
assert foo.eggs == 76 |
|
assert Foo.pb(foo).spam == 42 |
|
assert Foo.pb(foo).eggs == 76 |
|
foo.spam = 144 |
|
assert foo.spam == 144 |
|
assert foo.eggs == 76 |
|
assert Foo.pb(foo).spam == 144 |
|
assert Foo.pb(foo).eggs == 76 |
|
|
|
|
|
def test_int_del(): |
|
class Foo(proto.Message): |
|
bar = proto.Field(proto.INT32, number=1) |
|
|
|
foo = Foo(bar=42) |
|
assert foo.bar == 42 |
|
del foo.bar |
|
assert foo.bar == 0 |
|
assert not foo.bar |
|
|
|
|
|
def test_int_size(): |
|
class Foo(proto.Message): |
|
small = proto.Field(proto.INT32, number=1) |
|
big = proto.Field(proto.INT64, number=2) |
|
|
|
foo = Foo() |
|
foo.big = 2**40 |
|
assert foo.big == 2**40 |
|
with pytest.raises(ValueError): |
|
foo.small = 2**40 |
|
with pytest.raises(ValueError): |
|
Foo(small=2**40) |
|
|
|
|
|
def test_int_unsigned(): |
|
class Foo(proto.Message): |
|
signed = proto.Field(proto.INT32, number=1) |
|
unsigned = proto.Field(proto.UINT32, number=2) |
|
|
|
foo = Foo() |
|
foo.signed = -10 |
|
assert foo.signed == -10 |
|
with pytest.raises(ValueError): |
|
foo.unsigned = -10 |
|
with pytest.raises(ValueError): |
|
Foo(unsigned=-10) |
|
|
|
|
|
def test_field_descriptor_idempotent(): |
|
class Foo(proto.Message): |
|
bar = proto.Field(proto.INT32, number=1) |
|
|
|
bar_field = Foo.meta.fields["bar"] |
|
assert bar_field.descriptor is bar_field.descriptor |
|
|
|
|
|
def test_int64_dict_round_trip(): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Squid(proto.Message): |
|
mass_kg = proto.Field(proto.INT64, number=1) |
|
length_cm = proto.Field(proto.UINT64, number=2) |
|
age_s = proto.Field(proto.FIXED64, number=3) |
|
depth_m = proto.Field(proto.SFIXED64, number=4) |
|
serial_num = proto.Field(proto.SINT64, number=5) |
|
|
|
s = Squid(mass_kg=10, length_cm=20, age_s=30, depth_m=40, serial_num=50) |
|
|
|
s_dict = Squid.to_dict(s) |
|
|
|
s2 = Squid(s_dict) |
|
|
|
assert s == s2 |
|
|
|
|
|
class Clam(proto.Message): |
|
class Shell(proto.Message): |
|
class Pearl(proto.Message): |
|
mass_kg = proto.Field(proto.INT64, number=1) |
|
|
|
pearl = proto.Field(Pearl, number=1) |
|
|
|
shell = proto.Field(Shell, number=1) |
|
|
|
c = Clam(shell=Clam.Shell(pearl=Clam.Shell.Pearl(mass_kg=10))) |
|
|
|
c_dict = Clam.to_dict(c) |
|
|
|
c2 = Clam(c_dict) |
|
|
|
assert c == c2 |
|
|