def test_mutex_handler_begin_first(): trm = TransactionsManager() begins = [] mh = TransactionHandlerMock({"begin": lambda: begins.append("A")}) th = TransactionHandlerMock({"begin": lambda: begins.append("B")}) trm.set_mutex_handler(mh) trm.add_transaction_handler(th) with trm.begin(): assert begins == ["A", "B"]
def test_mutex_handler_commit_last(): trm = TransactionsManager() commits = [] mh = TransactionHandlerMock({"commit": lambda: commits.append("A")}) th = TransactionHandlerMock({"commit": lambda: commits.append("B")}) trm.set_mutex_handler(mh) trm.add_transaction_handler(th) with trm.begin(): trm.commit() assert commits == ["B", "A"]
def test_mutex_handler_rollback_last(): trm = TransactionsManager() rollbacks = [] mh = TransactionHandlerMock( {"rollback": lambda: rollbacks.append("A")}) th = TransactionHandlerMock( {"rollback": lambda: rollbacks.append("B")}) trm.set_mutex_handler(mh) trm.add_transaction_handler(th) with trm.begin(): trm.rollback() assert rollbacks == ["B", "A"]
def test_mutex_handler_begin_exception(): trm = TransactionsManager() begins = [] mh = TransactionHandlerMock({"begin": lambda: begins.append("A")}) trm.set_mutex_handler(mh) th = TransactionHandlerMock( {"begin": MagicMock(side_effect=Exception())}) trm.add_transaction_handler(th) try: with trm.begin(): pass except Exception: pass assert begins == ["A"]
def test_mutex_handler_rollback_exception(): trm = TransactionsManager() rollbacks = [] th = TransactionHandlerMock( {"rollback": lambda: rollbacks.append("A")}) trm.add_transaction_handler(th) mh = TransactionHandlerMock( {"rollback": MagicMock(side_effect=Exception())}) trm.set_mutex_handler(mh) try: with trm.begin(): trm.rollback() except Exception: pass assert rollbacks == ["A"] assert mh.rollback.called
def test_add_mutex_handler_begun(): trm = TransactionsManager() with trm.begin(): with pytest.raises(TransactionException): trm.set_mutex_handler(TransactionHandlerMock())
def test_add_mutex_handler(): trm = TransactionsManager() th = TransactionHandlerMock() trm.set_mutex_handler(th) assert trm._mutex_handler == th