def assert_has_edge( self: unittest.TestCase, u: BaseEntity, v: BaseEntity, graph: BELGraph, *, only: bool = False, permissive: bool = True, use_identifiers: bool = False, **expected_edge_data ): """A helper function for checking if an edge with the given properties is contained within a graph.""" self.assertIsInstance(u, BaseEntity) self.assertIsInstance(v, BaseEntity) self.assertTrue( graph.has_edge(u, v), msg='Edge ({}, {}) not in graph. Other edges:\n{}'.format(u, v, '\n'.join( edge_to_bel(u, v, d, use_identifiers=use_identifiers) for u, v, d in graph.edges(data=True) )) ) if not expected_edge_data: return if ANNOTATIONS in expected_edge_data: expected_edge_data[ANNOTATIONS] = graph._clean_annotations(expected_edge_data[ANNOTATIONS]) if only: _key, actual_edge_data = list(graph[u][v].items())[0] self.assertEqual(_remove_line(expected_edge_data), _remove_line(actual_edge_data), msg='Only entry not equal') else: actual_dicts = { k: _remove_line(v) for k, v in graph[u][v].items() } if permissive: matches = any_subdict_matches(actual_dicts, _remove_line(expected_edge_data)) else: matches = any_dict_matches(actual_dicts, _remove_line(expected_edge_data)) msg = 'No edge ({}, {}) with correct properties. expected:\n {}\nbut got:\n{}'.format( u, v, dumps(expected_edge_data, indent=2, sort_keys=True), dumps(actual_dicts, indent=2, sort_keys=True), ) self.assertTrue(matches, msg=msg)
def test_all_filter_dict(self): graph = BELGraph() graph.annotation_list['A'] = set('12345') a, b = Protein(namespace='hgnc', identifier='1', name='A'), Protein(namespace='hgnc', identifier='2', name='B') graph.add_increases(a, b, citation=n(), evidence=n(), annotations={ 'A': {'1', '2', '3'}, }) self.assertEqual(1, count_passed_edge_filter(graph, build_annotation_dict_all_filter(graph._clean_annotations({'A': {'1': True}})))) self.assertEqual(1, count_passed_edge_filter(graph, build_annotation_dict_all_filter(graph._clean_annotations({ 'A': {'1': True, '2': True} })))) self.assertEqual(1, count_passed_edge_filter(graph, build_annotation_dict_all_filter(graph._clean_annotations({ 'A': {'1': True, '2': True, '3': True} })))) self.assertEqual(0, count_passed_edge_filter(graph, build_annotation_dict_all_filter(graph._clean_annotations({ 'A': {'1': True, '2': True, '3': True, '4': True} })))) self.assertEqual(0, count_passed_edge_filter(graph, build_annotation_dict_all_filter(graph._clean_annotations({ 'A': {'4': True} }))))
def test_all_filter(self): graph = BELGraph() graph.annotation_list['A'] = set('12345') graph.add_increases(Protein(n(), n()), Protein(n(), n()), citation=n(), evidence=n(), annotations={ 'A': {'1', '2', '3'}, }) self.assertEqual(1, count_passed_edge_filter(graph, build_annotation_dict_all_filter(graph._clean_annotations({'A': {'1'}})))) self.assertEqual(1, count_passed_edge_filter(graph, build_annotation_dict_all_filter(graph._clean_annotations({'A': {'1', '2'}})))) self.assertEqual(1, count_passed_edge_filter(graph, build_annotation_dict_all_filter(graph._clean_annotations({'A': {'1', '2', '3'}})))) self.assertEqual(0, count_passed_edge_filter(graph, build_annotation_dict_all_filter(graph._clean_annotations({'A': {'1', '2', '3', '4'}})))) self.assertEqual(0, count_passed_edge_filter(graph, build_annotation_dict_all_filter(graph._clean_annotations({'A': {'4'}}))))