示例#1
0
def test_generate_sample_basic():
    # Note: the sample integration tests are needfully large
    # and difficult to eyeball parse. They are intended to be integration tests
    # that catch errors in behavior that is emergent from combining smaller features
    # or in features that are sufficiently small and trivial that it doesn't make sense
    # to have standalone tests.
    input_type = DummyMessage(
        type="REQUEST TYPE",
        fields={
            "classify_target":
            DummyField(message=DummyMessage(
                type="CLASSIFY TYPE",
                fields={
                    "video":
                    DummyField(message=DummyMessage(type="VIDEO TYPE"), ),
                    "location_annotation":
                    DummyField(message=DummyMessage(type="LOCATION TYPE"), )
                },
            ))
        })

    api_naming = naming.NewNaming(name="MolluscClient",
                                  namespace=("molluscs", "v1"))
    service = wrappers.Service(
        service_pb=namedtuple('service_pb', ['name'])('MolluscService'),
        methods={
            "Classify":
            DummyMethod(input=input_type,
                        output=message_factory("$resp.taxonomy"),
                        flattened_fields={
                            "classify_target":
                            DummyField(name="classify_target")
                        })
        },
        visible_resources={},
    )

    schema = DummyApiSchema(
        services={"animalia.mollusca.v1.Mollusc": service},
        naming=api_naming,
    )

    sample = {
        "service":
        "animalia.mollusca.v1.Mollusc",
        "rpc":
        "Classify",
        "id":
        "mollusc_classify_sync",
        "description":
        "Determine the full taxonomy of input mollusc",
        "request": [{
            "field": "classify_target.video",
            "value": "path/to/mollusc/video.mkv",
            "input_parameter": "video",
            "value_is_file": True
        }, {
            "field": "classify_target.location_annotation",
            "value": "New Zealand",
            "input_parameter": "location"
        }],
        "response": [{
            "print": ['Mollusc is a "%s"', "$resp.taxonomy"]
        }]
    }

    sample_str = samplegen.generate_sample(
        sample, schema, env.get_template('examples/sample.py.j2'))

    sample_id = ("mollusc_classify_sync")
    expected_str = '''# -*- coding: utf-8 -*-
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# DO NOT EDIT! This is a generated sample ("request",  "%s")
#
# To install the latest published package dependency, execute the following:
#   pip3 install molluscs-v1-molluscclient


# [START %s]
from google import auth
from google.auth import credentials
from molluscs.v1.molluscclient.services.mollusc_service import MolluscServiceClient

def sample_classify(video, location):
    """Determine the full taxonomy of input mollusc"""

    client = MolluscServiceClient(
        credentials=credentials.AnonymousCredentials(),
        transport="grpc",
    )

    classify_target = {}
    # video = "path/to/mollusc/video.mkv"
    with open(video, "rb") as f:
        classify_target["video"] = f.read()

    # location = "New Zealand"
    classify_target["location_annotation"] = location

 
 
    response = client.classify(classify_target=classify_target)
    print("Mollusc is a \\"{}\\"".format(response.taxonomy))
 

# [END %s]

def main():
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("--video",
                        type=str,
                        default="path/to/mollusc/video.mkv")
    parser.add_argument("--location",
                        type=str,
                        default="New Zealand")
    args = parser.parse_args()

    sample_classify(args.video, args.location)


if __name__ == "__main__":
    main()
''' % (sample_id, sample_id, sample_id)

    assert sample_str == expected_str
def test_generate_sample_basic():
    # Note: the sample integration tests are needfully large
    # and difficult to eyeball parse. They are intended to be integration tests
    # that catch errors in behavior that is emergent from combining smaller features
    # or in features that are sufficiently small and trivial that it doesn't make sense
    # to have standalone tests.
    input_type = DummyMessage(
        type="REQUEST TYPE",
        fields={
            "classify_request": DummyField(
                message=DummyMessage(
                    type="CLASSIFY TYPE",
                    fields={
                        "video": DummyField(
                            message=DummyMessage(type="VIDEO TYPE"),
                        ),
                        "location_annotation": DummyField(
                            message=DummyMessage(type="LOCATION TYPE"),
                        )
                    },
                )
            )
        }
    )
    schema = DummyApiSchema(
        {
            "animalia.mollusca.v1.Mollusc": DummyService(
                {
                    "Classify": DummyMethod(
                        input=input_type,
                        output=message_factory("$resp.taxonomy")
                    )
                }
            )
        },
        DummyNaming("molluscs-v1-mollusc")
    )

    sample = {"service": "animalia.mollusca.v1.Mollusc",
              "rpc": "Classify",
              "id": "mollusc_classify_sync",
              "description": "Determine the full taxonomy of input mollusc",
              "request": [
                  {"field": "classify_request.video",
                   "value": "'path/to/mollusc/video.mkv'",
                   "input_parameter": "video",
                   "value_is_file": True},
                  {"field": "classify_request.location_annotation",
                   "value": "'New Zealand'",
                   "input_parameter": "location"}
              ],
              "response": [{"print": ["Mollusc is a %s", "$resp.taxonomy"]}]}

    sample_str = samplegen.generate_sample(
        sample, env, schema)

    sample_id = ("mollusc_classify_sync")
    expected_str = '''# TODO: add a copyright
# TODO: add a license
#
# DO NOT EDIT! This is a generated sample ("request",  "%s")
#
# To install the latest published package dependency, execute the following:
#   pip3 install molluscs-v1-mollusc


# [START %s]

def sample_classify(video, location):
    """Determine the full taxonomy of input mollusc"""

    client = mollusca_v1.MolluscClient()

    classify_request = {}
    # video = 'path/to/mollusc/video.mkv'
    with open(video, "rb") as f:
        classify_request["video"] = f.read()

    # location = 'New Zealand'
    classify_request["location_annotation"] = location


    response = client.classify(classify_request)

    print("Mollusc is a {}".format(response.taxonomy))

# [END %s]

def main():
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("--video",
                        type=str,
                        default='path/to/mollusc/video.mkv')
    parser.add_argument("--location",
                        type=str,
                        default='New Zealand')
    args = parser.parse_args()

    sample_classify(args.video, args.location)


if __name__ == "__main__":
    main()
''' % (sample_id, sample_id, sample_id)

    assert sample_str == expected_str