Beispiel #1
0
 def test_begin_exception_already_rollbacked():
     trm = TransactionsManager()
     trh1 = TransactionHandlerMock()
     trm.add_transaction_handler(trh1)
     with pytest.raises(WeAreDoomedException):
         with trm.begin():
             trm.rollback()
             raise Exception("I KILL YOU !")
Beispiel #2
0
 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"]
Beispiel #3
0
 def test_begin_exception_exception_during_rollback():
     trm = TransactionsManager()
     trh1 = TransactionHandlerMock(
         {"rollback": MagicMock(side_effect=Exception)})
     trm.add_transaction_handler(trh1)
     with pytest.raises(WeAreDoomedException):
         with trm.begin():
             raise Exception("I KILL YOU !")
     assert len(trm.exceptions_encountered) == 2
Beispiel #4
0
 def test_begin():
     trm = TransactionsManager()
     trh1 = TransactionHandlerMock()
     trh2 = TransactionHandlerMock()
     trm.add_transaction_handler(trh1)
     trm.add_transaction_handler(trh2)
     with trm.begin():
         pass
     assert trh1.begin.called
     assert trh2.begin.called
Beispiel #5
0
 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"]
Beispiel #6
0
 def test_rollback():
     trm = TransactionsManager()
     trh1 = TransactionHandlerMock()
     trh2 = TransactionHandlerMock()
     trm.add_transaction_handler(trh1)
     trm.add_transaction_handler(trh2)
     with trm.begin():
         trm.rollback()
     assert trh1.rollback.called
     assert trh2.rollback.called
Beispiel #7
0
 def test_execute():
     trm = TransactionsManager()
     trh1 = TransactionHandlerMock()
     trh2 = TransactionHandlerMock()
     trm.add_transaction_handler(trh1)
     trm.add_transaction_handler(trh2)
     with trm.begin():
         trm.execute()
     assert trh1.execute.called
     assert trh2.execute.called
Beispiel #8
0
 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"]
Beispiel #9
0
    def test_begin_exception():
        trm = TransactionsManager()
        trh1 = TransactionHandlerMock()
        trm.add_transaction_handler(trh1)

        class TestException(Exception):
            pass

        with pytest.raises(TestException):
            with trm.begin():
                raise TestException("I KILL YOU !")
        assert trh1.rollback.called
Beispiel #10
0
 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"]
Beispiel #11
0
 def test_rollback_exceptions():
     trm = TransactionsManager()
     trh1 = TransactionHandlerMock(
         {"rollback": MagicMock(side_effect=Exception)})
     trh2 = TransactionHandlerMock(
         {"rollback": MagicMock(side_effect=Exception)})
     trm.add_transaction_handler(trh1)
     trm.add_transaction_handler(trh2)
     with pytest.raises(Exception):
         with trm.begin():
             trm.rollback()
     assert trh1.rollback.called
     assert trh2.rollback.called
     assert len(trm.exceptions_encountered) == 2
Beispiel #12
0
 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
Beispiel #13
0
 def test_commit():
     trm = TransactionsManager()
     trh1 = TransactionHandlerMock({
         "can_prepare_commit":
         MagicMock(return_value=True),
         "prepare_commit":
         MagicMock(return_value=True),
     })
     trh2 = TransactionHandlerMock(
         {"can_prepare_commit": MagicMock(return_value=False)})
     trm.add_transaction_handler(trh1)
     trm.add_transaction_handler(trh2)
     with trm.begin():
         trm.commit()
     assert trh1.can_prepare_commit.called
     assert trh2.can_prepare_commit.called
     assert trh1.prepare_commit.called
     assert trh1.commit.called
     assert trh2.commit.called
Beispiel #14
0
 def test_add_transaction_handler_begun():
     trm = TransactionsManager()
     with trm.begin():
         with pytest.raises(TransactionException):
             trm.add_transaction_handler(TransactionHandlerMock())