def test_one_callback(self): action = Action('test', sources=['draft'], callbacks=[disable_invoice]) self.assertTrue(self.invoice.is_available) state = State(self.invoice, 'status') action.change_state(state) self.assertEqual(self.invoice.status, 'draft') self.assertFalse(self.invoice.is_available) self.assertFalse(state.is_locked())
def test_many_side_effects(self): action = Action('test', sources=['draft'], side_effects=[disable_invoice, enable_invoice]) self.assertTrue(self.invoice.is_available) state = State(self.invoice, 'status') action.change_state(state) self.assertEqual(self.invoice.status, 'draft') self.assertTrue(self.invoice.is_available) self.assertFalse(state.is_locked())
def test_many_side_effects(self): transition = Transition('test', sources=[], target='cancelled', side_effects=[disable_invoice, enable_invoice]) self.assertTrue(self.invoice.is_available) state = State(self.invoice, 'status') transition.change_state(state) self.assertEqual(self.invoice.status, transition.target) self.assertTrue(self.invoice.is_available) self.assertFalse(state.is_locked())
def test_one_callback(self): transition = Transition('test', sources=[], target='cancelled', callbacks=[disable_invoice]) self.assertTrue(self.invoice.is_available) state = State(self.invoice, 'status') transition.change_state(state) self.assertEqual(self.invoice.status, transition.target) self.assertFalse(self.invoice.is_available) self.assertFalse(state.is_locked())
def test_failure_during_callbacks_with_failed_state(self): action = Action( 'test', failed_state='failed', sources=['draft'], side_effects=[disable_invoice, fail_invoice, enable_invoice]) self.assertTrue(self.invoice.is_available) state = State(self.invoice, 'status') action.change_state(state) self.assertEqual(self.invoice.status, 'failed') self.assertFalse(self.invoice.is_available) self.assertFalse(state.is_locked())
def test_failure_during_side_effect_with_failed_state(self): transition = Transition( 'test', sources=[], target='cancelled', failed_state='failed', side_effects=[disable_invoice, fail_invoice, enable_invoice]) self.assertTrue(self.invoice.is_available) state = State(self.invoice, 'status') transition.change_state(state) self.assertEqual(self.invoice.status, 'failed') self.assertFalse(self.invoice.is_available) self.assertFalse(state.is_locked())
def test_one_callback(self): transition = Transition('test', sources=[], target='success', side_effects=[fail_invoice], failure_callbacks=[disable_invoice], failed_state='failed') self.assertTrue(self.invoice.is_available) state = State(self.invoice, 'status') transition.change_state(state) self.assertEqual(self.invoice.status, 'failed') self.assertFalse(self.invoice.is_available) self.assertFalse(state.is_locked())
def test_many_callback(self): action = Action('test', side_effects=[fail_invoice], sources=['draft'], failure_callbacks=[disable_invoice, receive_invoice], failed_state='failed') self.assertTrue(self.invoice.is_available) self.assertFalse(self.invoice.customer_received) state = State(self.invoice, 'status') action.change_state(state) self.assertEqual(self.invoice.status, 'failed') self.assertFalse(self.invoice.is_available) self.assertTrue(self.invoice.customer_received) self.assertFalse(state.is_locked())
def test_callbacks_with_parameters(self): update_invoice(self.invoice, is_available=True, customer_received=True) action = Action('test', failed_state='failed', sources=['draft'], callbacks=[update_invoice]) self.invoice.refresh_from_db() self.assertTrue(self.invoice.is_available) self.assertTrue(self.invoice.customer_received) state = State(self.invoice, 'status') action.change_state(state, is_available=False, customer_received=False) self.invoice.refresh_from_db() self.assertFalse(self.invoice.is_available) self.assertFalse(self.invoice.customer_received) self.assertFalse(state.is_locked())
def test_side_effect_with_parameters(self): update_invoice(self.invoice, is_available=True, customer_received=True) transition = Transition('test', sources=[], target='cancelled', failed_state='failed', side_effects=[update_invoice]) self.invoice.refresh_from_db() self.assertTrue(self.invoice.is_available) self.assertTrue(self.invoice.customer_received) state = State(self.invoice, 'status') transition.change_state(state, is_available=False, customer_received=False) self.invoice.refresh_from_db() self.assertFalse(self.invoice.is_available) self.assertFalse(self.invoice.customer_received) self.assertFalse(state.is_locked())
def test_failure_callback_exception_passed(self, debug_mock): update_invoice(self.invoice, is_available=True, customer_received=True) action = Action('test', failed_state='failed', side_effects=[fail_invoice], sources=['draft'], failure_callbacks=[debug_action]) self.invoice.refresh_from_db() state = State(self.invoice, 'status') action.change_state(state, foo="bar") self.assertTrue(debug_mock.called) self.assertEqual(debug_mock.call_count, 1) call_args = debug_mock.call_args[0] call_kwargs = debug_mock.call_args[1] self.assertEqual(call_args, (self.invoice, )) self.assertEqual(len(call_kwargs), 2) self.assertTrue(isinstance(call_kwargs['exception'], Exception)) self.assertEqual(call_kwargs['foo'], 'bar')
def setUp(self) -> None: self.state = State(Invoice.objects.create(status='draft'), 'status')