コード例 #1
0
def test_method_field_headers_present():
    verbs = [
        'get',
        'put',
        'post',
        'delete',
        'patch',
    ]

    for v in verbs:
        rule = http_pb2.HttpRule(**{v: '/v1/{parent=projects/*}/topics'})
        method = make_method('DoSomething', http_rule=rule)
        assert method.field_headers == (wrappers.FieldHeader('parent'), )
        assert method.field_headers[0].raw == 'parent'
        assert method.field_headers[0].disambiguated == 'parent'

        # test that reserved keyword in field header is disambiguated
        rule = http_pb2.HttpRule(**{v: '/v1/{object=objects/*}/topics'})
        method = make_method('DoSomething', http_rule=rule)
        assert method.field_headers == (wrappers.FieldHeader('object'), )
        assert method.field_headers[0].raw == 'object'
        assert method.field_headers[0].disambiguated == 'object_'

        # test w/o equal sign
        rule = http_pb2.HttpRule(**{v: '/v1/{parent}/topics'})
        method = make_method('DoSomething', http_rule=rule)
        assert method.field_headers == (wrappers.FieldHeader('parent'), )
        assert method.field_headers[0].raw == 'parent'
        assert method.field_headers[0].disambiguated == 'parent'
コード例 #2
0
def test_method_http_options_additional_bindings():
    http_rule = http_pb2.HttpRule(
        post='/v1/{parent=projects/*}/topics',
        body='*',
        additional_bindings=[
            http_pb2.HttpRule(
                post='/v1/{parent=projects/*/regions/*}/topics',
                body='*',
            ),
            http_pb2.HttpRule(
                post='/v1/projects/p1/topics',
                body='body_field',
            ),
        ]
    )
    method = make_method('DoSomething', http_rule=http_rule)
    assert [dataclasses.asdict(http) for http in method.http_options] == [
        {
            'method': 'post',
            'uri': '/v1/{parent=projects/*}/topics',
            'body': '*'
            },
        {
            'method': 'post',
            'uri': '/v1/{parent=projects/*/regions/*}/topics',
            'body': '*'
            },
        {
            'method': 'post',
            'uri': '/v1/projects/p1/topics',
            'body': 'body_field'
            }]
コード例 #3
0
def test_method_http_options_empty_http_rule():
    http_rule = http_pb2.HttpRule()
    method = make_method('DoSomething', http_rule=http_rule)
    assert method.http_options == []

    http_rule = http_pb2.HttpRule(get='')
    method = make_method('DoSomething', http_rule=http_rule)
    assert method.http_options == []
コード例 #4
0
def test_method_path_params():
    # tests only the basic case of grpc transcoding
    http_rule = http_pb2.HttpRule(post='/v1/{project}/topics')
    method = make_method('DoSomething', http_rule=http_rule)
    assert method.path_params == ['project']

    http_rule2 = http_pb2.HttpRule(post='/v1beta1/{name=rooms/*/blurbs/*}')
    method2 = make_method("DoSomething", http_rule=http_rule2)
    assert method2.path_params == ["name"]
コード例 #5
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'
        }
    }
コード例 #6
0
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'}}
コード例 #7
0
def test_method_http_opt_no_body():
    http_rule = http_pb2.HttpRule(post='/v1/{parent=projects/*}/topics')
    method = make_method('DoSomething', http_rule=http_rule)
    assert method.http_opt == {
        'verb': 'post',
        'url': '/v1/{parent=projects/*}/topics'
    }
コード例 #8
0
def test_set_msg_with_dict_field():
    rule = http_pb2.HttpRule()
    pattern = {"kind": "foo", "path": "bar"}

    protobuf_helpers.set(rule, "custom", pattern)

    assert rule.custom.kind == "foo"
    assert rule.custom.path == "bar"
コード例 #9
0
def test_set_msg_nested_key():
    rule = http_pb2.HttpRule(
        custom=http_pb2.CustomHttpPattern(kind="foo", path="bar"))

    protobuf_helpers.set(rule, "custom.kind", "baz")

    assert rule.custom.kind == "baz"
    assert rule.custom.path == "bar"
コード例 #10
0
def test_set_msg_with_msg_field():
    rule = http_pb2.HttpRule()
    pattern = http_pb2.CustomHttpPattern(kind='foo', path='bar')

    protobuf_helpers.set(rule, 'custom', pattern)

    assert rule.custom.kind == 'foo'
    assert rule.custom.path == 'bar'
