def test_generate_sample_rpc_not_found(): schema = DummyApiSchema( {"Mollusc": DummyService({})}, DummyNaming("pkg_name")) sample = {"service": "Mollusc", "rpc": "Classify"} with pytest.raises(types.RpcMethodNotFound): list(samplegen.generate_sample(sample, env, schema))
def test_generate_manifest_relative_path_sanity(): with pytest.raises(types.InvalidSampleFpath): manifest.generate( {"molluscs/squid.py": { "id": "squid_sample" }}.items(), DummyApiSchema(naming=DummyNaming(name="Mollusc", version="v1")))
def test_generate_sample_service_not_found(): schema = DummyApiSchema({}, DummyNaming("pkg_name")) sample = {"service": "Mollusc"} with pytest.raises(types.UnknownService): samplegen.generate_sample( sample, schema, env.get_template('examples/sample.py.j2'), )
def test_generate_manifest(): fpath_to_dummy_sample = { "samples/squid_fpath.py": {"id": "squid_sample"}, "samples/clam_fpath.py": {"id": "clam_sample", "region_tag": "giant_clam_sample"}, } fname, info = manifest.generate( fpath_to_dummy_sample.items(), DummyApiSchema(naming=DummyNaming(name="Mollusc", version="v1")), # Empirically derived number such that the # corresponding time_struct tests the zero # padding in the returned filename. manifest_time=4486525628 ) assert fname == "Mollusc.v1.python.21120304.090708.manifest.yaml" doc = gapic_yaml.Doc([ gapic_yaml.KeyVal("type", "manifest/samples"), gapic_yaml.KeyVal("schema_version", "3"), gapic_yaml.Map(name="python", anchor_name="python", elements=[ gapic_yaml.KeyVal( "environment", "python"), gapic_yaml.KeyVal( "bin", "python3"), gapic_yaml.KeyVal( "base_path", "samples"), gapic_yaml.KeyVal( "invocation", "'{bin} {path} @args'"), ]), gapic_yaml.Collection(name="samples", elements=[ [ gapic_yaml.Alias( "python"), gapic_yaml.KeyVal( "sample", "squid_sample"), gapic_yaml.KeyVal( "path", "'{base_path}/squid_fpath.py'"), gapic_yaml.Null, ], [ gapic_yaml.Alias("python"), gapic_yaml.KeyVal( "sample", "clam_sample"), gapic_yaml.KeyVal( "path", "'{base_path}/clam_fpath.py'"), gapic_yaml.KeyVal( "region_tag", "giant_clam_sample") ], ]) ]) assert info == doc expected_rendering = dedent( """\ --- type: manifest/samples schema_version: 3 python: &python environment: python bin: python3 base_path: samples invocation: '{bin} {path} @args' samples: - <<: *python sample: squid_sample path: '{base_path}/squid_fpath.py' - <<: *python sample: clam_sample path: '{base_path}/clam_fpath.py' region_tag: giant_clam_sample """) rendered_yaml = doc.render() assert rendered_yaml == expected_rendering expected_parsed_manifest = { "type": "manifest/samples", "schema_version": 3, "python": { "environment": "python", "bin": "python3", "base_path": "samples", "invocation": "{bin} {path} @args", }, "samples": [ { "environment": "python", "bin": "python3", "base_path": "samples", "invocation": "{bin} {path} @args", "sample": "squid_sample", "path": "{base_path}/squid_fpath.py", }, { "environment": "python", "bin": "python3", "base_path": "samples", "invocation": "{bin} {path} @args", "sample": "clam_sample", "path": "{base_path}/clam_fpath.py", "region_tag": "giant_clam_sample", }, ], } parsed_manifest = yaml.safe_load(rendered_yaml) assert parsed_manifest == expected_parsed_manifest
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