def test_from_dict_when_entity_has_multiple_links(self): graph = Graph.from_dict({ 'entities': [{ 'entity_id': 1, 'name': 'E1' }, { 'entity_id': 2, 'name': 'E2' }, { 'entity_id': 3, 'name': 'E3' }], 'links': [ { 'from': 1, 'to': 3 }, { 'from': 2, 'to': 3 }, ] }) e1 = graph.entities[1] e2 = graph.entities[2] e3 = graph.entities[3] self.assert_links(e1, successors=set([e3])) self.assert_links(e2, successors=set([e3])) self.assert_links(e3, predecessors=set([e1, e2]))
def test_from_dict_when_entities_are_linked_in_loop(self): graph = Graph.from_dict({ 'entities': [{ 'entity_id': 1, 'name': 'E1' }, { 'entity_id': 2, 'name': 'E2' }, { 'entity_id': 3, 'name': 'E3' }], 'links': [{ 'from': 1, 'to': 2 }, { 'from': 2, 'to': 3 }, { 'from': 3, 'to': 1 }] }) e1 = graph.entities[1] e2 = graph.entities[2] e3 = graph.entities[3] self.assert_links(e1, successors=set([e2]), predecessors=set([e3])) self.assert_links(e2, successors=set([e3]), predecessors=set([e1])) self.assert_links(e3, successors=set([e1]), predecessors=set([e2]))
def test_graph_clone_when_provided_entity_is_isolated(self): graph = Graph.from_dict({ 'entities': [ { 'entity_id': 1, 'name': 'E1' }, { 'entity_id': 2, 'name': 'E2' }, { 'entity_id': 3, 'name': 'E3' }, ], 'links': [{ 'from': 1, 'to': 2 }] }) graph.clone(3) self.assert_graph_dict( graph.to_dict(), { 'entities': [ { 'entity_id': 1, 'name': 'E1' }, { 'entity_id': 2, 'name': 'E2' }, { 'entity_id': 3, 'name': 'E3' }, { 'entity_id': 4, 'name': 'E3' }, ], 'links': [ { 'from': 1, 'to': 2 }, ] })
def test_from_dict_when_links_does_not_exist_in_dict(self): graph = Graph.from_dict({ 'entities': [{ 'entity_id': 1, 'name': 'E1' }, { 'entity_id': 2, 'name': 'E2' }] }) self.assertEqual(len(graph.entities), 2) self.assertListEqual(list(graph.entities.keys()), [1, 2]) self.assert_links(graph.entities[1]) self.assert_links(graph.entities[2])
def process(input_file, entity_id, sort_keys_and_objects=False): """ Function that processes input and returns string to be written in stdout. :param input_file: input file :param entity_id: root entity id to start cloning from :param sort_keys_and_objects: if set to True, enables sorting of keys and objects in output json """ graph = Graph.from_dict(from_json_file(input_file), sort_links=sort_keys_and_objects) graph.clone(entity_id) return json.dumps(graph.to_dict(), indent=4, sort_keys=sort_keys_and_objects)
def test_graph_clone_when_provided_id_exists_in_graph(self): graph = Graph.from_dict({ 'entities': [{ 'entity_id': 1, 'name': 'E1' }, { 'entity_id': 2, 'name': 'E2', 'description': 'test' }], 'links': [{ 'from': 1, 'to': 2 }] }) graph.clone(2) self.assert_graph_dict( graph.to_dict(), { 'entities': [ { 'entity_id': 1, 'name': 'E1' }, { 'entity_id': 2, 'name': 'E2', 'description': 'test' }, { 'entity_id': 3, 'name': 'E2', 'description': 'test' }, ], 'links': [ { 'from': 1, 'to': 3 }, { 'from': 1, 'to': 2 }, ] })
def test_graph_clone_when_provided_id_doesnt_exist_in_graph(self): input_dict = { 'entities': [{ 'entity_id': 1, 'name': 'E1' }, { 'entity_id': 2, 'name': 'E2' }], 'links': [{ 'from': 1, 'to': 2 }] } graph = Graph.from_dict(input_dict) graph.clone(3) self.assert_graph_dict(graph.to_dict(), input_dict)
def test_from_dict_when_two_groups_exist(self): graph = Graph.from_dict({ 'entities': [ # Group 1 { 'entity_id': 1, 'name': 'E1' }, { 'entity_id': 2, 'name': 'E2' }, # Group 2 { 'entity_id': 3, 'name': 'E3' }, { 'entity_id': 4, 'name': 'E4' }, ], 'links': [ # Group 1 { 'from': 1, 'to': 2 }, # Group 2 { 'from': 3, 'to': 4 }, ] }) e1 = graph.entities[1] e2 = graph.entities[2] e3 = graph.entities[3] e4 = graph.entities[4] self.assert_links(e1, successors=set([e2])) self.assert_links(e2, predecessors=set([e1])) self.assert_links(e3, successors=set([e4])) self.assert_links(e4, predecessors=set([e3]))
def test_graph_clone_when_graph_has_multiple_loops(self): graph = Graph.from_dict({ 'entities': [ { 'entity_id': 1, 'name': 'E1' }, { 'entity_id': 2, 'name': 'E2' }, { 'entity_id': 3, 'name': 'E3' }, { 'entity_id': 4, 'name': 'E4' }, { 'entity_id': 5, 'name': 'E5' }, { 'entity_id': 6, 'name': 'E6' }, ], 'links': [ { 'from': 1, 'to': 2 }, { 'from': 2, 'to': 3 }, { 'from': 3, 'to': 4 }, { 'from': 4, 'to': 2 }, { 'from': 4, 'to': 5 }, { 'from': 5, 'to': 6 }, { 'from': 6, 'to': 4 }, ] }) graph.clone(3) self.assert_graph_dict( graph.to_dict(), { 'entities': [ { 'entity_id': 1, 'name': 'E1' }, { 'entity_id': 2, 'name': 'E2' }, { 'entity_id': 3, 'name': 'E3' }, { 'entity_id': 4, 'name': 'E4' }, { 'entity_id': 5, 'name': 'E5' }, { 'entity_id': 6, 'name': 'E6' }, # Cloned { 'entity_id': 7, 'name': 'E3' }, # Loop 1 { 'entity_id': 8, 'name': 'E4' }, { 'entity_id': 9, 'name': 'E2' }, # Loop 2 { 'entity_id': 10, 'name': 'E5' }, { 'entity_id': 11, 'name': 'E6' }, ], 'links': [ { 'from': 1, 'to': 2 }, { 'from': 2, 'to': 3 }, { 'from': 3, 'to': 4 }, { 'from': 4, 'to': 2 }, { 'from': 4, 'to': 5 }, { 'from': 5, 'to': 6 }, { 'from': 6, 'to': 4 }, # Cloned { 'from': 2, 'to': 7 }, # connects the new subgraph # Loop 1 { 'from': 7, 'to': 8 }, { 'from': 8, 'to': 9 }, { 'from': 9, 'to': 7 }, # Loop 2 { 'from': 8, 'to': 10 }, { 'from': 10, 'to': 11 }, { 'from': 11, 'to': 8 }, ] })
def test_graph_clone_when_provided_entity_is_in_a_loop(self): graph = Graph.from_dict({ 'entities': [ { 'entity_id': 1, 'name': 'E1' }, { 'entity_id': 2, 'name': 'E2' }, { 'entity_id': 3, 'name': 'E3' }, { 'entity_id': 4, 'name': 'E4' }, ], 'links': [{ 'from': 1, 'to': 2 }, { 'from': 2, 'to': 3 }, { 'from': 3, 'to': 4 }, { 'from': 4, 'to': 2 }] }) graph.clone(3) self.assert_graph_dict( graph.to_dict(), { 'entities': [ { 'entity_id': 1, 'name': 'E1' }, { 'entity_id': 2, 'name': 'E2' }, { 'entity_id': 3, 'name': 'E3' }, { 'entity_id': 4, 'name': 'E4' }, # Cloned { 'entity_id': 5, 'name': 'E3' }, { 'entity_id': 6, 'name': 'E4' }, { 'entity_id': 7, 'name': 'E2' }, ], 'links': [ { 'from': 1, 'to': 2 }, { 'from': 2, 'to': 3 }, { 'from': 3, 'to': 4 }, { 'from': 4, 'to': 2 }, # Cloned { 'from': 2, 'to': 5 }, # connects the new subgraph { 'from': 5, 'to': 6 }, { 'from': 6, 'to': 7 }, { 'from': 7, 'to': 5 }, ] })
def test_graph_clone_when_provided_entity_has_multiple_links(self): graph = Graph.from_dict( { 'entities': [ { 'entity_id': 1, 'name': 'E1' }, { 'entity_id': 2, 'name': 'E2' }, { 'entity_id': 3, 'name': 'E3' }, { 'entity_id': 4, 'name': 'E4' }, { 'entity_id': 5, 'name': 'E5' }, ], 'links': [{ 'from': 1, 'to': 3 }, { 'from': 2, 'to': 3 }, { 'from': 3, 'to': 4 }, { 'from': 3, 'to': 5 }] }, sort_links=True) graph.clone(3) self.assert_graph_dict( graph.to_dict(), { 'entities': [ { 'entity_id': 1, 'name': 'E1' }, { 'entity_id': 2, 'name': 'E2' }, { 'entity_id': 3, 'name': 'E3' }, { 'entity_id': 4, 'name': 'E4' }, { 'entity_id': 5, 'name': 'E5' }, # Cloned { 'entity_id': 6, 'name': 'E3' }, { 'entity_id': 7, 'name': 'E4' }, { 'entity_id': 8, 'name': 'E5' }, ], 'links': [ { 'from': 1, 'to': 3 }, { 'from': 2, 'to': 3 }, { 'from': 3, 'to': 4 }, { 'from': 3, 'to': 5 }, # Cloned { 'from': 1, 'to': 6 }, { 'from': 2, 'to': 6 }, { 'from': 6, 'to': 7 }, { 'from': 6, 'to': 8 }, ] })
def test_graph_clone_when_graph_is_empty(self): graph = Graph.from_dict({}) graph.clone(1) self.assert_graph_dict(graph.to_dict(), {'entities': [], 'links': []})
def test_from_dict_when_entities_does_not_exist_in_dict(self): graph = Graph.from_dict({'links': [{'from': 1, 'to': 2}]}) self.assertDictEqual(graph.entities, {})