Example #1
0
def test_method_paged_result_field_no_page_field():
    input_msg = make_message(
        name='ListFoosRequest',
        fields=(
            make_field(name='parent', type=9),  # str
            make_field(name='page_size', type=5),  # int
            make_field(name='page_token', type=9),  # str
        ))
    output_msg = make_message(
        name='ListFoosResponse',
        fields=(
            make_field(name='foos',
                       message=make_message('Foo'),
                       repeated=False),
            make_field(name='next_page_token', type=9),  # str
        ))
    method = make_method(
        'ListFoos',
        input_message=input_msg,
        output_message=output_msg,
    )
    assert method.paged_result_field is None

    method = make_method(
        name='Foo',
        input_message=make_message(
            name='FooRequest',
            fields=(make_field(name='page_token', type=9), )  # str
        ),
        output_message=make_message(
            name='FooResponse',
            fields=(make_field(name='next_page_token', type=9), )  # str
        ))
    assert method.paged_result_field is None
def test_field_types():
    # Create the inner message.
    inner_msg = make_message(
        'InnerMessage',
        fields=(
            make_field(
                'hidden_message',
                message=make_message('HiddenMessage'),
            ),
        )
    )
    inner_enum = make_enum('InnerEnum')

    # Create the outer message, which contains an Inner as a field.
    fields = (
        make_field('inner_message', message=inner_msg),
        make_field('inner_enum', enum=inner_enum),
        make_field('not_interesting'),
    )
    outer = make_message('Outer', fields=fields)

    # Assert that composite field types are recognized but primitives are not.
    assert len(outer.field_types) == 2
    assert inner_msg in outer.field_types
    assert inner_enum in outer.field_types
def test_method_paged_result_ref_types():
    input_msg = make_message(
        name='ListSquidsRequest',
        fields=(
            make_field(name='parent', type=9),  # str
            make_field(name='page_size', type=5),  # int
            make_field(name='page_token', type=9),  # str
        ),
        module='squid',
    )
    mollusc_msg = make_message('Mollusc', module='mollusc')
    output_msg = make_message(name='ListMolluscsResponse',
                              fields=(make_field(name='molluscs',
                                                 message=mollusc_msg,
                                                 repeated=True),
                                      make_field(name='next_page_token',
                                                 type=9)),
                              module='mollusc')
    method = make_method('ListSquids',
                         input_message=input_msg,
                         output_message=output_msg,
                         module='squid')

    ref_type_names = {t.name for t in method.ref_types}
    assert ref_type_names == {
        'ListSquidsRequest',
        'ListSquidsPager',
        'ListSquidsAsyncPager',
        'Mollusc',
    }
def test_field_types_recursive():
    enumeration = make_enum('Enumeration')
    innest_msg = make_message(
        'InnestMessage',
        fields=(
            make_field('enumeration', enum=enumeration),
        )
    )
    inner_msg = make_message(
        'InnerMessage',
        fields=(
            make_field('innest_message', message=innest_msg),
        )
    )
    topmost_msg = make_message(
        'TopmostMessage',
        fields=(
            make_field('inner_message', message=inner_msg),
            make_field('uninteresting')
        )
    )

    actual = {t.name for t in topmost_msg.recursive_field_types}
    expected = {t.name for t in (enumeration, innest_msg, inner_msg)}
    assert actual == expected
def test_method_include_flattened_message_fields():
    a = make_field('a', type=5)
    b = make_field('b', type=11, type_name='Eggs',
                   message=make_message('Eggs'))
    input_msg = make_message('Z', fields=(a, b))
    method = make_method('F', input_message=input_msg, signatures=('a,b',))
    assert len(method.flattened_fields) == 2
def test_method_http_options_generate_sample_implicit_template():
    http_rule = http_pb2.HttpRule(
        get='/v1/{resource.id}/stuff',
    )
    method = make_method(
        'DoSomething',
        make_message(
            name="Input",
            fields=[
                make_field(
                    name="resource",
                    number=1,
                    message=make_message(
                        "Resource",
                        fields=[
                            make_field(name="id", type=9),
                        ],
                    ),
                ),
            ],
        ),
        http_rule=http_rule,
    )

    sample = method.http_options[0].sample_request(method)
    assert sample == {'resource': {
        'id': 'sample1'}}
