def test_to_dict_when_entity_has_multiple_links(self): graph = Graph() e1 = Entity(1, 'E1') e2 = Entity(2, 'E2') e3 = Entity(3, 'E3') graph.add_entity(e1) graph.add_entity(e2) graph.add_entity(e3) graph.link_entities(e1, e3) graph.link_entities(e2, e3) self.assertDictEqual(graph.entities, {1: e1, 2: e2, 3: e3}) json_dict = graph.to_dict() self.assert_graph_dict( json_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 }], })
def test_to_dict_when_entities_are_linked_in_loop(self): graph = Graph() e1 = Entity(1, 'E1') e2 = Entity(2, 'E2') graph.add_entity(e1) graph.add_entity(e2) graph.link_entities(e1, e2) graph.link_entities(e2, e1) self.assertDictEqual(graph.entities, {1: e1, 2: e2}) json_dict = graph.to_dict() self.assert_graph_dict( json_dict, { 'entities': [{ 'entity_id': 1, 'name': 'E1' }, { 'entity_id': 2, 'name': 'E2' }], 'links': [{ 'from': 1, 'to': 2 }, { 'from': 2, 'to': 1 }], })
def test_to_dict_when_no_entities_exist(self): graph = Graph() json_dict = graph.to_dict() self.assert_graph_dict(json_dict, { 'entities': [], 'links': [], })
def test_to_dict_when_no_links_exist(self): graph = Graph() e1 = Entity(1, 'E1', 'D1') e2 = Entity(2, 'E2') graph.add_entity(e1) graph.add_entity(e2) self.assertDictEqual(graph.entities, {1: e1, 2: e2}) json_dict = graph.to_dict() self.assert_graph_dict( json_dict, { 'entities': [{ 'entity_id': 1, 'name': 'E1', 'description': 'D1' }, { 'entity_id': 2, 'name': 'E2' }], 'links': [], })