def test_tsm_after_transactions(self): callable_func_mock = MagicMock() callable_func_mock.side_effect = Exception( 'On Exceptions Rollback Error.') callable_func_2_mock = MagicMock() states = [State('1'), State('2', initial=True), State('3')] sm = TransactionalFiniteStateMachine(states=states) with self.assertRaisesRegex(Exception, 'On Exceptions Rollback Error.'): with sm.managed_transition( states[2], pre_transactions=[ Transaction(target=callable_func_mock, args=(1, 2), kwargs={'test': 'hi'}, rb_transactions=[ Transaction( target=callable_func_2_mock, args=(4, 5), kwargs={'test': 'hello'}) ]) ]): pass self.assertEqual(sm.current_state, states[1]) callable_func_mock.assert_called_once_with(1, 2, test='hi') callable_func_2_mock.assert_called_once_with(4, 5, test='hello')
def test_tsm_on_exception_transactions(self): callable_func_mock = MagicMock() callable_func_mock.side_effect = Exception( 'On Exceptions Rollback Error.') callable_func_2_mock = MagicMock() states = [State('1'), State('2'), State('3')] sm = TransactionalFiniteStateMachine(states=states) with self.assertRaisesRegex(Exception, 'On Exceptions Rollback Error.'): with sm.managed_transition( states[0], on_error_transactions=[ Transaction(target=callable_func_mock, args=(1, 2), kwargs={'test': 'hi'}, rb_transactions=[ Transaction( target=callable_func_2_mock, args=(4, 5), kwargs={'test': 'hello'}) ]) ]): raise Exception('Error during transition') self.assertEqual(sm.current_state, State('UNKNOWN')) callable_func_mock.assert_called_once_with(1, 2, test='hi') callable_func_2_mock.assert_called_once_with(4, 5, test='hello')
def test_tsm_before_transactions_negative(self): callable_func_mock = MagicMock() callable_func_2_mock = MagicMock() callable_func_mock.side_effect = Exception('Failed') states = [ State('1', initial=True, allowed_transitions=['2']), State('2'), State('3') ] sm = TransactionalFiniteStateMachine(states=states) with self.assertRaisesRegex(Exception, 'Failed'): with sm.managed_transition( states[1], pre_transactions=[ Transaction(target=callable_func_mock, args=(1, 2), kwargs={'test': 'hi'}), Transaction(target=callable_func_2_mock, args=(4, 5), kwargs={'test': 'hello'}) ]): pass self.assertEqual(sm.current_state, State('1')) callable_func_mock.assert_called_once_with(1, 2, test='hi') callable_func_2_mock.assert_not_called()
def test_transaction_no_rb(self): callable_func_mock = MagicMock() rb_callable_func_mock = MagicMock() t = Transaction( target=callable_func_mock, rb_transactions=[Transaction(target=rb_callable_func_mock)]) t() callable_func_mock.assert_called_once() rb_callable_func_mock.assert_not_called()
def test_transaction_no_rb_result(self): callable_func_mock = MagicMock() callable_func_mock.return_value = 'func_return' rb_callable_func_mock = MagicMock() t = Transaction( target=callable_func_mock, rb_transactions=[Transaction(target=rb_callable_func_mock)]) t() callable_func_mock.assert_called_once() rb_callable_func_mock.assert_not_called() self.assertEqual(t.result, 'func_return') self.assertEqual(t.error, None)
def test_transaction_repr(self): callable_func_mock = MagicMock() rb_callable_func_mock = MagicMock() t = Transaction( target=callable_func_mock, rb_transactions=[Transaction(target=rb_callable_func_mock)]) t() self.assertEqual( str(t), '<Transaction callable={} args={} kwargs={} rb_conditions={}>'. format(callable_func_mock, (), {}, []))
def test_transaction_rb(self): callable_func_mock = MagicMock() callable_func_mock.side_effect = Exception('Failed') rb_callable_func_mock = MagicMock() t = Transaction( target=callable_func_mock, rb_transactions=[Transaction(target=rb_callable_func_mock)]) with self.assertRaisesRegex(Exception, 'Failed'): t() callable_func_mock.assert_called_once() rb_callable_func_mock.assert_called_once() self.assertEqual(t.result, None) self.assertEqual(type(t.error), type(Exception('Failed')))
def test_tsm_before_transactions_positive(self): callable_func_mock = MagicMock() states = [State('1'), State('2'), State('3')] sm = TransactionalFiniteStateMachine(states=states) with sm.managed_transition(states[0], pre_transactions=[ Transaction(target=callable_func_mock, args=(1, 2), kwargs={'test': 'hi'}) ]): pass self.assertEqual(sm.current_state, states[0]) callable_func_mock.assert_called_once_with(1, 2, test='hi')