def test_integer_attribute(self): i = IntegerAttribute(44) self.assertEqual(i.value, 44) i = IntegerAttribute(-3) self.assertEqual(i.value, -3) i = IntegerAttribute(None, allow_null=True) with self.assertRaises(AttributeValueError): i = IntegerAttribute(None) with self.assertRaises(AttributeValueError): i = IntegerAttribute(settings.POSITIVE_INF_MARKER)
def test_metadata_correct(self): resource_path = os.path.join(TESTDIR, 'gene_annotations.tsv') t = FeatureTable() column_dict = {} feature_list = [] for i, line in enumerate(open(resource_path)): if i == 0: contents = line.strip().split('\t') for j,c in enumerate(contents[1:]): column_dict[j] = c else: contents = line.strip().split('\t') gene_name = contents[0] attr_dict = {} for j,v in enumerate(contents[1:]): try: v = int(v) attr = IntegerAttribute(v) except ValueError: attr = StringAttribute(v) attr_dict[column_dict[j]] = attr f = Feature(gene_name, attr_dict) feature_list.append(f) expected_feature_set = FeatureSetSerializer(FeatureSet(feature_list)).data metadata = t.extract_metadata(resource_path) self.assertEqual(metadata[FEATURE_SET_KEY], expected_feature_set) self.assertIsNone(metadata[OBSERVATION_SET_KEY]) self.assertIsNone(metadata[PARENT_OP_KEY])
def test_metadata_correct(self): resource_path = os.path.join(TESTDIR, 'gene_annotations.tsv') t = FeatureTable() column_dict = {} feature_list = [] for i, line in enumerate(open(resource_path)): if i == 0: contents = line.strip().split('\t') for j, c in enumerate(contents[1:]): column_dict[j] = c else: contents = line.strip().split('\t') gene_name = contents[0] attr_dict = {} for j, v in enumerate(contents[1:]): try: v = int(v) attr = IntegerAttribute(v) except ValueError: attr = StringAttribute(v) attr_dict[column_dict[j]] = attr f = Feature(gene_name, attr_dict) feature_list.append(f) expected_feature_set = FeatureSetSerializer( FeatureSet(feature_list)).data metadata = t.extract_metadata(resource_path, 'tsv') # Commented out when we removed the automatic creation of Feature metadata # for FeatureTable resource types. For large files, it was causing issues # with exceptionally large JSON failing to store in db table. #self.assertEqual(metadata[FEATURE_SET_KEY], expected_feature_set) self.assertIsNone(metadata[FEATURE_SET_KEY]) self.assertIsNone(metadata[OBSERVATION_SET_KEY]) self.assertIsNone(metadata[PARENT_OP_KEY])
def setUp(self): float_attr = FloatAttribute(0.01) int_attr = IntegerAttribute(3) self.demo_element = Feature('my_identifier', { 'keyA': float_attr, 'keyB': int_attr }) # the class that will execute the tests self.tester_class = ElementTester(Feature)
def test_adding_ovewrite_attribute(self, testcase): ''' Test overwriting an existing attribute ''' float_attr = FloatAttribute(0.01) int_attr = IntegerAttribute(3) element = self.element_class('some_identifier', { 'keyA': float_attr, 'keyB': int_attr }) element.add_attribute('keyA', { 'attribute_type': 'Float', 'value': 2.3 }, overwrite=True) expected_attr = FloatAttribute(2.3) testcase.assertEqual(element.attributes['keyA'], expected_attr)
def test_adding_new_attribute(self, testcase): ''' Test adding a new attribute ''' float_attr = FloatAttribute(0.01) int_attr = IntegerAttribute(3) element = self.element_class('some_identifier', { 'keyA': float_attr, 'keyB': int_attr }) element.add_attribute('keyC', { 'attribute_type': 'String', 'value': 'abc' }) expected_attr = StringAttribute('abc') testcase.assertEqual(element.attributes['keyC'], expected_attr) expected_keys = set(['keyA', 'keyB', 'keyC']) existing_keys = set(element.attributes.keys()) testcase.assertTrue(expected_keys == existing_keys)
def setUp(self): float_attr = FloatAttribute(0.01) int_attr = IntegerAttribute(3) boolean_attr = BooleanAttribute(True) bounded_float_attr = BoundedFloatAttribute(0.1, min=0.0, max=1.0) self.demo_element = Feature('my_identifier', { 'keyA': float_attr, 'keyB': int_attr }) self.demo_element2 = Feature('my_identifier', {}) self.demo_element_data = { 'id': 'my_identifier', 'attributes': { 'keyA': { 'attribute_type': 'Float', 'value': 0.01 }, 'keyB': { 'attribute_type': 'Integer', 'value': 3 } } } self.demo_element_data2 = {'id': 'my_identifier', 'attributes': {}} self.bad_element_data = { 'id': 'my_identifier', 'attributes': { 'keyA': { 'attribute_type': 'Float', 'value': 'abc' }, 'keyB': { 'attribute_type': 'Integer', 'value': 3 } } } self.demo_element_data_w_bounds = { 'id': 'my_identifier', 'attributes': { 'pvalue': { 'attribute_type': 'BoundedFloat', 'value': 0.1, 'min': 0.0, 'max': 1.0 }, 'keyB': { 'attribute_type': 'Integer', 'value': 3 } } } self.demo_element_w_bounds = Feature('my_identifier', { 'pvalue': bounded_float_attr, 'keyB': int_attr }) self.bad_demo_element_data_w_bounds = { 'id': 'my_identifier', 'attributes': { 'pvalue': { 'attribute_type': 'BoundedFloat', 'value': 1.1, # out of bounds!! 'min': 0.0, 'max': 1.0 }, 'keyB': { 'attribute_type': 'Integer', 'value': 3 } } } self.demo_element_w_bool = Feature('my_identifier', { 'keyA': int_attr, 'some_bool': boolean_attr }) self.demo_element_data_w_bool1 = { 'id': 'my_identifier', 'attributes': { 'keyA': { 'attribute_type': 'Integer', 'value': 3 }, 'some_bool': { 'attribute_type': 'Boolean', 'value': 'true' } } } self.demo_element_data_w_bool2 = { 'id': 'my_identifier', 'attributes': { 'keyA': { 'attribute_type': 'Integer', 'value': 3 }, 'some_bool': { 'attribute_type': 'Boolean', 'value': 1 } } } self.demo_element_data_w_bool3 = { 'id': 'my_identifier', 'attributes': { 'keyA': { 'attribute_type': 'Integer', 'value': 3 }, 'some_bool': { 'attribute_type': 'Boolean', 'value': True } } } self.bad_demo_element_data_w_bool = { 'id': 'my_identifier', 'attributes': { 'keyA': { 'attribute_type': 'Integer', 'value': 3 }, 'some_bool': { 'attribute_type': 'Boolean', 'value': -1 } } } # the class that will execute the tests self.tester_class = ElementSerializerTester(FeatureSerializer)
def test_int_string_rejected_for_integer(self): with self.assertRaises(AttributeValueError): IntegerAttribute('2')
def test_float_rejected_for_integer(self): with self.assertRaises(AttributeValueError): IntegerAttribute(1.2)
def convert(self, input_key, user_input, op_dir, staging_dir): i = IntegerAttribute(user_input) return {input_key: i.value}
def setUp(self): float_attr = FloatAttribute(0.01) int_attr = IntegerAttribute(3) bounded_float_attr = BoundedFloatAttribute(0.1, min=0.0, max=1.0) self.demo_element = Observation('my_identifier', { 'keyA': float_attr, 'keyB': int_attr }) self.demo_element2 = Observation('my_identifier', {}) self.demo_element_data = { 'id': 'my_identifier', 'attributes': { 'keyA': { 'attribute_type': 'Float', 'value': 0.01 }, 'keyB': { 'attribute_type': 'Integer', 'value': 3 } } } self.demo_element_data2 = {'id': 'my_identifier', 'attributes': {}} self.bad_element_data = { 'id': 'my_identifier', 'attributes': { 'keyA': { 'attribute_type': 'Float', 'value': 'abc' }, 'keyB': { 'attribute_type': 'Integer', 'value': 3 } } } self.demo_element_data_w_bounds = { 'id': 'my_identifier', 'attributes': { 'pvalue': { 'attribute_type': 'BoundedFloat', 'value': 0.1, 'min': 0.0, 'max': 1.0 }, 'keyB': { 'attribute_type': 'Integer', 'value': 3 } } } self.demo_element_w_bounds = Observation('my_identifier', { 'pvalue': bounded_float_attr, 'keyB': int_attr }) self.bad_demo_element_data_w_bounds = { 'id': 'my_identifier', 'attributes': { 'pvalue': { 'attribute_type': 'BoundedFloat', 'value': 1.1, # out of bounds!! 'min': 0.0, 'max': 1.0 }, 'keyB': { 'attribute_type': 'Integer', 'value': 3 } } } # the class that will execute the tests self.tester_class = ElementSerializerTester(ObservationSerializer)