Exemplo n.º 1
0
def test_casttype() -> None:
    s = Simple1()
    s.user_id = UserId(33)
    assert s.user_id == 33
    s.email = Email("*****@*****.**")
    assert s.email == "*****@*****.**"
    s.email_by_uid[UserId(33)] = Email("*****@*****.**")
Exemplo n.º 2
0
def test_has_field_proto2():
    # type: () -> None
    """For HasField which is typed with Literal"""
    s = Simple1()
    s.a_string = "Hello"

    # Proto2 tests
    assert s.HasField(u"a_string")
    assert s.HasField("a_string")
    if six.PY2:
        assert s.HasField(b"a_string")
    assert not s.HasField("a_inner")
    assert not s.HasField("a_enum")
    assert not s.HasField("a_oneof")

    # Erase the types to verify that incorrect inputs fail at runtime
    # Each test here should be duplicated in test_negative to ensure mypy fails it too
    s_untyped = s  # type: Any
    with pytest.raises(ValueError,
                       match="Protocol message Simple1 has no field garbage."):
        s_untyped.HasField("garbage")
    with pytest.raises(
            ValueError,
            match=
            'Protocol message Simple1 has no singular "a_repeated_string" field',
    ):
        s_untyped.HasField("a_repeated_string")
    if six.PY3:
        with pytest.raises(TypeError,
                           match="bad argument type for built-in operation"):
            s_untyped.HasField(b"a_string")
Exemplo n.º 3
0
def test_extensions_proto2():
    # type: () -> None
    s1 = Simple1()
    s2 = Simple2()

    assert isinstance(Extensions1.ext, FieldDescriptor)
    assert isinstance(Extensions2.foo, FieldDescriptor)
    assert isinstance(SeparateFileExtension.ext, FieldDescriptor)

    e1 = s1.Extensions[Extensions1.ext]
    e1.ext1_string = "first extension"
    assert isinstance(e1, Extensions1)

    e2 = s1.Extensions[Extensions2.foo]
    e2.flag = True
    assert isinstance(e2, Extensions2)

    e3 = s2.Extensions[SeparateFileExtension.ext]
    e3.flag = True
    assert isinstance(e3, SeparateFileExtension)

    del s1.Extensions[Extensions2.foo]

    # Using __iter__, x is a FieldDescriptor but the type of the message that
    # s1.Extensions[x] yields is unknown (it could be any of the extension messages).
    # Hence, s1.Extensions[x] is typed as Any.
    for x in s1.Extensions:
        assert isinstance(x, FieldDescriptor)
        assert x.is_extension
        y = s1.Extensions[x]
        assert y.ext1_string == "first extension"

    assert Extensions1.ext in s1.Extensions

    assert len(s2.Extensions) == 1
Exemplo n.º 4
0
def test_func():
    # type: () -> None
    s = Simple1(a_string="hello")

    s = Simple1()
    s.a_string = "Hello"
    s.a_repeated_string.append("Hello")

    s2 = Simple1.FromString(s.SerializeToString())
    assert s2.a_string == "Hello"

    s3 = Simple1()
    s3.ParseFromString(s.SerializeToString())
    assert s3.a_string == "Hello"

    s4 = Simple1()
    s4.CopyFrom(s)
    assert s4.a_string == "Hello"

    l = lower2()
    l.upper.Lower.a = 2
    assert l == lower2(upper=Upper(Lower=lower(a=2)))
Exemplo n.º 5
0
def test_func() -> None:
    s = Simple1(a_string="hello")

    s = Simple1()
    s.a_string = "Hello"
    s.a_repeated_string.append("Hello")

    s2 = Simple1.FromString(s.SerializeToString())
    assert s2.a_string == "Hello"
    assert s2.A_STRING_FIELD_NUMBER == 1
    assert s2.USER_ID_FIELD_NUMBER == 21

    s3 = Simple1()
    s3.ParseFromString(s.SerializeToString())
    assert s3.a_string == "Hello"

    s4 = Simple1()
    s4.CopyFrom(s)
    assert s4.a_string == "Hello"

    l = lower2()
    l.upper.Lower.a = 2
    assert l == lower2(upper=Upper(Lower=lower(a=2)))
