From ef15ba195794b7d19f94fef3f33dd8599177efa3 Mon Sep 17 00:00:00 2001 From: David Steinberg Date: Mon, 9 Jan 2017 19:34:59 -0800 Subject: [PATCH] Clean up imports Add tests that show basic attributes examples --- .../ga4gh/genotype_phenotype_service.proto | 1 - .../proto/ga4gh/sequence_annotations.proto | 1 - tests/test_imports.py | 84 ++++++++++++++++++- 3 files changed, 82 insertions(+), 4 deletions(-) diff --git a/src/main/proto/ga4gh/genotype_phenotype_service.proto b/src/main/proto/ga4gh/genotype_phenotype_service.proto index 56c3ce4d..8a8cf537 100644 --- a/src/main/proto/ga4gh/genotype_phenotype_service.proto +++ b/src/main/proto/ga4gh/genotype_phenotype_service.proto @@ -3,7 +3,6 @@ syntax = "proto3"; package ga4gh; import "ga4gh/common.proto"; -import "ga4gh/metadata.proto"; import "ga4gh/genotype_phenotype.proto"; import "google/api/annotations.proto"; diff --git a/src/main/proto/ga4gh/sequence_annotations.proto b/src/main/proto/ga4gh/sequence_annotations.proto index 8d688486..cea5024e 100644 --- a/src/main/proto/ga4gh/sequence_annotations.proto +++ b/src/main/proto/ga4gh/sequence_annotations.proto @@ -3,7 +3,6 @@ syntax = "proto3"; package ga4gh; import "ga4gh/common.proto"; -import "ga4gh/metadata.proto"; // This protocol defines annotations on GA4GH genomic sequences It includes two diff --git a/tests/test_imports.py b/tests/test_imports.py index 52a52efa..e65664d6 100644 --- a/tests/test_imports.py +++ b/tests/test_imports.py @@ -9,16 +9,17 @@ # similar to dev_glue.py import ga4gh + ga4gh.__path__.insert(0, 'python/ga4gh') import ga4gh.schemas._protocol_version as version # NOQA import ga4gh.schemas.google.api.http_pb2 as http_pb2 # NOQA import ga4gh.schemas.ga4gh.common_pb2 as common_pb2 # NOQA +import ga4gh.schemas.ga4gh.metadata_pb2 as metadata # NOQA import ga4gh.schemas.pb as pb # NOQA class TestPb(unittest.TestCase): - def testString(self): self.assertEqual(pb.DEFAULT_STRING, pb.string(None)) self.assertEqual('A', pb.string('A')) @@ -29,7 +30,6 @@ def testInt(self): class TestSchemas(unittest.TestCase): - def testVersion(self): version.version.split('.') self.assertIsNotNone(version.version) @@ -37,5 +37,85 @@ def testVersion(self): def testGa4ghCommon(self): self.assertIsNotNone(common_pb2.DESCRIPTOR) + def testDatasetAttributes(self): + """ + Demonstrates the usage of the Attribute's field on a dataset message. + This field is available on many other messages as well. + """ + key = "numbers_and_strings" + string_value = "hello" + int32_value = 5 + double_value = 3.14159 + attributes = {key: [string_value, int32_value, double_value]} + dataset = metadata.Dataset() + myattribute = dataset.attributes.attr[key].values + + myattribute.add().string_value = string_value + myattribute.add().int32_value = int32_value + myattribute.add().double_value = double_value + + self.assertEqual( + dataset.attributes.attr[key].values[0].string_value, + attributes[key][0]) + + self.assertEqual( + dataset.attributes.attr[key].values[1].int32_value, + attributes[key][1]) + + self.assertEqual( + dataset.attributes.attr[key].values[2].double_value, + attributes[key][2]) + + def testNestedAttributes(self): + """ + Demonstrates how nested attributes can be used to interchange + arbitrary unstructured, typed data. + """ + dataset = metadata.Dataset() + key1 = "key1" + key2 = "key2" + string_value = "hello" + int32_value = 32 + attributes = {key1: [{key2: [string_value]}, int32_value]} + myAttribute = dataset.attributes.attr[key1].values + nestedAttribute = myAttribute.add().attributes.attr[key2].values + nestedAttribute.add().string_value = string_value + myAttribute.add().int32_value = int32_value + + self.assertEqual( + dataset.attributes.attr[key1].values[0]. + attributes.attr[key2].values[0].string_value, + attributes[key1][0][key2][0]) + + self.assertEqual(dataset.attributes.attr[key1]. + values[1].int32_value, attributes[key1][1]) + + def testTypedAttributes(self): + """ + Demonstrates how to use other types defined in common to create an + attribute message. + """ + dataset = metadata.Dataset() + ontologyTerm = common_pb2.OntologyTerm() + ontologyTerm.id = "test" + ontologyKey = "my_ontology_term" + + dataset.attributes.attr[ontologyKey] \ + .values.add().ontology_term.MergeFrom(ontologyTerm) + + self.assertEqual( + dataset.attributes.attr[ontologyKey].values[0].ontology_term.id, + ontologyTerm.id) + + experiment = common_pb2.Experiment() + experiment.id = "test" + key = "my_experiment" + + dataset.attributes.attr[key] \ + .values.add().experiment.MergeFrom(experiment) + self.assertEqual( + dataset.attributes.attr[key].values[0].experiment.id, experiment.id) + + def testGoogle(self): self.assertIsNotNone(http_pb2.DESCRIPTOR)