Python google.protobuf.descriptor_pb2.MessageOptions() Examples

The following are 30 code examples of google.protobuf.descriptor_pb2.MessageOptions(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module google.protobuf.descriptor_pb2 , or try the search function .
Example #1
Source File: test_samplegen.py    From gapic-generator-python with Apache License 2.0 6 votes vote down vote up
def test_validate_expression_mapped_no_message():
    OutputType = DummyMessage(
        fields={
            "cephalopods": DummyField(
                message=DummyMessage(
                    fields={
                        "key": DummyField(),
                        # The value field needs a message.
                        "value": DummyField(),
                    },
                    type="CEPHALOPODS_TYPE",
                    options=namedtuple("MessageOptions", ["map_field"])(True)),
                repeated=True,
            )
        },
        type="MOLLUSC_TYPE"
    )
    method = DummyMethod(output=OutputType)
    v = samplegen.Validator(method)
    with pytest.raises(types.BadAttributeLookup):
        v.validate_expression('$resp.cephalopods{"squid"}.mantle') 
Example #2
Source File: test_samplegen.py    From gapic-generator-python with Apache License 2.0 6 votes vote down vote up
def test_validate_expression_mapped_no_value():
    OutputType = DummyMessage(
        fields={
            "cephalopods": DummyField(
                message=DummyMessage(
                    # Maps need 'key' AND 'value' attributes.
                    fields={"key": DummyField()},
                    type="CEPHALOPODS_TYPE",
                    options=namedtuple("MessageOptions", ["map_field"])(True)),
                repeated=True,
            )
        },
        type="MOLLUSC_TYPE"
    )
    method = DummyMethod(output=OutputType)
    v = samplegen.Validator(method)
    with pytest.raises(types.BadAttributeLookup):
        v.validate_expression('$resp.cephalopods{"squid"}.mantle') 
Example #3
Source File: test_samplegen.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def test_loop_map_reserved_key():
    loop = {
        "loop": {
            "map": "$resp.molluscs",
            # Can't use 'class' since it's a reserved keyword
            "key": "class",
            "value": "mollusc",
            "body": [{"print": ["A %s is a %s", "mollusc", "class"]}],
        }
    }
    OutputType = DummyMessage(
        fields={
            "molluscs": DummyField(
                message=DummyMessage(
                    fields={
                        "key": DummyField(),
                        "value": DummyField(
                            message=DummyMessage(
                                fields={},
                                type="MOLLUSC_TYPE"
                            )
                        )
                    },
                    type="MOLLUSCS_TYPE",
                    options=namedtuple("MessageOptions", ["map_field"])(True)
                ),
                repeated=True
            ),
        },
        type="RESPONSE_TYPE"
    )

    v = samplegen.Validator(DummyMethod(output=OutputType))
    with pytest.raises(types.ReservedVariableName):
        v.validate_response([loop]) 
Example #4
Source File: test_samplegen.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def test_map_loop_lexical_scope_inline():
    statements = [
        {
            "loop": {
                "map": "$resp.molluscs",
                "key": "cls",
                "value": "mollusc",
                "body": [{"define": "tmp=mollusc"}],
            }
        },
        # 'tmp' is outside the visible lexical scope according to strict
        # samplegen rules, even though it is valid python.
        {"define": "last_mollusc=tmp"},
    ]
    OutputType = DummyMessage(
        fields={
            "molluscs": DummyField(
                message=DummyMessage(
                    fields={
                        "key": DummyField(),
                        "value": DummyField(
                            message=DummyMessage(
                                fields={},
                                type="MOLLUSC_TYPE"
                            )
                        )
                    },
                    type="MOLLUSCS_TYPE",
                    options=namedtuple("MessageOptions", ["map_field"])(True)
                ),
                repeated=True
            ),
        },
        type="RESPONSE_TYPE"
    )
    v = samplegen.Validator(DummyMethod(output=OutputType))
    with pytest.raises(types.UndefinedVariableReference):
        v.validate_response(statements) 
Example #5
Source File: test_samplegen.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def test_loop_map_no_key():
    loop = {
        "loop": {
            "map": "$resp.molluscs",
            "value": "mollusc",
            "body": [{"print": ["Mollusc: %s", "mollusc"]}],
        }
    }
    OutputType = DummyMessage(
        fields={
            "molluscs": DummyField(
                message=DummyMessage(
                    fields={
                        "key": DummyField(),
                        "value": DummyField(
                            message=DummyMessage(
                                fields={},
                                type="CLASS_TYPE"
                            )
                        )
                    },
                    type="MOLLUSCS_TYPE",
                    options=namedtuple("MessageOptions", ["map_field"])(True)
                ),
                repeated=True
            ),
        },
        type="RESPONSE_TYPE"
    )

    v = samplegen.Validator(DummyMethod(output=OutputType))
    v.validate_response([loop]) 
Example #6
Source File: test_samplegen.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def test_loop_map_no_value():
    loop = {
        "loop": {
            "map": "$resp.molluscs",
            "key": "mollusc",
            "body": [{"print": ["Mollusc: %s", "mollusc"]}],
        }
    }
    OutputType = DummyMessage(
        fields={
            "molluscs": DummyField(
                message=DummyMessage(
                    fields={
                        "key": DummyField(),
                        "value": DummyField(
                            message=DummyMessage(
                                fields={},
                                type="CLASS_TYPE"
                            )
                        )
                    },
                    type="MOLLUSCS_TYPE",
                    options=namedtuple("MessageOptions", ["map_field"])(True)
                ),
                repeated=True
            ),
        },
        type="RESPONSE_TYPE"
    )

    v = samplegen.Validator(DummyMethod(output=OutputType))
    v.validate_response([loop]) 
Example #7
Source File: test_samplegen.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def test_loop_map_no_key_or_value():
    loop = {"loop": {"map": "$resp.molluscs",
                     # Need at least one of 'key' or 'value'
                     "body": [{"print": ["Dead loop"]}]}}
    OutputType = DummyMessage(
        fields={
            "molluscs": DummyField(
                message=DummyMessage(
                    fields={
                        "key": DummyField(),
                        "value": DummyField(
                            message=DummyMessage(
                                fields={},
                                type="CLASS_TYPE"
                            )
                        )
                    },
                    type="MOLLUSCS_TYPE",
                    options=namedtuple("MessageOptions", ["map_field"])(True)
                ),
                repeated=True
            ),
        },
        type="RESPONSE_TYPE"
    )

    v = samplegen.Validator(DummyMethod(output=OutputType))
    with pytest.raises(types.BadLoop):
        v.validate_response([loop]) 
Example #8
Source File: test_samplegen.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def test_loop_map_redefined_key():
    statements = [
        {"define": "mollusc=$resp.molluscs"},
        {
            "loop": {
                "map": "$resp.molluscs",
                # Can't redefine mollusc, which was defined one statement above.
                "key": "mollusc",
                "body": [{"print": ["Mollusc: %s", "mollusc"]}],
            }
        },
    ]
    OutputType = DummyMessage(
        fields={
            "molluscs": DummyField(
                message=DummyMessage(
                    fields={
                        "key": DummyField(),
                        "value": DummyField(
                            message=DummyMessage(
                                fields={},
                                type="CLASS_TYPE"
                            )
                        )
                    },
                    type="MOLLUSCS_TYPE",
                    options=namedtuple("MessageOptions", ["map_field"])(True)
                ),
                repeated=True
            ),
        },
        type="RESPONSE_TYPE"
    )

    v = samplegen.Validator(DummyMethod(output=OutputType))
    with pytest.raises(types.RedefinedVariable):
        v.validate_response(statements) 
Example #9
Source File: test_samplegen.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def test_loop_map_redefined_value():
    statements = [
        {"define": "mollusc=$resp.molluscs"},
        {
            "loop": {
                "map": "$resp.molluscs",
                # Can't redefine 'mollusc', which was defined one statement above.
                "value": "mollusc",
                "body": [{"print": ["Mollusc: %s", "mollusc"]}],
            }
        },
    ]
    OutputType = DummyMessage(
        fields={
            "molluscs": DummyField(
                message=DummyMessage(
                    fields={
                        "key": DummyField(),
                        "value": DummyField(
                            message=DummyMessage(
                                fields={},
                                type="CLASS_TYPE"
                            )
                        )
                    },
                    type="MOLLUSCS_TYPE",
                    options=namedtuple("MessageOptions", ["map_field"])(True)
                ),
                repeated=True
            ),
        },
        type="RESPONSE_TYPE"
    )

    v = samplegen.Validator(DummyMethod(output=OutputType))
    with pytest.raises(types.RedefinedVariable):
        v.validate_response(statements) 
Example #10
Source File: test_samplegen.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def test_validate_expression_map_lookup_terminal_lookup():
    OutputType = DummyMessage(
        fields={
            "cephalopods": DummyField(
                message=DummyMessage(
                    fields={
                        "key": DummyField(),
                        "value": DummyField(
                            message=DummyMessage(
                                fields={
                                    "mantle": DummyField(
                                        message=DummyMessage(type="MANTLE_TYPE",
                                                             fields={}),
                                    )
                                },
                                type="CEPHALOPOD_TYPE"
                            )
                        ),
                    },
                    type="CEPHALOPODS_TYPE",
                    options=namedtuple("MessageOptions", ["map_field"])(True)),
                repeated=True,
            )
        },
        type="MOLLUSC_TYPE"
    )
    method = DummyMethod(output=OutputType)
    v = samplegen.Validator(method)
    v.validate_expression('$resp.cephalopods{"squid"}') 
Example #11
Source File: test_samplegen.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def test_validate_expression_mapped_no_map_field():
    OutputType = DummyMessage(
        fields={
            "cephalopods": DummyField(
                message=DummyMessage(
                    fields={
                        "key": DummyField(),
                        "value": DummyField(
                            message=DummyMessage(
                                fields={
                                    "mantle": DummyField(
                                        message=DummyMessage(type="MANTLE_TYPE",
                                                             fields={}),
                                    )
                                },
                                type="CEPHALOPOD_TYPE"
                            )
                        )},
                    type="CEPHALOPODS_TYPE",
                    # The map_field attribute in the options indicates whether
                    # a message type is 'really' a map or just looks like one.
                    options=namedtuple("MessageOptions", ["map_field"])(False)),
                repeated=True,
            )
        },
        type="MOLLUSC_TYPE"
    )
    method = DummyMethod(output=OutputType)
    v = samplegen.Validator(method)
    with pytest.raises(types.BadAttributeLookup):
        v.validate_expression('$resp.cephalopods{"squid"}.mantle') 
Example #12
Source File: test_samplegen.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def test_validate_request_no_such_pattern():
    request = [
        # Note that there's only the one attribute, 'phylum', and that the only
        # pattern expects both 'kingdom' and 'phylum'.
        {"field": "taxon%phylum", "value": "mollusca", "input_parameter": "phylum"}
    ]

    resource_type = "taxonomy.google.com/Linnaean"
    taxon_field = make_field(name="taxon")
    rr = taxon_field.options.Extensions[resource_pb2.resource_reference]
    rr.type = resource_type
    request_descriptor = make_message(name="Request", fields=[taxon_field])

    phylum_options = descriptor_pb2.MessageOptions()
    resource = phylum_options.Extensions[resource_pb2.resource]
    resource.type = resource_type
    resource.pattern.append("kingdom/{kingdom}/phylum/{phylum}")
    phylum_descriptor = make_message(name="Phylum", options=phylum_options)

    method = DummyMethod(input=request_descriptor)
    api_schema = DummyApiSchema(
        messages={
            k: v
            for k, v in enumerate([
                request_descriptor,
                phylum_descriptor,
            ])
        }
    )

    v = samplegen.Validator(method=method, api_schema=api_schema)
    with pytest.raises(types.NoSuchResourcePattern):
        v.validate_and_transform_request(types.CallingForm.Request, request) 
Example #13
Source File: test_message.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def test_resource_path():
    options = descriptor_pb2.MessageOptions()
    resource = options.Extensions[resource_pb2.resource]
    resource.pattern.append(
        "kingdoms/{kingdom}/phyla/{phylum}/classes/{klass}")
    resource.pattern.append(
        "kingdoms/{kingdom}/divisions/{division}/classes/{klass}")
    resource.type = "taxonomy.biology.com/Class"
    message = make_message('Squid', options=options)

    assert message.resource_path == "kingdoms/{kingdom}/phyla/{phylum}/classes/{klass}"
    assert message.resource_path_args == ["kingdom", "phylum", "klass"]
    assert message.resource_type == "Class" 
Example #14
Source File: test_message.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def test_parse_resource_path():
    options = descriptor_pb2.MessageOptions()
    resource = options.Extensions[resource_pb2.resource]
    resource.pattern.append(
        "kingdoms/{kingdom}/phyla/{phylum}/classes/{klass}"
    )
    resource.type = "taxonomy.biology.com/Klass"
    message = make_message('Klass', options=options)

    # Plausible resource ID path
    path = "kingdoms/animalia/phyla/mollusca/classes/cephalopoda"
    expected = {
        'kingdom': 'animalia',
        'phylum': 'mollusca',
        'klass': 'cephalopoda',
    }
    actual = re.match(message.path_regex_str, path).groupdict()

    assert expected == actual

    options2 = descriptor_pb2.MessageOptions()
    resource2 = options2.Extensions[resource_pb2.resource]
    resource2.pattern.append(
        "kingdoms-{kingdom}_{phylum}#classes%{klass}"
    )
    resource2.type = "taxonomy.biology.com/Klass"
    message2 = make_message('Klass', options=options2)

    # Plausible resource ID path from a non-standard schema
    path2 = "kingdoms-Animalia/_Mollusca~#classes%Cephalopoda"
    expected2 = {
        'kingdom': 'Animalia/',
        'phylum': 'Mollusca~',
        'klass': 'Cephalopoda',
    }
    actual2 = re.match(message2.path_regex_str, path2).groupdict()

    assert expected2 == actual2 
Example #15
Source File: test_message.py    From gapic-generator-python with Apache License 2.0 5 votes vote down vote up
def test_field_map():
    # Create an Entry message.
    entry_msg = make_message(
        name='FooEntry',
        fields=(
            make_field(name='key', type=9),
            make_field(name='value', type=9),
        ),
        options=descriptor_pb2.MessageOptions(map_entry=True),
    )
    entry_field = make_field('foos', message=entry_msg, repeated=True)
    assert entry_msg.map
    assert entry_field.map 
Example #16
Source File: reflection_test.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def testHandWrittenReflection(self):
    # Hand written extensions are only supported by the pure-Python
    # implementation of the API.
    if api_implementation.Type() != 'python':
      return

    FieldDescriptor = descriptor.FieldDescriptor
    foo_field_descriptor = FieldDescriptor(
        name='foo_field', full_name='MyProto.foo_field',
        index=0, number=1, type=FieldDescriptor.TYPE_INT64,
        cpp_type=FieldDescriptor.CPPTYPE_INT64,
        label=FieldDescriptor.LABEL_OPTIONAL, default_value=0,
        containing_type=None, message_type=None, enum_type=None,
        is_extension=False, extension_scope=None,
        options=descriptor_pb2.FieldOptions())
    mydescriptor = descriptor.Descriptor(
        name='MyProto', full_name='MyProto', filename='ignored',
        containing_type=None, nested_types=[], enum_types=[],
        fields=[foo_field_descriptor], extensions=[],
        options=descriptor_pb2.MessageOptions())
    class MyProtoClass(six.with_metaclass(reflection.GeneratedProtocolMessageType, message.Message)):
      DESCRIPTOR = mydescriptor
    myproto_instance = MyProtoClass()
    self.assertEqual(0, myproto_instance.foo_field)
    self.assertTrue(not myproto_instance.HasField('foo_field'))
    myproto_instance.foo_field = 23
    self.assertEqual(23, myproto_instance.foo_field)
    self.assertTrue(myproto_instance.HasField('foo_field')) 
Example #17
Source File: descriptor_test.py    From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License 5 votes vote down vote up
def testGetOptions(self):
    self.assertEqual(self.my_enum.GetOptions(),
                     descriptor_pb2.EnumOptions())
    self.assertEqual(self.my_enum.values[0].GetOptions(),
                     descriptor_pb2.EnumValueOptions())
    self.assertEqual(self.my_message.GetOptions(),
                     descriptor_pb2.MessageOptions())
    self.assertEqual(self.my_message.fields[0].GetOptions(),
                     descriptor_pb2.FieldOptions())
    self.assertEqual(self.my_method.GetOptions(),
                     descriptor_pb2.MethodOptions())
    self.assertEqual(self.my_service.GetOptions(),
                     descriptor_pb2.ServiceOptions()) 
Example #18
Source File: reflection_test.py    From keras-lambda with MIT License 5 votes vote down vote up
def testHandWrittenReflection(self):
    # Hand written extensions are only supported by the pure-Python
    # implementation of the API.
    if api_implementation.Type() != 'python':
      return

    FieldDescriptor = descriptor.FieldDescriptor
    foo_field_descriptor = FieldDescriptor(
        name='foo_field', full_name='MyProto.foo_field',
        index=0, number=1, type=FieldDescriptor.TYPE_INT64,
        cpp_type=FieldDescriptor.CPPTYPE_INT64,
        label=FieldDescriptor.LABEL_OPTIONAL, default_value=0,
        containing_type=None, message_type=None, enum_type=None,
        is_extension=False, extension_scope=None,
        options=descriptor_pb2.FieldOptions())
    mydescriptor = descriptor.Descriptor(
        name='MyProto', full_name='MyProto', filename='ignored',
        containing_type=None, nested_types=[], enum_types=[],
        fields=[foo_field_descriptor], extensions=[],
        options=descriptor_pb2.MessageOptions())
    class MyProtoClass(six.with_metaclass(reflection.GeneratedProtocolMessageType, message.Message)):
      DESCRIPTOR = mydescriptor
    myproto_instance = MyProtoClass()
    self.assertEqual(0, myproto_instance.foo_field)
    self.assertTrue(not myproto_instance.HasField('foo_field'))
    myproto_instance.foo_field = 23
    self.assertEqual(23, myproto_instance.foo_field)
    self.assertTrue(myproto_instance.HasField('foo_field')) 
Example #19
Source File: descriptor_test.py    From keras-lambda with MIT License 5 votes vote down vote up
def testGetOptions(self):
    self.assertEqual(self.my_enum.GetOptions(),
                     descriptor_pb2.EnumOptions())
    self.assertEqual(self.my_enum.values[0].GetOptions(),
                     descriptor_pb2.EnumValueOptions())
    self.assertEqual(self.my_message.GetOptions(),
                     descriptor_pb2.MessageOptions())
    self.assertEqual(self.my_message.fields[0].GetOptions(),
                     descriptor_pb2.FieldOptions())
    self.assertEqual(self.my_method.GetOptions(),
                     descriptor_pb2.MethodOptions())
    self.assertEqual(self.my_service.GetOptions(),
                     descriptor_pb2.ServiceOptions()) 
Example #20
Source File: reflection_test.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def testHandWrittenReflection(self):
    # Hand written extensions are only supported by the pure-Python
    # implementation of the API.
    if api_implementation.Type() != 'python':
      return

    FieldDescriptor = descriptor.FieldDescriptor
    foo_field_descriptor = FieldDescriptor(
        name='foo_field', full_name='MyProto.foo_field',
        index=0, number=1, type=FieldDescriptor.TYPE_INT64,
        cpp_type=FieldDescriptor.CPPTYPE_INT64,
        label=FieldDescriptor.LABEL_OPTIONAL, default_value=0,
        containing_type=None, message_type=None, enum_type=None,
        is_extension=False, extension_scope=None,
        options=descriptor_pb2.FieldOptions())
    mydescriptor = descriptor.Descriptor(
        name='MyProto', full_name='MyProto', filename='ignored',
        containing_type=None, nested_types=[], enum_types=[],
        fields=[foo_field_descriptor], extensions=[],
        options=descriptor_pb2.MessageOptions())
    class MyProtoClass(six.with_metaclass(reflection.GeneratedProtocolMessageType, message.Message)):
      DESCRIPTOR = mydescriptor
    myproto_instance = MyProtoClass()
    self.assertEqual(0, myproto_instance.foo_field)
    self.assertTrue(not myproto_instance.HasField('foo_field'))
    myproto_instance.foo_field = 23
    self.assertEqual(23, myproto_instance.foo_field)
    self.assertTrue(myproto_instance.HasField('foo_field')) 
Example #21
Source File: descriptor_test.py    From lambda-packs with MIT License 5 votes vote down vote up
def testGetOptions(self):
    self.assertEqual(self.my_enum.GetOptions(),
                     descriptor_pb2.EnumOptions())
    self.assertEqual(self.my_enum.values[0].GetOptions(),
                     descriptor_pb2.EnumValueOptions())
    self.assertEqual(self.my_message.GetOptions(),
                     descriptor_pb2.MessageOptions())
    self.assertEqual(self.my_message.fields[0].GetOptions(),
                     descriptor_pb2.FieldOptions())
    self.assertEqual(self.my_method.GetOptions(),
                     descriptor_pb2.MethodOptions())
    self.assertEqual(self.my_service.GetOptions(),
                     descriptor_pb2.ServiceOptions()) 
Example #22
Source File: reflection_test.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def testHandWrittenReflection(self):
    # Hand written extensions are only supported by the pure-Python
    # implementation of the API.
    if api_implementation.Type() != 'python':
      return

    FieldDescriptor = descriptor.FieldDescriptor
    foo_field_descriptor = FieldDescriptor(
        name='foo_field', full_name='MyProto.foo_field',
        index=0, number=1, type=FieldDescriptor.TYPE_INT64,
        cpp_type=FieldDescriptor.CPPTYPE_INT64,
        label=FieldDescriptor.LABEL_OPTIONAL, default_value=0,
        containing_type=None, message_type=None, enum_type=None,
        is_extension=False, extension_scope=None,
        options=descriptor_pb2.FieldOptions())
    mydescriptor = descriptor.Descriptor(
        name='MyProto', full_name='MyProto', filename='ignored',
        containing_type=None, nested_types=[], enum_types=[],
        fields=[foo_field_descriptor], extensions=[],
        options=descriptor_pb2.MessageOptions())
    class MyProtoClass(six.with_metaclass(reflection.GeneratedProtocolMessageType, message.Message)):
      DESCRIPTOR = mydescriptor
    myproto_instance = MyProtoClass()
    self.assertEqual(0, myproto_instance.foo_field)
    self.assertTrue(not myproto_instance.HasField('foo_field'))
    myproto_instance.foo_field = 23
    self.assertEqual(23, myproto_instance.foo_field)
    self.assertTrue(myproto_instance.HasField('foo_field')) 
Example #23
Source File: descriptor_test.py    From auto-alt-text-lambda-api with MIT License 5 votes vote down vote up
def testGetOptions(self):
    self.assertEqual(self.my_enum.GetOptions(),
                     descriptor_pb2.EnumOptions())
    self.assertEqual(self.my_enum.values[0].GetOptions(),
                     descriptor_pb2.EnumValueOptions())
    self.assertEqual(self.my_message.GetOptions(),
                     descriptor_pb2.MessageOptions())
    self.assertEqual(self.my_message.fields[0].GetOptions(),
                     descriptor_pb2.FieldOptions())
    self.assertEqual(self.my_method.GetOptions(),
                     descriptor_pb2.MethodOptions())
    self.assertEqual(self.my_service.GetOptions(),
                     descriptor_pb2.ServiceOptions()) 
Example #24
Source File: reflection_test.py    From sklearn-theano with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def testHandWrittenReflection(self):
    # Hand written extensions are only supported by the pure-Python
    # implementation of the API.
    if api_implementation.Type() != 'python':
      return

    FieldDescriptor = descriptor.FieldDescriptor
    foo_field_descriptor = FieldDescriptor(
        name='foo_field', full_name='MyProto.foo_field',
        index=0, number=1, type=FieldDescriptor.TYPE_INT64,
        cpp_type=FieldDescriptor.CPPTYPE_INT64,
        label=FieldDescriptor.LABEL_OPTIONAL, default_value=0,
        containing_type=None, message_type=None, enum_type=None,
        is_extension=False, extension_scope=None,
        options=descriptor_pb2.FieldOptions())
    mydescriptor = descriptor.Descriptor(
        name='MyProto', full_name='MyProto', filename='ignored',
        containing_type=None, nested_types=[], enum_types=[],
        fields=[foo_field_descriptor], extensions=[],
        options=descriptor_pb2.MessageOptions())
    class MyProtoClass(six.with_metaclass(reflection.GeneratedProtocolMessageType, message.Message)):
      DESCRIPTOR = mydescriptor
    myproto_instance = MyProtoClass()
    self.assertEqual(0, myproto_instance.foo_field)
    self.assertTrue(not myproto_instance.HasField('foo_field'))
    myproto_instance.foo_field = 23
    self.assertEqual(23, myproto_instance.foo_field)
    self.assertTrue(myproto_instance.HasField('foo_field')) 
Example #25
Source File: descriptor_test.py    From sklearn-theano with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def testGetOptions(self):
    self.assertEqual(self.my_enum.GetOptions(),
                     descriptor_pb2.EnumOptions())
    self.assertEqual(self.my_enum.values[0].GetOptions(),
                     descriptor_pb2.EnumValueOptions())
    self.assertEqual(self.my_message.GetOptions(),
                     descriptor_pb2.MessageOptions())
    self.assertEqual(self.my_message.fields[0].GetOptions(),
                     descriptor_pb2.FieldOptions())
    self.assertEqual(self.my_method.GetOptions(),
                     descriptor_pb2.MethodOptions())
    self.assertEqual(self.my_service.GetOptions(),
                     descriptor_pb2.ServiceOptions()) 
Example #26
Source File: reflection_test.py    From botchallenge with MIT License 5 votes vote down vote up
def testHandWrittenReflection(self):
    # Hand written extensions are only supported by the pure-Python
    # implementation of the API.
    if api_implementation.Type() != 'python':
      return

    FieldDescriptor = descriptor.FieldDescriptor
    foo_field_descriptor = FieldDescriptor(
        name='foo_field', full_name='MyProto.foo_field',
        index=0, number=1, type=FieldDescriptor.TYPE_INT64,
        cpp_type=FieldDescriptor.CPPTYPE_INT64,
        label=FieldDescriptor.LABEL_OPTIONAL, default_value=0,
        containing_type=None, message_type=None, enum_type=None,
        is_extension=False, extension_scope=None,
        options=descriptor_pb2.FieldOptions())
    mydescriptor = descriptor.Descriptor(
        name='MyProto', full_name='MyProto', filename='ignored',
        containing_type=None, nested_types=[], enum_types=[],
        fields=[foo_field_descriptor], extensions=[],
        options=descriptor_pb2.MessageOptions())
    class MyProtoClass(message.Message, metaclass=reflection.GeneratedProtocolMessageType):
      DESCRIPTOR = mydescriptor
    myproto_instance = MyProtoClass()
    self.assertEqual(0, myproto_instance.foo_field)
    self.assertTrue(not myproto_instance.HasField('foo_field'))
    myproto_instance.foo_field = 23
    self.assertEqual(23, myproto_instance.foo_field)
    self.assertTrue(myproto_instance.HasField('foo_field')) 
Example #27
Source File: descriptor_test.py    From botchallenge with MIT License 5 votes vote down vote up
def testGetOptions(self):
    self.assertEqual(self.my_enum.GetOptions(),
                     descriptor_pb2.EnumOptions())
    self.assertEqual(self.my_enum.values[0].GetOptions(),
                     descriptor_pb2.EnumValueOptions())
    self.assertEqual(self.my_message.GetOptions(),
                     descriptor_pb2.MessageOptions())
    self.assertEqual(self.my_message.fields[0].GetOptions(),
                     descriptor_pb2.FieldOptions())
    self.assertEqual(self.my_method.GetOptions(),
                     descriptor_pb2.MethodOptions())
    self.assertEqual(self.my_service.GetOptions(),
                     descriptor_pb2.ServiceOptions()) 
Example #28
Source File: reflection_test.py    From lambda-packs with MIT License 5 votes vote down vote up
def testHandWrittenReflection(self):
    # Hand written extensions are only supported by the pure-Python
    # implementation of the API.
    if api_implementation.Type() != 'python':
      return

    FieldDescriptor = descriptor.FieldDescriptor
    foo_field_descriptor = FieldDescriptor(
        name='foo_field', full_name='MyProto.foo_field',
        index=0, number=1, type=FieldDescriptor.TYPE_INT64,
        cpp_type=FieldDescriptor.CPPTYPE_INT64,
        label=FieldDescriptor.LABEL_OPTIONAL, default_value=0,
        containing_type=None, message_type=None, enum_type=None,
        is_extension=False, extension_scope=None,
        options=descriptor_pb2.FieldOptions())
    mydescriptor = descriptor.Descriptor(
        name='MyProto', full_name='MyProto', filename='ignored',
        containing_type=None, nested_types=[], enum_types=[],
        fields=[foo_field_descriptor], extensions=[],
        options=descriptor_pb2.MessageOptions())
    class MyProtoClass(six.with_metaclass(reflection.GeneratedProtocolMessageType, message.Message)):
      DESCRIPTOR = mydescriptor
    myproto_instance = MyProtoClass()
    self.assertEqual(0, myproto_instance.foo_field)
    self.assertTrue(not myproto_instance.HasField('foo_field'))
    myproto_instance.foo_field = 23
    self.assertEqual(23, myproto_instance.foo_field)
    self.assertTrue(myproto_instance.HasField('foo_field')) 
Example #29
Source File: descriptor_test.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def testGetOptions(self):
    self.assertEqual(self.my_enum.GetOptions(),
                     descriptor_pb2.EnumOptions())
    self.assertEqual(self.my_enum.values[0].GetOptions(),
                     descriptor_pb2.EnumValueOptions())
    self.assertEqual(self.my_message.GetOptions(),
                     descriptor_pb2.MessageOptions())
    self.assertEqual(self.my_message.fields[0].GetOptions(),
                     descriptor_pb2.FieldOptions())
    self.assertEqual(self.my_method.GetOptions(),
                     descriptor_pb2.MethodOptions())
    self.assertEqual(self.my_service.GetOptions(),
                     descriptor_pb2.ServiceOptions()) 
Example #30
Source File: descriptor_test.py    From coremltools with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def testGetOptions(self):
    self.assertEqual(self.my_enum.GetOptions(),
                     descriptor_pb2.EnumOptions())
    self.assertEqual(self.my_enum.values[0].GetOptions(),
                     descriptor_pb2.EnumValueOptions())
    self.assertEqual(self.my_message.GetOptions(),
                     descriptor_pb2.MessageOptions())
    self.assertEqual(self.my_message.fields[0].GetOptions(),
                     descriptor_pb2.FieldOptions())
    self.assertEqual(self.my_method.GetOptions(),
                     descriptor_pb2.MethodOptions())
    self.assertEqual(self.my_service.GetOptions(),
                     descriptor_pb2.ServiceOptions())