Example #7
0
def test_method_http_options_generate_sample():
    http_rule = http_pb2.HttpRule(
        get='/v1/{resource.id=projects/*/regions/*/id/**}/stuff', )

    method = make_method(
        'DoSomething',
        make_message(
            name="Input",
            fields=[
                make_field(
                    name="resource",
                    number=1,
                    type=11,
                    message=make_message(
                        "Resource",
                        fields=[
                            make_field(name="id", type=9),
                        ],
                    ),
                ),
            ],
        ),
        http_rule=http_rule,
    )
    sample = method.http_options[0].sample_request(method)
    assert sample == {
        'resource': {
            'id': 'projects/sample1/regions/sample2/id/sample3'
        }
    }
def test_method_flattened_fields_different_package_non_primitive():
    # This test verifies that method flattening handles a special case where:
    # * the method's request message type lives in a different package and
    # * a field in the method_signature is a non-primitive.
    #
    # If the message is defined in a different package it is not guaranteed to
    # be a proto-plus wrapped type, which puts restrictions on assigning
    # directly to its fields, which complicates request construction.
    # The easiest solution in this case is to just prohibit these fields
    # in the method flattening.
    message = make_message('Mantle',
                           package="mollusc.cephalopod.v1",
                           module="squid")
    mantle = make_field('mantle',
                        type=11,
                        type_name='Mantle',
                        message=message,
                        meta=message.meta)
    arms_count = make_field('arms_count', type=5, meta=message.meta)
    input_message = make_message('Squid',
                                 fields=(mantle, arms_count),
                                 package=".".join(
                                     message.meta.address.package),
                                 module=message.meta.address.module)
    method = make_method('PutSquid',
                         input_message=input_message,
                         package="remote.package.v1",
                         module="module",
                         signatures=("mantle,arms_count", ))
    assert set(method.flattened_fields) == {'arms_count'}
def test_method_types():
    input_msg = make_message(name='Input', module='baz')
    output_msg = make_message(name='Output', module='baz')
    method = make_method('DoSomething', input_msg, output_msg,
                         package='foo.bar', module='bacon')
    assert method.name == 'DoSomething'
    assert method.input.name == 'Input'
    assert method.output.name == 'Output'
Example #10
0
def test_is_operation_polling_method():
    T = descriptor_pb2.FieldDescriptorProto.Type

    operation = make_message(
        name="Operation",
        fields=[
            make_field(name=name, type=T.Value("TYPE_STRING"), number=i)
            for i, name in enumerate(
                ("name", "status", "error_code", "error_message"), start=1)
        ],
    )
    for f in operation.fields.values():
        options = descriptor_pb2.FieldOptions()
        # Note: The field numbers were carefully chosen to be the corresponding enum values.
        options.Extensions[ex_ops_pb2.operation_field] = f.number
        f.options.MergeFrom(options)

    request = make_message(
        name="GetOperation",
        fields=[
            make_field(name="name", type=T.Value("TYPE_STRING"), number=1)
        ],
    )

    # Correct positive
    options = descriptor_pb2.MethodOptions()
    options.Extensions[ex_ops_pb2.operation_polling_method] = True
    polling_method = make_method(
        name="Get",
        input_message=request,
        output_message=operation,
        options=options,
    )

    assert polling_method.is_operation_polling_method

    # Normal method that returns operation
    normal_method = make_method(
        name="Get",
        input_message=request,
        output_message=operation,
    )

    assert not normal_method.is_operation_polling_method

    # Method with invalid options combination
    response = make_message(name="Response", fields=[make_field(name="name")])

    invalid_method = make_method(
        name="Get",
        input_message=request,
        output_message=response,
        options=options,  # Reuse options from the actual polling method
    )

    assert not invalid_method.is_operation_polling_method
