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 }], })
class TestGraphLinkEntities(TestCase, AssertEntityMixin): def setUp(self): self.graph = Graph() self.entity_1 = Entity(1, 'E1') self.entity_2 = Entity(2, 'E2') self.graph.add_entity(self.entity_1) self.graph.add_entity(self.entity_2) def test_link_entities_when_arguments_are_none(self): self.graph.link_entities(self.entity_1, None) self.graph.link_entities(None, self.entity_2) self.graph.link_entities(None, None) self.assert_links(self.entity_1) self.assert_links(self.entity_2) def test_link_entities(self): self.graph.link_entities(self.entity_1, self.entity_2) self.assert_links(self.entity_1, successors=set([self.entity_2])) self.assert_links(self.entity_2, predecessors=set([self.entity_1])) def test_link_entities_by_id(self): self.graph.link_entities_by_id(1, 2) self.assert_links(self.entity_1, successors=set([self.entity_2])) self.assert_links(self.entity_2, predecessors=set([self.entity_1]))
class TestGraphCopyAndAddEntity(TestCase): def setUp(self): self.graph = Graph() self.entity_1 = Entity(1, 'E1') self.graph.add_entity(self.entity_1) def test_copy_and_add_entity(self): self.assertDictEqual(self.graph.entities, {1: self.entity_1}) self.assertEqual(self.graph.next_entity_id, 2) self.graph.copy_and_add_entity(self.entity_1) self.assertEqual(len(self.graph.entities), 2) self.assertEqual(self.graph.entities[2].id, 2) self.assertEqual(self.graph.entities[2].name, 'E1') self.assertEqual(self.graph.next_entity_id, 3)
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': [], })
class TestGraphAddEntity(TestCase): def setUp(self): self.graph = Graph() def test_add_entity_when_none_is_passed(self): self.assertDictEqual(self.graph.entities, {}) self.graph.add_entity(None) self.assertDictEqual(self.graph.entities, {}) def test_add_entity_when_graph_contains_no_entities(self): self.assertDictEqual(self.graph.entities, {}) new_entity = Entity(1, 'Entity_1') self.graph.add_entity(new_entity) self.assertDictEqual(self.graph.entities, {1: new_entity}) def test_add_entity_when_graph_contains_another_entity(self): entity_1 = Entity(1, 'Entity_1') self.graph.entities = {1: entity_1} self.assertDictEqual(self.graph.entities, {1: entity_1}) new_entity = Entity(2, 'Entity_2') self.graph.add_entity(new_entity) self.assertDictEqual(self.graph.entities, {1: entity_1, 2: new_entity}) def test_add_entity_when_graph_contains_another_entity_with_same_id(self): old_entity = Entity(1, 'old') self.graph.entities = {1: old_entity} self.assertDictEqual(self.graph.entities, {1: old_entity}) new_entity = Entity(1, 'new') self.graph.add_entity(new_entity) self.assertDictEqual(self.graph.entities, {1: new_entity}) def test_add_entity_sets_next_entity_id(self): self.assertEqual(self.graph.next_entity_id, 1) new_entity = Entity(1, 'Entity_1') self.graph.add_entity(new_entity) self.assertEqual(self.graph.next_entity_id, 2) def test_add_entity_doesnt_set_next_entity_id_when_added_entity_has_smaller_value( self): self.graph.next_entity_id = 3 self.assertEqual(self.graph.next_entity_id, 3) new_entity = Entity(1, 'Entity_1') self.graph.add_entity(new_entity) self.assertEqual(self.graph.next_entity_id, 3)