コード例 #1
0
def test_get_output_filename_with_namespace():
    g = generator.Generator(api_schema=make_api(
        naming=make_naming(
            name='Spam',
            namespace=('Ham', 'Bacon'),
            version='v2',
        ),
    ))
    template_name = '$namespace/$name_$version/foo.py.j2'
    assert g._get_output_filename(template_name) == 'ham/bacon/spam_v2/foo.py'
コード例 #2
0
def test_proto_builder_constructor():
    # Create a generator.
    g = generator.Generator(api_schema=make_api())
    assert isinstance(g._api, api.API)

    # Assert we have a Jinja environment also, with the expected filters.
    # This is internal implementation baseball, but this is the best place
    # to establish this and templates will depend on it.
    assert isinstance(g._env, jinja2.Environment)
    assert 'snake_case' in g._env.filters
    assert 'subsequent_indent' in g._env.filters
    assert 'wrap' in g._env.filters
コード例 #3
0
def test_get_output_filename_with_service():
    g = generator.Generator(api_schema=make_api(
        naming=make_naming(namespace=(), name='Spam', version='v2'),
    ))
    template_name = '$name/$service/foo.py.j2'
    assert g._get_output_filename(
        template_name,
        context={
            'service': wrappers.Service(
                methods=[],
                service_pb=descriptor_pb2.ServiceDescriptorProto(name='Eggs'),
            ),
        }
    ) == 'spam/eggs/foo.py'
コード例 #4
0
def test_render_templates_additional_context():
    g = generator.Generator(api_schema=make_api())

    # Determine the templates to be rendered.
    templates = ('foo.j2',)
    with mock.patch.object(jinja2.Environment, 'get_template') as get_template:
        get_template.return_value = jinja2.Template('A {{ thing }}!')

        # Render the templates.
        files = g._render_templates(templates, additional_context={
            'thing': 'bird',
        })

    # Test that we get back the expected content for each template.
    assert len(files) == 1
    assert files[0].name == 'foo'
    assert files[0].content == 'A bird!\n'
コード例 #5
0
def test_render_templates():
    g = generator.Generator(api_schema=make_api())

    # Determine the templates to be rendered.
    templates = ('foo.j2', 'bar.j2')
    with mock.patch.object(jinja2.Environment, 'get_template') as get_template:
        get_template.side_effect = lambda t: jinja2.Template(
            f'Hello, I am `{t}`.',
        )

        # Render the templates.
        files = g._render_templates(templates)

    # Test that we get back the expected content for each template.
    assert len(files) == 2
    assert files[0].name == 'foo'
    assert files[1].name == 'bar'
    assert files[0].content == 'Hello, I am `foo.j2`.\n'
    assert files[1].content == 'Hello, I am `bar.j2`.\n'
コード例 #6
0
def test_get_response():
    # Create a generator with mock data.
    #
    # We want to ensure that templates are rendered for each service,
    # which we prove by sending two services.
    file_pb2 = descriptor_pb2.FileDescriptorProto(
        name='bacon.proto',
        package='foo.bar.v1',
        service=[descriptor_pb2.ServiceDescriptorProto(name='SpamService'),
                 descriptor_pb2.ServiceDescriptorProto(name='EggsService')],
    )
    api_schema = make_api(make_proto(file_pb2))
    g = generator.Generator(api_schema=api_schema)

    # Mock all the rendering methods.
    with mock.patch.object(g, '_render_templates') as _render_templates:
        _render_templates.return_value = [
            plugin_pb2.CodeGeneratorResponse.File(
                name='template_file',
                content='This was a template.',
            ),
        ]

        # Okay, now run the `get_response` method.
        response = g.get_response()

        # First and foremost, we care that we got a valid response
        # object back (albeit not so much what is in it).
        assert isinstance(response, plugin_pb2.CodeGeneratorResponse)

        # Next, determine that the general API templates and service
        # templates were both called; the method should be called
        # once per service plus one for the API as a whole.
        assert _render_templates.call_count == len(file_pb2.service) + 1

        # The service templates should have been called with the
        # filename transformation and the additional `service` variable.
        for call in _render_templates.mock_calls:
            _, args, kwargs = call
            if args[0] != g._env.loader.service_templates:
                continue
            service = kwargs['additional_context']['service']
            assert isinstance(service, wrappers.Service)
コード例 #7
0
def test_get_output_filename():
    g = generator.Generator(api_schema=make_api(
        naming=make_naming(namespace=(), name='Spam', version='v2'),
    ))
    template_name = '$namespace/$name_$version/foo.py.j2'
    assert g._get_output_filename(template_name) == 'spam_v2/foo.py'