def element_factory(): event_manager = EventManager() event_manager.subscribe(handler) clear_events() factory = ElementFactory(event_manager) yield factory clear_events()
def test_uml_associations(self): from gaphor.UML.event import AssociationUpdated from gaphor.UML.properties import association, derivedunion from gaphor.UML import Element event_manager = EventManager() undo_manager = UndoManager(event_manager) element_factory = ElementFactory(event_manager) class A(Element): is_unlinked = False def unlink(self): self.is_unlinked = True Element.unlink(self) A.a1 = association("a1", A, upper=1) A.a2 = association("a2", A, upper=1) A.b1 = association("b1", A, upper="*") A.b2 = association("b2", A, upper="*") A.b3 = association("b3", A, upper=1) A.derived_a = derivedunion(A, "derived_a", 0, 1, A.a1, A.a2) A.derived_b = derivedunion(A, "derived_b", 0, "*", A.b1, A.b2, A.b3) events = [] @event_handler(AssociationUpdated) def handler(event, events=events): events.append(event) event_manager.subscribe(handler) try: a = element_factory.create(A) undo_manager.begin_transaction() a.a1 = element_factory.create(A) undo_manager.commit_transaction() assert len(events) == 1, events assert events[0].property is A.a1 assert undo_manager.can_undo() undo_manager.undo_transaction() assert not undo_manager.can_undo() assert undo_manager.can_redo() assert len(events) == 2, events assert events[1].property is A.a1 finally: event_manager.unsubscribe(handler) undo_manager.shutdown()
class TransactionTestCase(TestCase): """Test case for transactions with the component registry enabled.""" def setUp(self): """Initialize Gaphor services and register transaction event handlers.""" self.event_manager = EventManager() self.event_manager.subscribe(handle_begins) self.event_manager.subscribe(handle_commits) self.event_manager.subscribe(handle_rollback) del begins[:] del commits[:] del rollbacks[:] def tearDown(self): """Finished with the test case. Unregister event handlers that store transaction events.""" self.event_manager.unsubscribe(handle_begins) self.event_manager.unsubscribe(handle_commits) self.event_manager.unsubscribe(handle_rollback) def test_transaction_commit(self): """Test committing a transaction.""" tx = Transaction(self.event_manager) assert tx._stack, "Transaction has no stack" assert 1 == len(begins), "Incorrect number of TrasactionBegin events" assert 0 == len( commits), "Incorrect number of TransactionCommit events" assert 0 == len( rollbacks), "Incorrect number of TransactionRollback events" tx.commit() assert 1 == len(begins), "Incorrect number of TrasactionBegin events" assert 1 == len( commits), "Incorrect number of TransactionCommit events" assert 0 == len( rollbacks), "Incorrect number of TransactionRollback events" assert not tx._stack, "Transaction stack is not empty" try: tx.commit() except TransactionError: pass else: self.fail("Commit should not have succeeded") def test_transaction_rollback(self): """Test rolling back a transaction.""" tx = Transaction(self.event_manager) assert tx._stack, "Transaction has no stack" assert 1 == len(begins), "Incorrect number of TrasactionBegin events" assert 0 == len( commits), "Incorrect number of TransactionCommit events" assert 0 == len( rollbacks), "Incorrect number of TransactionRollback events" tx.rollback() assert 1 == len(begins), "Incorrect number of TrasactionBegin events" assert 0 == len( commits), "Incorrect number of TransactionCommit events" assert 1 == len( rollbacks), "Incorrect number of TransactionRollback events" assert not tx._stack, "Transaction stack is not empty" def test_transaction_commit_after_rollback(self): """Test committing one transaction after rolling back another transaction.""" tx1 = Transaction(self.event_manager) tx2 = Transaction(self.event_manager) tx2.rollback() tx1.commit() assert 1 == len(begins), "Incorrect number of TrasactionBegin events" assert 0 == len( commits), "Incorrect number of TransactionCommit events" assert 1 == len( rollbacks), "Incorrect number of TransactionRollback events" def test_transaction_stack(self): """Test the transaction stack.""" tx1 = Transaction(self.event_manager) tx2 = Transaction(self.event_manager) try: self.assertRaises(TransactionError, tx1.commit) finally: tx2.rollback() tx1.rollback() def test_transaction_context(self): """Test the transaction context manager.""" with Transaction(self.event_manager) as tx: assert isinstance( tx, Transaction), "Context is not a Transaction instance" assert (Transaction._stack ), "Transaction instance has no stack inside a context" assert not Transaction._stack, "Transaction stack should be empty" def test_transaction_context_error(self): """Test the transaction context manager with errors.""" try: with Transaction(self.event_manager): raise TypeError("transaction error") except TypeError as e: assert "transaction error" == str( e ), "Transaction context manager did no raise correct exception" else: self.fail( "Transaction context manager did not raise exception when it should have" )