|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import base64 |
|
import pytest |
|
|
|
import proto |
|
|
|
|
|
def test_bytes_init(): |
|
class Foo(proto.Message): |
|
bar = proto.Field(proto.BYTES, number=1) |
|
baz = proto.Field(proto.BYTES, number=2) |
|
|
|
foo = Foo(bar=b"spam") |
|
assert foo.bar == b"spam" |
|
assert foo.baz == b"" |
|
assert not foo.baz |
|
assert Foo.pb(foo).bar == b"spam" |
|
assert Foo.pb(foo).baz == b"" |
|
|
|
|
|
def test_bytes_rmw(): |
|
class Foo(proto.Message): |
|
spam = proto.Field(proto.BYTES, number=1) |
|
eggs = proto.Field(proto.BYTES, number=2) |
|
|
|
foo = Foo(spam=b"bar") |
|
foo.eggs = b"baz" |
|
assert foo.spam == b"bar" |
|
assert foo.eggs == b"baz" |
|
assert Foo.pb(foo).spam == b"bar" |
|
assert Foo.pb(foo).eggs == b"baz" |
|
foo.spam = b"bacon" |
|
assert foo.spam == b"bacon" |
|
assert foo.eggs == b"baz" |
|
assert Foo.pb(foo).spam == b"bacon" |
|
assert Foo.pb(foo).eggs == b"baz" |
|
|
|
|
|
def test_bytes_del(): |
|
class Foo(proto.Message): |
|
bar = proto.Field(proto.BYTES, number=1) |
|
|
|
foo = Foo(bar=b"spam") |
|
assert foo.bar == b"spam" |
|
del foo.bar |
|
assert foo.bar == b"" |
|
assert not foo.bar |
|
|
|
|
|
def test_bytes_string_distinct(): |
|
class Foo(proto.Message): |
|
bar = proto.Field(proto.STRING, number=1) |
|
baz = proto.Field(proto.BYTES, number=2) |
|
|
|
foo = Foo() |
|
assert foo.bar != foo.baz |
|
|
|
|
|
|
|
foo.bar = b"anything" |
|
assert foo.bar == "anything" |
|
|
|
|
|
|
|
|
|
|
|
|
|
encoded_swallow: str = base64.urlsafe_b64encode(b"unladen swallow").decode("utf-8") |
|
assert type(encoded_swallow) == str |
|
foo.baz = encoded_swallow |
|
assert foo.baz == b"unladen swallow" |
|
|
|
|
|
def test_bytes_to_dict_bidi(): |
|
class Foo(proto.Message): |
|
bar = proto.Field(proto.BYTES, number=1) |
|
|
|
foo = Foo(bar=b"spam") |
|
|
|
foo_dict = Foo.to_dict(foo) |
|
foo_two = Foo(foo_dict) |
|
|
|
assert foo == foo_two |
|
|