def test_get_data_type_for_attributes_two_types(self): ne = NetworkEdge() # try various combos of <type> and None for a in [(None, 'string'), ('string', None), (None, 'list_of_string'), ('list_of_string', None), ('boolean', None), (None, 'boolean'), ('list_of_integer', None), (None, 'list_of_double')]: attrs = [Attribute(data_type=a[0]), Attribute(data_type=a[1])] self.assertEqual('list_of_string', ne._get_data_type_for_attributes(attrs)) # try the other types for a in [('string', 'list_of_string'), ('list_of_string', 'string'), ('boolean', 'list_of_boolean'), ('list_of_boolean', 'boolean'), ('double', 'list_of_double'), ('list_of_double', 'double'), ('integer', 'list_of_integer'), ('list_of_integer', 'integer'), ('long', 'list_of_long'), ('list_of_long', 'long')]: attrs = [Attribute(data_type=a[0]), Attribute(data_type=a[1])] if a[0].startswith('list_of'): checkval = a[0] else: checkval = 'list_of_' + a[0] self.assertEqual(checkval, ne._get_data_type_for_attributes(attrs))
def test_remove_identical_attributes_two_diff_attrs(self): ne = NetworkEdge() # test where there is 1 attribute attrs = [ Attribute(name='foo', value='bar'), Attribute(name='blah', value='yo') ] ne.set_attributes(attrs) self.assertEqual([], ne.remove_identical_attributes()) self.assertEqual(2, len(ne.get_attributes()))
def test_add_attribute_to_node(self): nef = Attribute(name='foo', value='someval') net = NiceCXNetwork() node_one = net.create_node('node1') nef.add_attribute_to_node(net_cx=net, node_id=node_one) e_attrs = net.get_node_attributes(node_one) self.assertEqual(1, len(e_attrs)) self.assertEqual('foo', e_attrs[0]['n']) self.assertEqual('someval', e_attrs[0]['v'])
def test_merge_attributes_test_where_dtype_mixed_string_none(self): ne = NetworkEdge() attrs = [ Attribute(name='x', value='1', data_type='string'), Attribute(name='x', value='2') ] res = ne._merge_attributes(attrs_list=attrs) self.assertEqual('x', res.get_name()) self.assertEqual(2, len(res.get_value())) self.assertTrue('1' in res.get_value()) self.assertTrue('2' in res.get_value()) self.assertEqual('list_of_string', res.get_data_type())
def test_merge_attributes_test_with_str_dtype(self): ne = NetworkEdge() attrs = [ Attribute(name='x', value=['1'], data_type='list_of_string'), Attribute(name='x', value='3') ] res = ne._merge_attributes(attrs_list=attrs) self.assertEqual('x', res.get_name()) self.assertEqual(2, len(res.get_value())) self.assertTrue('1' in res.get_value()) self.assertTrue('3' in res.get_value()) self.assertEqual('list_of_string', res.get_data_type())
def test_remove_identical_attributes_four_attrs_two_same(self): ne = NetworkEdge() # test where there is 1 attribute attrs = [ Attribute(name='foo', value='bar'), Attribute(name='blah', value='yo'), Attribute(name='blah', value='yo'), Attribute(name='ha', value='well') ] ne.set_attributes(attrs) merged = ne.remove_identical_attributes() self.assertEqual(1, len(merged)) self.assertEqual('blah', merged[0].get_name()) self.assertEqual(3, len(ne.get_attributes()))
def test_add_attribute_to_edge(self): nef = Attribute(name='foo', value='someval') net = NiceCXNetwork() node_one = net.create_node('node1') node_two = net.create_node('node2') edge_id = net.create_edge(edge_source=node_one, edge_target=node_two, edge_interaction='activates') nef.add_attribute_to_edge(net_cx=net, edge_id=edge_id) e_attrs = net.get_edge_attributes(edge_id) self.assertEqual(1, len(e_attrs)) self.assertEqual('foo', e_attrs[0]['n']) self.assertEqual('someval', e_attrs[0]['v'])
def test_merge_attributes_test_with_integer_dtype(self): ne = NetworkEdge() attrs = [ Attribute(name='x', value=1, data_type='integer'), Attribute(name='x', value=[2, 3], data_type='list_of_integer') ] res = ne._merge_attributes(attrs_list=attrs) self.assertEqual('x', res.get_name()) self.assertEqual(3, len(res.get_value())) self.assertTrue(1 in res.get_value()) self.assertTrue(2 in res.get_value()) self.assertTrue(3 in res.get_value()) self.assertEqual('list_of_integer', res.get_data_type())
def test_get_data_type_for_attributes_single_type(self): ne = NetworkEdge() # single None attrs = [Attribute(data_type=None)] self.assertEqual('list_of_string', ne._get_data_type_for_attributes(attrs)) # multiple None attrs = [Attribute(data_type=None), Attribute(data_type=None)] self.assertEqual('list_of_string', ne._get_data_type_for_attributes(attrs)) # single other types for a in ['string', 'boolean', 'double', 'integer', 'long']: attrs = [Attribute(data_type=a)] self.assertEqual('list_of_' + a, ne._get_data_type_for_attributes(attrs)) # multiple of matching types for a in [ 'string', 'boolean', 'double', 'integer', 'long', 'list_of_boolean', 'list_of_double', 'list_of_integer', 'list_of_long', 'list_of_string' ]: attrs = [ Attribute(data_type=a), Attribute(data_type=a), Attribute(data_type=a) ] if a.startswith('list_of'): checkval = a else: checkval = 'list_of_' + a self.assertEqual(checkval, ne._get_data_type_for_attributes(attrs))
def test_remove_identical_attributes_five_attrs_two_pair_same(self): ne = NetworkEdge() # test where there is 5 attribute attrs = [ Attribute(name='x', value='xval'), Attribute(name='a', value='bar'), Attribute(name='blah', value='yo'), Attribute(name='a', value='bar'), Attribute(name='blah', value='yo') ] ne.set_attributes(attrs) merged = ne.remove_identical_attributes() self.assertEqual(2, len(merged)) merged_anames = set(x.get_name() for x in merged) self.assertTrue('a' in merged_anames) self.assertTrue('blah' in merged_anames) self.assertEqual(2, len(merged_anames)) self.assertEqual(3, len(ne.get_attributes()))
def test_get_edge_key(self): ne = NetworkEdge(source_node_id=1, target_node_id=2, source_node_name='src', target_node_name='target', interaction='activates') remover = ProteinFamilyNodeMemberRemover() # try with no attributes e_key, d_val = remover._get_edge_key(ne) self.assertEqual('s=1, t=2, i=activates, directed=False', e_key) self.assertEqual(False, d_val) e_key, d_val = remover._get_edge_key(ne, flip_src_target=True) self.assertEqual('s=2, t=1, i=activates, directed=False', e_key) self.assertEqual(False, d_val) # try with attributes but no match ne.set_attributes([Attribute(name='foo', value='1')]) e_key, d_val = remover._get_edge_key(ne) self.assertEqual('s=1, t=2, i=activates, directed=False', e_key) self.assertEqual(False, d_val) e_key, d_val = remover._get_edge_key(ne, flip_src_target=True) self.assertEqual('s=2, t=1, i=activates, directed=False', e_key) self.assertEqual(False, d_val) # try with directed attribute True ne.set_attributes([Attribute(name='directed', value=True, data_type='boolean')]) e_key, d_val = remover._get_edge_key(ne) self.assertEqual('s=1, t=2, i=activates, directed=True', e_key) self.assertEqual(True, d_val) e_key, d_val = remover._get_edge_key(ne, flip_src_target=True) self.assertEqual('s=2, t=1, i=activates, directed=True', e_key) self.assertEqual(True, d_val) # try with directed attribute False ne.set_attributes([Attribute(name='directed', value=False, data_type='boolean')]) e_key, d_val = remover._get_edge_key(ne) self.assertEqual('s=1, t=2, i=activates, directed=False', e_key) self.assertEqual(False, d_val) e_key, d_val = remover._get_edge_key(ne, flip_src_target=True) self.assertEqual('s=2, t=1, i=activates, directed=False', e_key) self.assertEqual(False, d_val)
def test_merge_duplicate_attributes(self): # test merge with None for attributes ne = NetworkEdge() ne.merge_duplicate_attributes() # test merge with only 1 attribute ne.set_attributes([Attribute(name='foo', value='bar')]) ne.merge_duplicate_attributes() self.assertEqual(1, len(ne.get_attributes())) self.assertEqual('foo', ne.get_attributes()[0].get_name()) # test merge with two identical attributes ne.set_attributes([ Attribute(name='foo', value='bar'), Attribute(name='foo', value='bar') ]) ne.merge_duplicate_attributes() self.assertEqual(1, len(ne.get_attributes())) self.assertEqual('foo', ne.get_attributes()[0].get_name()) # test merge with two identical and 1 different ne.set_attributes([ Attribute(name='foo', value='bar'), Attribute(name='foo', value='bar'), Attribute(name='other', value='yo') ]) ne.merge_duplicate_attributes() self.assertEqual(2, len(ne.get_attributes())) a_names = set(x.get_name() for x in ne.get_attributes()) self.assertTrue('foo' in a_names) self.assertTrue('other' in a_names)
def test_add_edge_to_network(self): net = NiceCXNetwork() src_node = net.create_node('node1') target_node = net.create_node('node2') attrs = [] attrs.append(Attribute(name='foo', value='ha')) attrs.append(Attribute(name='foo2', value='ha2', data_type='string')) ne = NetworkEdge(edge_id=1, interaction='activates', attributes=attrs) self.assertEqual(0, len(net.get_edges())) edge_id = ne.add_edge_to_network(net_cx=net, source_node_id=src_node, target_node_id=target_node) self.assertEqual(1, len(net.get_edges())) nef = NetworkEdgeFactory() new_ne = nef.get_network_edge_from_network(net_cx=net, edge_id=edge_id) self.assertEqual('node1', new_ne.get_source_node_name()) self.assertEqual('node2', new_ne.get_target_node_name()) self.assertEqual(src_node, new_ne.get_source_node_id()) self.assertEqual(target_node, new_ne.get_target_node_id()) self.assertEqual('activates', new_ne.get_interaction()) self.assertEqual(2, len(new_ne.get_attributes())) a_dict = {} for attr in new_ne.get_attributes(): a_dict[attr.get_name()] = attr self.assertEqual('ha', a_dict['foo'].get_value()) self.assertEqual('ha2', a_dict['foo2'].get_value()) new_ne.remove_edge_from_network(net_cx=net) self.assertEqual(0, len(net.get_edges()))
def test_get_edge_key(self): remover = ProteinFamilyNodeMemberRemover() # try with 1 edge no attributes ne = NetworkEdge(source_node_id=3, target_node_id=2, interaction='activates') res = remover._get_edge_dict([ne]) self.assertEqual(1, len(res)) key = 's=3, t=2, i=activates, directed=False' self.assertTrue(key in res) self.assertEqual(1, len(res[key])) self.assertTrue(ne.get_source_node_id() == res[key][0].get_source_node_id()) # try 3 edges where two edges have source/target are flipped and # directed is same value ne_one = NetworkEdge(source_node_id=1, target_node_id=2, interaction='activates', attributes=[Attribute(name='directed', value=False, data_type='boolean')]) ne_two = NetworkEdge(source_node_id=2, target_node_id=1, interaction='activates', attributes=[Attribute(name='directed', value=False, data_type='boolean')]) res = remover._get_edge_dict([ne_one, ne_two, ne]) self.assertEqual(2, len(res)) self.assertTrue(key in res) self.assertEqual(1, len(res[key])) self.assertTrue(ne.get_source_node_id() == res[key][0].get_source_node_id()) mkey = 's=1, t=2, i=activates, directed=False' self.assertTrue(mkey in res) self.assertEqual(2, len(res[mkey]))
def test_merge(self): # test against None ne = NetworkEdge() ne.merge_edge(None) # test common scenario ne_one = NetworkEdge(attributes=[ Attribute(name='directed', value=True, data_type='boolean') ]) ne_two = NetworkEdge(attributes=[ Attribute(name='directed', value=True, data_type='boolean') ]) ne.merge_edge(ne_two) self.assertTrue(1, len(ne_one.get_attributes())) self.assertEqual('directed', ne.get_attributes()[0].get_name()) self.assertEqual(True, ne.get_attributes()[0].get_value()) self.assertEqual('boolean', ne.get_attributes()[0].get_data_type()) # test another common scenario ne_one = NetworkEdge(attributes=[ Attribute( name='citation', value=['1', '2'], data_type='list_of_string'), Attribute(name='directed', value=True, data_type='boolean') ]) ne_two = NetworkEdge(attributes=[ Attribute(name='citation', value=['3', '1', '2'], data_type='list_of_string'), Attribute(name='directed', value=True, data_type='boolean') ]) ne.merge_edge(ne_two) self.assertEqual(2, len(ne.get_attributes())) a_names = set(x.get_name() for x in ne.get_attributes()) self.assertTrue('citation' in a_names) self.assertTrue('directed' in a_names) for x in ne.get_attributes(): if x.get_name() == 'citation': self.assertEqual(3, len(x.get_value())) self.assertTrue('1' in x.get_value()) self.assertTrue('2' in x.get_value()) self.assertTrue('3' in x.get_value())
def test_constructor(self): nef = Attribute(name='name', value='value', data_type='data_type') self.assertEqual('name', nef.get_name()) self.assertEqual('value', nef.get_value()) self.assertEqual('data_type', nef.get_data_type())
def test_str(self): nef = Attribute() self.assertEqual('name=None, values=None, type=None', str(nef))
def test_getters_and_setters(self): nef = Attribute() self.assertEqual(None, nef.get_name()) self.assertEqual(None, nef.get_value()) self.assertEqual(None, nef.get_data_type()) self.assertEqual(None, nef.get_equality_fail_reason()) nef.set_name('name') nef.set_value('value') nef.set_data_type('data_type') self.assertEqual('name', nef.get_name()) self.assertEqual('value', nef.get_value()) self.assertEqual('data_type', nef.get_data_type())
def test__eq__(self): # try with self unset nef = Attribute() self.assertTrue(nef == nef) # try where other is none self.assertFalse(nef == None) self.assertEqual('Other is None', nef.get_equality_fail_reason()) # try where names differ nef1 = Attribute(name='name1', value='foo', data_type='string') nef2 = Attribute(name='name2', value='foo', data_type='string') self.assertFalse(nef1 == nef2) self.assertEqual('name1 name does not match name2', nef1.get_equality_fail_reason()) self.assertFalse(nef2 == nef1) self.assertEqual('name2 name does not match name1', nef2.get_equality_fail_reason()) # try where values differ nef1.set_name('name') nef2.set_name(nef1.get_name()) nef1.set_value('foo') nef2.set_value('blah') self.assertFalse(nef1 == nef2) self.assertEqual('foo value does not match blah', nef1.get_equality_fail_reason()) self.assertFalse(nef2 == nef1) self.assertEqual('blah value does not match foo', nef2.get_equality_fail_reason()) # try where one value is list and other is not nef2.set_value(['1', '2']) self.assertFalse(nef1 == nef2) self.assertEqual('foo value does not match [\'1\', \'2\']', nef1.get_equality_fail_reason()) self.assertFalse(nef2 == nef1) self.assertEqual('[\'1\', \'2\'] value does not match foo', nef2.get_equality_fail_reason()) # try where both are lists but differ nef1.set_value(['3', '4']) nef2.set_value(['1', '2']) self.assertFalse(nef1 == nef2) self.assertEqual("['3', '4'] value does not match ['1', '2']", nef1.get_equality_fail_reason()) self.assertFalse(nef2 == nef1) self.assertEqual("['1', '2'] value does not match ['3', '4']", nef2.get_equality_fail_reason()) # try where both lists match nef2.set_value(nef1.get_value()) self.assertTrue(nef1 == nef2) self.assertEqual(None, nef1.get_equality_fail_reason()) self.assertTrue(nef2 == nef1) self.assertEqual(None, nef2.get_equality_fail_reason()) # try where one data type is none and other is str nef1.set_data_type('string') nef2.set_data_type(None) self.assertTrue(nef1 == nef2) self.assertEqual(None, nef1.get_equality_fail_reason()) self.assertTrue(nef2 == nef1) self.assertEqual(None, nef2.get_equality_fail_reason()) # try where data types differ nef1.set_data_type('string') nef2.set_data_type('boolean') self.assertFalse(nef1 == nef2) self.assertEqual('data types differ', nef1.get_equality_fail_reason()) self.assertFalse(nef2 == nef1) self.assertEqual('data types differ', nef2.get_equality_fail_reason())