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_copy(self): original = Entity(1, 'entity', 'description') copy = original.copy(2) self.assertNotEqual(original, copy) self.assertEqual(copy.id, 2) self.assertEqual(copy.name, 'entity') self.assertEqual(copy.description, 'description')
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_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_predecessor_when_existing_entity_is_passed(self): pred = Entity(2, 'pred') other_pred = Entity(3, 'other_pred') self.entity.predecessors = set([pred, other_pred]) self.assert_links(self.entity, predecessors=set([pred, other_pred])) self.entity.add_predecessor(pred) self.assert_links(self.entity, predecessors=set([pred, other_pred]))
def test_add_successor_when_existing_entity_is_passed(self): succ = Entity(2, 'succ') other_succ = Entity(3, 'other_succ') self.entity.successors = set([succ, other_succ]) self.assert_links(self.entity, successors=set([succ, other_succ])) self.entity.add_successor(succ) self.assert_links(self.entity, successors=set([succ, other_succ]))
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_predecessor_when_new_entity_is_passed(self): self.assert_links(self.entity) pred = Entity(2, 'pred') self.entity.add_predecessor(pred) self.assert_links(self.entity, predecessors=set([pred]))
def test_add_successor_when_new_entity_is_passed(self): self.assert_links(self.entity) succ = Entity(2, 'succ') self.entity.add_successor(succ) self.assert_links(self.entity, successors=set([succ]))
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)
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': [], })
def setUp(self): self.entity = Entity(1, 'entity')
class TestEntity(TestCase, AssertEntityMixin): def setUp(self): self.entity = Entity(1, 'entity') def test_add_successor_when_none_is_passed(self): self.assert_links(self.entity) self.entity.add_successor(None) self.assert_links(self.entity) def test_add_successor_when_new_entity_is_passed(self): self.assert_links(self.entity) succ = Entity(2, 'succ') self.entity.add_successor(succ) self.assert_links(self.entity, successors=set([succ])) def test_add_successor_when_existing_entity_is_passed(self): succ = Entity(2, 'succ') other_succ = Entity(3, 'other_succ') self.entity.successors = set([succ, other_succ]) self.assert_links(self.entity, successors=set([succ, other_succ])) self.entity.add_successor(succ) self.assert_links(self.entity, successors=set([succ, other_succ])) def test_add_predecessor_when_none_is_passed(self): self.assert_links(self.entity) self.entity.add_predecessor(None) self.assert_links(self.entity) def test_add_predecessor_when_new_entity_is_passed(self): self.assert_links(self.entity) pred = Entity(2, 'pred') self.entity.add_predecessor(pred) self.assert_links(self.entity, predecessors=set([pred])) def test_add_predecessor_when_existing_entity_is_passed(self): pred = Entity(2, 'pred') other_pred = Entity(3, 'other_pred') self.entity.predecessors = set([pred, other_pred]) self.assert_links(self.entity, predecessors=set([pred, other_pred])) self.entity.add_predecessor(pred) self.assert_links(self.entity, predecessors=set([pred, other_pred])) def test_copy(self): original = Entity(1, 'entity', 'description') copy = original.copy(2) self.assertNotEqual(original, copy) self.assertEqual(copy.id, 2) self.assertEqual(copy.name, 'entity') self.assertEqual(copy.description, 'description')
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_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)