def test_flattened_oneof_fields():
    mass_kg = make_field(name="mass_kg", oneof="mass", type=5)
    mass_lbs = make_field(name="mass_lbs", oneof="mass", type=5)

    length_m = make_field(name="length_m", oneof="length", type=5)
    length_f = make_field(name="length_f", oneof="length", type=5)

    color = make_field(name="color", type=5)
    mantle = make_field(
        name="mantle",
        message=make_message(
            name="Mantle",
            fields=(
                make_field(name="color", type=5),
                mass_kg,
                mass_lbs,
            ),
        ),
    )
    request = make_message(
        name="CreateMolluscReuqest",
        fields=(
            length_m,
            length_f,
            color,
            mantle,
        ),
    )
    method = make_method(
        name="CreateMollusc",
        input_message=request,
        signatures=[
            "length_m,",
            "length_f,",
            "mantle.mass_kg,",
            "mantle.mass_lbs,",
            "color",
        ]
    )

    expected = {"mass": [mass_kg, mass_lbs], "length": [length_m, length_f]}
    actual = method.flattened_oneof_fields()
    assert expected == actual

    # Check this method too becasue the setup is a lot of work.
    expected = {
        "color": "color",
        "length_m": "length_m",
        "length_f": "length_f",
        "mass_kg": "mantle.mass_kg",
        "mass_lbs": "mantle.mass_lbs",
    }
    actual = method.flattened_field_to_key
    assert expected == actual
def test_method_legacy_flattened_fields():
    required_options = descriptor_pb2.FieldOptions()
    required_options.Extensions[field_behavior_pb2.field_behavior].append(
        field_behavior_pb2.FieldBehavior.Value("REQUIRED"))

    # Cephalopods are required.
    squid = make_field(name="squid", options=required_options)
    octopus = make_field(
        name="octopus",
        message=make_message(
            name="Octopus",
            fields=[make_field(name="mass", options=required_options)]
        ),
        options=required_options)

    # Bivalves are optional.
    clam = make_field(name="clam")
    oyster = make_field(
        name="oyster",
        message=make_message(
            name="Oyster",
            fields=[make_field(name="has_pearl")]
        )
    )

    # Interleave required and optional fields to make sure
    # that, in the legacy flattening, required fields are always first.
    request = make_message("request", fields=[squid, clam, octopus, oyster])

    method = make_method(
        name="CreateMolluscs",
        input_message=request,
        # Signatures should be ignored.
        signatures=[
            "squid,octopus.mass",
            "squid,octopus,oyster.has_pearl"
        ]
    )

    # Use an ordered dict because ordering is important:
    # required fields should come first.
    expected = collections.OrderedDict([
        ("squid", squid),
        ("octopus", octopus),
        ("clam", clam),
        ("oyster", oyster)
    ])

    assert method.legacy_flattened_fields == expected
Example #13
0
def test_body_fields():
    http_rule = http_pb2.HttpRule(post='/v1/{arms_shape=arms/*}/squids',
                                  body='mantle')

    mantle_stuff = make_field(name='mantle_stuff', type=9)
    message = make_message('Mantle', fields=(mantle_stuff, ))
    mantle = make_field('mantle', type=11, type_name='Mantle', message=message)
    arms_shape = make_field('arms_shape', type=9)
    input_message = make_message('Squid', fields=(mantle, arms_shape))
    method = make_method('PutSquid',
                         input_message=input_message,
                         http_rule=http_rule)
    assert set(method.body_fields) == {'mantle'}
    mock_value = method.body_fields['mantle'].mock_value
    assert mock_value == "baz.Mantle(mantle_stuff='mantle_stuff_value')"
def test_method_paged_result_field_not_first():
    paged = make_field(name='foos', message=make_message('Foo'), repeated=True)
    input_msg = make_message(name='ListFoosRequest', fields=(
        make_field(name='parent', type=9),      # str
        make_field(name='page_size', type=5),   # int
        make_field(name='page_token', type=9),  # str
    ))
    output_msg = make_message(name='ListFoosResponse', fields=(
        make_field(name='next_page_token', type=9),  # str
        paged,
    ))
    method = make_method('ListFoos',
                         input_message=input_msg,
                         output_message=output_msg,
                         )
    assert method.paged_result_field == paged