コード例 #11
0
def test_set_msg_with_dict_field():
    rule = http_pb2.HttpRule()
    pattern = {'kind': 'foo', 'path': 'bar'}

    protobuf_helpers.set(rule, 'custom', pattern)

    assert rule.custom.kind == 'foo'
    assert rule.custom.path == 'bar'
コード例 #12
0
def test_set_msg_nested_key():
    rule = http_pb2.HttpRule(
        custom=http_pb2.CustomHttpPattern(kind='foo', path='bar'))

    protobuf_helpers.set(rule, 'custom.kind', 'baz')

    assert rule.custom.kind == 'baz'
    assert rule.custom.path == 'bar'
コード例 #13
0
def test_set_msg_with_msg_field():
    rule = http_pb2.HttpRule()
    pattern = http_pb2.CustomHttpPattern(kind="foo", path="bar")

    protobuf_helpers.set(rule, "custom", pattern)

    assert rule.custom.kind == "foo"
    assert rule.custom.path == "bar"
コード例 #14
0
def test_body_fields_no_body():
    http_rule = http_pb2.HttpRule(
        post='/v1/{arms_shape=arms/*}/squids',
    )

    method = make_method(
        'PutSquid', http_rule=http_rule)

    assert not method.body_fields
コード例 #15
0
def test_method_http_options_reserved_name_in_url():
    http_rule = http_pb2.HttpRule(post='/v1/license/{license=lic/*}', body='*')
    method = make_method('DoSomething', http_rule=http_rule)
    assert [dataclasses.asdict(http) for http in method.http_options] == [{
        'method':
        'post',
        'uri':
        '/v1/license/{license_=lic/*}',
        'body':
        '*'
    }]
コード例 #16
0
def test_method_http_options_body_field():
    http_rule = http_pb2.HttpRule(
        post='/v1/{parent=projects/*}/topics',
        body='body_field'
    )
    method = make_method('DoSomething', http_rule=http_rule)
    assert [dataclasses.asdict(http) for http in method.http_options] == [{
        'method': 'post',
        'uri': '/v1/{parent=projects/*}/topics',
        'body': 'body_field'
    }]
コード例 #17
0
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'}
コード例 #18
0
def test_method_http_options():
    verbs = ['get', 'put', 'post', 'delete', 'patch']
    for v in verbs:
        http_rule = http_pb2.HttpRule(**{v: '/v1/{parent=projects/*}/topics'})
        method = make_method('DoSomething', http_rule=http_rule)
        assert [dataclasses.asdict(http) for http in method.http_options] == [{
            'method':
            v,
            'uri':
            '/v1/{parent=projects/*}/topics',
            'body':
            None
        }]
コード例 #19
0
def test_method_field_headers_present():
    verbs = [
        'get',
        'put',
        'post',
        'delete',
        'patch',
    ]

    for v in verbs:
        rule = http_pb2.HttpRule(**{v: '/v1/{parent=projects/*}/topics'})
        method = make_method('DoSomething', http_rule=rule)
        assert method.field_headers == ('parent', )
コード例 #20
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')"
コード例 #21
0
 def test_set_dict_nested_with_message(self):
     rule = http_pb2.HttpRule()
     pattern = http_pb2.CustomHttpPattern(kind='foo', path='bar')
     protobuf.set(rule, 'custom', pattern)
     assert rule.custom.kind == 'foo'
     assert rule.custom.path == 'bar'
コード例 #22
0
def test_method_field_headers_present():
    http_rule = http_pb2.HttpRule(get='/v1/{parent=projects/*}/topics')
    method = make_method('DoSomething', http_rule=http_rule)
    assert method.field_headers == ('parent', )
コード例 #23
0
def test_method_idempotent_no():
    http_rule = http_pb2.HttpRule(post='/v1/{parent=projects/*}/topics')
    method = make_method('DoSomething', http_rule=http_rule)
    assert method.idempotent is False
コード例 #24
0
 def test_set_dict_nested_with_dict(self):
     rule = http_pb2.HttpRule()
     pattern = {'kind': 'foo', 'path': 'bar'}
     protobuf.set(rule, 'custom', pattern)
     assert rule.custom.kind == 'foo'
     assert rule.custom.path == 'bar'
コード例 #25
0
def test_service_has_field_headers():
    http_rule = http_pb2.HttpRule(get='/v1/{parent=projects/*}/topics')
    service = make_service_with_method_options(http_rule=http_rule)
    assert service.has_field_headers
コード例 #26
0
def test_method_path_params():
    # tests only the basic case of grpc transcoding
    http_rule = http_pb2.HttpRule(post='/v1/{project}/topics')
    method = make_method('DoSomething', http_rule=http_rule)
    assert method.path_params == ['project']