Exemplo n.º 6
0
def test_which_oneof_proto2():
    # type: () -> None
    s = Simple1()

    assert s.WhichOneof("a_oneof") is None
    s.a_oneof_1 = "hello"
    assert s.WhichOneof("a_oneof") == "a_oneof_1"
    assert s.WhichOneof(u"a_oneof") == "a_oneof_1"
    assert s.WhichOneof(b"a_oneof") == "a_oneof_1"
    assert type(s.WhichOneof("a_oneof")) == str
    assert s.HasField(s.WhichOneof("a_oneof"))

    # Erase the types to verify that incorrect inputs fail at runtime
    # Each test here should be duplicated in test_negative to ensure mypy fails it too
    s_untyped = s  # type: Any
    with pytest.raises(ValueError,
                       match='Protocol message has no oneof "garbage" field.'):
        s_untyped.WhichOneof("garbage")
Exemplo n.º 7
0
def test_clear_field_proto2() -> None:
    """For ClearField which is typed with Literal"""
    s = Simple1()
    s.a_string = "Hello"

    # Proto2 tests
    s.ClearField("a_string")
    s.ClearField("a_inner")
    s.ClearField("a_repeated_string")
    s.ClearField("a_oneof")
    if CPP_IMPL:
        s.ClearField(b"a_string")

        # Erase the types to verify that incorrect inputs fail at runtime
        # Each test here should be duplicated in test_negative to ensure mypy fails it too
        s_untyped: Any = s
        with pytest.raises(ValueError,
                           match='Protocol message has no "garbage" field.'):
            s_untyped.ClearField("garbage")
Exemplo n.º 8
0
)

from testproto.test_extensions2_pb2 import SeparateFileExtension
from testproto.test_pb2 import (
    DESCRIPTOR,
    Extensions1,
    Extensions2,
    FOO,
    Simple1,
    Simple2,
)
from testproto.test3_pb2 import OuterEnum, OuterMessage3, SimpleProto3
from testproto.dot.com.test_pb2 import TestMessage
from test.test_generated_mypy import Email, UserId

s = Simple1()
s.a_string = "Hello"

s2 = Simple1.FromString(s.SerializeToStringg())  # E:2.7 E:3.8

s3 = Simple1()
s3.ParseFromString(s)  # E:2.7 E:3.8

s4 = Simple1()
s4.CopyFrom(s.SerializeToString())  # E:2.7 E:3.8

s5 = Simple1()
l = []  # type: List[int]
l.extend(s5.a_repeated_string)  # E:2.7 E:3.8

tm = TestMessage(foo=55)  # E:2.7 E:3.8
Exemplo n.º 9
0
def test_message_descriptor_proto2():
    # type: () -> None
    assert Simple1().DESCRIPTOR.full_name == "test.Simple1"
    assert Simple1.DESCRIPTOR.full_name == "test.Simple1"
Exemplo n.º 10
0
    Text,
)

from testproto.test_extensions2_pb2 import SeparateFileExtension
from testproto.test_pb2 import (
    DESCRIPTOR,
    Extensions1,
    Extensions2,
    FOO,
    Simple1,
    Simple2,
)
from testproto.test3_pb2 import OuterEnum, OuterEnumValue, SimpleProto3
from testproto.dot.com.test_pb2 import TestMessage

s = Simple1()
s.a_string = "Hello"

s2 = Simple1.FromString(s.SerializeToStringg())  # E:2.7 E:3.5

s3 = Simple1()
s3.ParseFromString(s)  # E:2.7 E:3.5

s4 = Simple1()
s4.CopyFrom(s.SerializeToString())  # E:2.7 E:3.5

s5 = Simple1()
l = []  # type: List[int]
l.extend(s5.a_repeated_string)  # E:2.7 E:3.5

tm = TestMessage(foo=55)  # E:2.7 E:3.5