Example #15
0
def test_required_fields():
    REQUIRED = field_behavior_pb2.FieldBehavior.Value('REQUIRED')

    mass_kg = make_field(name="mass_kg", type=5)
    mass_kg.options.Extensions[field_behavior_pb2.field_behavior].append(
        REQUIRED)

    length_m = make_field(name="length_m", type=5)
    length_m.options.Extensions[field_behavior_pb2.field_behavior].append(
        REQUIRED)

    color = make_field(name="color", type=5)
    color.options.Extensions[field_behavior_pb2.field_behavior].append(
        REQUIRED)

    request = make_message(
        name="CreateMolluscReuqest",
        fields=(
            mass_kg,
            length_m,
            color,
        ),
    )

    assert set(request.required_fields) == {mass_kg, length_m, color}
Example #16
0
def test_is_msg_field_pb():
    msg_field = make_field('msg_field', message=make_message('test_msg'))
    str_field = make_field('str_field', type=9)
    int_field = make_field('int_field', type=5)
    assert checks.is_msg_field_pb(msg_field.field_pb)
    assert not checks.is_msg_field_pb(str_field.field_pb)
    assert not checks.is_msg_field_pb(int_field.field_pb)
Example #17
0
def test_differently_named_extended_operation_fields(
    all_field_names,
    canonical_name_to_field_name,
):
    T = descriptor_pb2.FieldDescriptorProto.Type
    operation = make_message(
        name="Operation",
        fields=[
            make_field(
                name=name.lower(),
                type=T.Value("TYPE_STRING"),
                number=i,
            ) for i, name in enumerate(all_field_names, start=1)
        ])
    for f in operation.fields.values():
        options = descriptor_pb2.FieldOptions()
        options.Extensions[ex_ops_pb2.operation_field] = f.number
        f.options.MergeFrom(options)

    expected = {
        k: operation.fields[v]
        for k, v in canonical_name_to_field_name.items()
    } if canonical_name_to_field_name is not None else None
    actual = operation.differently_named_extended_operation_fields

    assert expected == actual
def test_method_flattened_fields():
    a = make_field('a', type=5)  # int
    b = make_field('b', type=5)
    input_msg = make_message('Z', fields=(a, b))
    method = make_method('F', input_message=input_msg, signatures=('a,b', ))
    assert len(method.flattened_fields) == 2
    assert 'a' in method.flattened_fields
    assert 'b' in method.flattened_fields
def test_flattened_ref_types():
    method = make_method(
        'IdentifyMollusc',
        input_message=make_message(
            'IdentifyMolluscRequest',
            fields=(
                make_field(
                    'cephalopod',
                    message=make_message(
                        'Cephalopod',
                        fields=(
                            make_field('mass_kg', type='TYPE_INT32'),
                            make_field(
                                'squid',
                                number=2,
                                message=make_message('Squid'),
                            ),
                            make_field(
                                'clam',
                                number=3,
                                message=make_message('Clam'),
                            ),
                        ),
                    ),
                ),
                make_field(
                    'stratum',
                    enum=make_enum(
                        'Stratum',
                    )
                ),
            ),
        ),
        signatures=('cephalopod.squid,stratum',),
        output_message=make_message('Mollusc'),
    )

    expected_flat_ref_type_names = {
        'IdentifyMolluscRequest',
        'Squid',
        'Stratum',
        'Mollusc',
    }
    actual_flat_ref_type_names = {t.name for t in method.flat_ref_types}
    assert expected_flat_ref_type_names == actual_flat_ref_type_names
def test_method_query_params():
    # tests only the basic case of grpc transcoding
    http_rule = http_pb2.HttpRule(post='/v1/{project}/topics', body='address')
    input_message = make_message('MethodInput',
                                 fields=(make_field('region'),
                                         make_field('project'),
                                         make_field('address')))
    method = make_method('DoSomething',
                         http_rule=http_rule,
                         input_message=input_message)
    assert method.query_params == {'region'}