def test_has_field_proto3() -> None:
    s = SimpleProto3()
    assert not s.HasField("outer_message")
    assert not s.HasField("a_oneof")

    assert not s.HasField("an_optional_string")
    # synthetic oneof from optional field, see https://github.com/protocolbuffers/protobuf/blob/v3.12.0/docs/implementing_proto3_presence.md#updating-a-code-generator
    assert not s.HasField("_an_optional_string")

    s = SimpleProto3(an_optional_string=None)
    assert not s.HasField("an_optional_string")
    # synthetic oneof from optional field
    assert not s.HasField("_an_optional_string")

    s = SimpleProto3(an_optional_string="")
    assert s.HasField("an_optional_string")
    # synthetic oneof from optional field
    assert s.HasField("_an_optional_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
    if CPP_IMPL:
        s_untyped: Any = s
        with pytest.raises(
                ValueError,
                match="Protocol message SimpleProto3 has no field garbage."):
            s_untyped.HasField("garbage")
        with pytest.raises(
                ValueError,
                match=
                'Can\'t test non-optional, non-submessage field "SimpleProto3.a_string" for presence in proto3.',
        ):
            s_untyped.HasField("a_string")
        with pytest.raises(
                ValueError,
                match=
                'Can\'t test non-optional, non-submessage field "SimpleProto3.a_outer_enum" for presence in proto3.',
        ):
            s_untyped.HasField("a_outer_enum")
        with pytest.raises(
                ValueError,
                match=
                'Protocol message SimpleProto3 has no singular "a_repeated_string" field',
        ):
            s_untyped.HasField("a_repeated_string")
        with pytest.raises(TypeError,
                           match="bad argument type for built-in operation"):
            s_untyped.HasField(b"outer_message")

        none_err = "bad argument type for built-in operation"
        with pytest.raises(TypeError, match=none_err):
            s_untyped.HasField(None)
def test_mapping_type() -> None:
    s = SimpleProto3()
    s.map_scalar[5] = "abcd"
    assert s.map_scalar[5] == "abcd"

    s.map_message[5].a_string = "hi"
    assert s.map_message[5] == OuterMessage3(a_string="hi")

    assert s.map_message.get_or_create(6) == OuterMessage3()
    assert s.map_message[6] == OuterMessage3()
    assert s.map_message.get_or_create(6) == OuterMessage3()

    s2 = SimpleProto3(map_scalar={5: "abcd"},
                      map_message={5: OuterMessage3(a_string="hi")})
Beispiel #3
0
def test_mapping_type():
    # type: () -> None
    s = SimpleProto3()
    s.map_scalar[5] = "abcd"
    assert s.map_scalar[5] == "abcd"

    s.map_message[5].a_bool = True
    assert s.map_message[5] == OuterMessage3(a_bool=True)

    assert s.map_message.get_or_create(6) == OuterMessage3()
    assert s.map_message[6] == OuterMessage3()
    assert s.map_message.get_or_create(6) == OuterMessage3()

    s2 = SimpleProto3(map_scalar={5: "abcd"},
                      map_message={5: OuterMessage3(a_bool=True)})
def test_constructor_proto3() -> None:
    x = SimpleProto3()
    assert x.a_string == ""
    assert not x.HasField("an_optional_string")

    # an_optional_string has optional keyword so None is allowed
    x = SimpleProto3(an_optional_string=None)
    assert not x.HasField("an_optional_string")

    x = SimpleProto3(a_string="", an_optional_string="")
    assert x.a_string == ""
    assert x.HasField("an_optional_string")

    x = SimpleProto3(a_string="hello", an_optional_string="hello")
    assert x.a_string == "hello"
    assert x.HasField("an_optional_string")
def test_which_oneof_proto3() -> None:
    s = SimpleProto3()

    assert s.WhichOneof("a_oneof") is None
    s.a_oneof_1 = "hello"
    s.b_oneof_1 = "world"
    assert s.WhichOneof("a_oneof") == "a_oneof_1"
    if CPP_IMPL:
        assert s.WhichOneof(b"a_oneof") == "a_oneof_1"
    assert type(s.WhichOneof("a_oneof")) == str

    field_a = s.WhichOneof("a_oneof")
    assert field_a is not None
    assert s.HasField(field_a)

    field_b = s.WhichOneof("b_oneof")
    assert field_b is not None
    assert s.HasField(field_b)

    # synthetic oneof from optional field
    assert s.WhichOneof("_an_optional_string") is None
    s.an_optional_string = "foo"
    field_aos = s.WhichOneof("_an_optional_string")
    assert field_aos is not None
    assert s.HasField(field_aos)

    # 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 oneof "garbage" field.'):
        s_untyped.WhichOneof("garbage")
Beispiel #6
0
def test_clear_field_proto3():
    # type: () -> None
    """For ClearField which is typed with Literal"""
    s = SimpleProto3()
    s.a_string = "Hello"

    # Proto2 tests
    s.ClearField(u"a_string")
    if six.PY2:
        s.ClearField(b"a_string")
    s.ClearField("a_string")
    s.ClearField("a_outer_enum")
    s.ClearField("outer_message")
    s.ClearField("a_repeated_string")
    s.ClearField("a_oneof")
    s.ClearField(b"a_string")
    s.ClearField("an_optional_string")
    # synthetic oneof from optional field
    s.ClearField("_an_optional_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 = s  # type: Any
    with pytest.raises(ValueError,
                       match='Protocol message has no "garbage" field.'):
        s_untyped.ClearField("garbage")
Beispiel #7
0
def test_which_oneof_proto3():
    # type: () -> None
    s = SimpleProto3()

    assert s.WhichOneof("a_oneof") is None
    s.a_oneof_1 = "hello"
    s.b_oneof_1 = "world"
    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"))
    assert s.HasField(s.WhichOneof("b_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")
Beispiel #8
0
def test_has_field_proto3():
    # type: () -> None
    s = SimpleProto3()
    assert not s.HasField(u"outer_message")
    assert not s.HasField("outer_message")
    if six.PY2:
        assert not s.HasField(b"outer_message")
    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 SimpleProto3 has no field garbage."):
        s_untyped.HasField(u"garbage")
    with pytest.raises(
            ValueError,
            match=
            'Can\'t test non-submessage field "SimpleProto3.a_string" for presence in proto3.',
    ):
        s_untyped.HasField(u"a_string")
    with pytest.raises(
            ValueError,
            match=
            'Can\'t test non-submessage field "SimpleProto3.a_outer_enum" for presence in proto3.',
    ):
        s_untyped.HasField("a_outer_enum")
    with pytest.raises(
            ValueError,
            match=
            'Protocol message SimpleProto3 has no singular "a_repeated_string" field',
    ):
        s_untyped.HasField(u"a_repeated_string")
    if six.PY3:
        with pytest.raises(TypeError,
                           match="bad argument type for built-in operation"):
            s_untyped.HasField(b"outer_message")
Beispiel #9
0
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

e = FOO
e = 3  # E:2.7 E:3.8

# Proto2
s.HasField("garbage")  # E:2.7 E:3.8
s.HasField("a_repeated_string")  # E:2.7 E:3.8

# Proto3
s6 = SimpleProto3()
s6.HasField(u"garbage")  # E:2.7 E:3.8
s6.HasField(u"a_string")  # E:2.7 E:3.8
s6.HasField(u"outer_enum")  # E:2.7 E:3.8
s6.HasField(u"a_repeated_string")  # E:2.7 E:3.8

# Proto2
s.ClearField("garbage")  # E:2.7 E:3.8

# Proto3
s6.ClearField("garbage")  # E:2.7 E:3.8

# Proto2 WhichOneof
s.WhichOneof("garbage")  # E:2.7 E:3.8
a = 5
a = s.WhichOneof("a_oneof")  # E:2.7 E:3.8
Beispiel #10
0
def test_message_descriptor_proto3():
    # type: () -> None
    assert SimpleProto3().DESCRIPTOR.full_name == "test3.SimpleProto3"
    assert SimpleProto3.DESCRIPTOR.full_name == "test3.SimpleProto3"
Beispiel #11
0
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

e = FOO
e = 3  # E:2.7 E:3.5

# Proto2
s.HasField("garbage")  # E:2.7 E:3.5
s.HasField("a_repeated_string")  # E:2.7 E:3.5

# Proto3
s6 = SimpleProto3()
s6.HasField(u"garbage")  # E:2.7 E:3.5
s6.HasField(u"a_string")  # E:2.7 E:3.5
s6.HasField(u"outer_enum")  # E:2.7 E:3.5
s6.HasField(u"a_repeated_string")  # E:2.7 E:3.5

# Proto2
s.ClearField("garbage")  # E:2.7 E:3.5

# Proto3
s6.ClearField("garbage")  # E:2.7 E:3.5

# Proto2 WhichOneof
s.WhichOneof("garbage")  # E:2.7 E:3.5
a = 5
a = s.WhichOneof("a_oneof")  # E:2.7 E:3.5