Python google.protobuf.descriptor_pb2.MessageOptions() Examples

The following are 30 code examples for showing how to use google.protobuf.descriptor_pb2.MessageOptions(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

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
Project: gapic-generator-python   Author: googleapis   File: test_samplegen.py    License: 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 2
Project: gapic-generator-python   Author: googleapis   File: test_samplegen.py    License: 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 3
Project: lambda-packs   Author: ryfeus   File: reflection_test.py    License: 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 4
Project: lambda-packs   Author: ryfeus   File: descriptor_test.py    License: 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 5
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: reflection_test.py    License: 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 6
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: descriptor_test.py    License: 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 7
Project: sklearn-theano   Author: sklearn-theano   File: reflection_test.py    License: 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 8
Project: sklearn-theano   Author: sklearn-theano   File: descriptor_test.py    License: 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 9
Project: botchallenge   Author: katharosada   File: reflection_test.py    License: 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 10
Project: botchallenge   Author: katharosada   File: descriptor_test.py    License: 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 11
Project: coremltools   Author: apple   File: reflection_test.py    License: 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 12
Project: coremltools   Author: apple   File: descriptor_test.py    License: 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 13
Project: coremltools   Author: apple   File: descriptor_test.py    License: 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 14
Project: go2mapillary   Author: enricofer   File: reflection_test.py    License: GNU General Public License v3.0 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 15
Project: go2mapillary   Author: enricofer   File: descriptor_test.py    License: GNU General Public License v3.0 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 16
Project: myelin-acorn-electron-hardware   Author: google   File: nanopb_generator.py    License: Apache License 2.0 5 votes vote down vote up
def get_nanopb_suboptions(subdesc, options, name):
    '''Get copy of options, and merge information from subdesc.'''
    new_options = nanopb_pb2.NanoPBOptions()
    new_options.CopyFrom(options)

    if hasattr(subdesc, 'syntax') and subdesc.syntax == "proto3":
        new_options.proto3 = True

    # Handle options defined in a separate file
    dotname = '.'.join(name.parts)
    for namemask, options in Globals.separate_options:
        if fnmatch(dotname, namemask):
            Globals.matched_namemasks.add(namemask)
            new_options.MergeFrom(options)

    # Handle options defined in .proto
    if isinstance(subdesc.options, descriptor.FieldOptions):
        ext_type = nanopb_pb2.nanopb
    elif isinstance(subdesc.options, descriptor.FileOptions):
        ext_type = nanopb_pb2.nanopb_fileopt
    elif isinstance(subdesc.options, descriptor.MessageOptions):
        ext_type = nanopb_pb2.nanopb_msgopt
    elif isinstance(subdesc.options, descriptor.EnumOptions):
        ext_type = nanopb_pb2.nanopb_enumopt
    else:
        raise Exception("Unknown options type")

    if subdesc.options.HasExtension(ext_type):
        ext = subdesc.options.Extensions[ext_type]
        new_options.MergeFrom(ext)

    if Globals.verbose_options:
        sys.stderr.write("Options for " + dotname + ": ")
        sys.stderr.write(text_format.MessageToString(new_options) + "\n")

    return new_options


# ---------------------------------------------------------------------------
#                         Command line interface
# --------------------------------------------------------------------------- 
Example 17
Project: gapic-generator-python   Author: googleapis   File: test_samplegen.py    License: Apache License 2.0 5 votes vote down vote up
def test_loop_map():
    loop = {
        "loop": {
            "map": "$resp.molluscs",
            "key": "cls",
            "value": "mollusc",
            "body": [{"print": ["A %s is a %s", "mollusc", "cls"]}],
        }
    }
    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))
    v.validate_response([loop]) 
Example 18
Project: gapic-generator-python   Author: googleapis   File: test_samplegen.py    License: Apache License 2.0 5 votes vote down vote up
def test_map_loop_lexical_scope_key():
    statements = [
        {
            "loop": {
                "map": "$resp.molluscs",
                "key": "cls",
                "value": "mollusc",
                "body": [{"define": "tmp=cls"}],
            }
        },
        # 'cls' is outside the visible lexical scope according to strict
        # samplegen rules, even though it is valid python.
        {"define": "last_cls=cls"},
    ]
    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 19
Project: gapic-generator-python   Author: googleapis   File: test_samplegen.py    License: Apache License 2.0 5 votes vote down vote up
def test_map_loop_lexical_scope_value():
    statements = [
        {
            "loop": {
                "map": "$resp.molluscs",
                "key": "cls",
                "value": "mollusc",
                "body": [{"define": "tmp=mollusc"}],
            }
        },
        # 'mollusc' is outside the visible lexical scope according to strict
        # samplegen rules, even though it is valid python.
        {"define": "last_mollusc=mollusc"},
    ]
    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 20
Project: gapic-generator-python   Author: googleapis   File: test_samplegen.py    License: 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 21
Project: gapic-generator-python   Author: googleapis   File: test_samplegen.py    License: 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 22
Project: gapic-generator-python   Author: googleapis   File: test_samplegen.py    License: 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 23
Project: gapic-generator-python   Author: googleapis   File: test_samplegen.py    License: 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 24
Project: gapic-generator-python   Author: googleapis   File: test_samplegen.py    License: 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 25
Project: gapic-generator-python   Author: googleapis   File: test_samplegen.py    License: 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 26
Project: gapic-generator-python   Author: googleapis   File: test_samplegen.py    License: 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 27
Project: gapic-generator-python   Author: googleapis   File: test_samplegen.py    License: 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 28
Project: gapic-generator-python   Author: googleapis   File: test_samplegen.py    License: 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 29
Project: gapic-generator-python   Author: googleapis   File: test_samplegen.py    License: 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 30
Project: gapic-generator-python   Author: googleapis   File: test_message.py    License: 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"