def test_did_exit_state_callback_is_invoked(self): did_exit_mock = Mock() callbacks = StateCallbacks(did_exit_state=did_exit_mock) start_state = State(name='start', callbacks=callbacks) busy_state = State(name='busy') do_work_event = Event(name='do_work', source_states=[start_state], destination_state=busy_state) fsm = StateMachine(states=[start_state, busy_state], events=[do_work_event], initial_state=start_state) fsm.activate() fsm.fire_event(do_work_event) did_exit_mock.assert_called()
def test_firing_event_successfully(self): start_state = State(name='start') busy_state = State(name='busy') do_work_event = Event(name='do_work', source_states=[start_state], destination_state=busy_state) fsm = StateMachine(states=[start_state, busy_state], events=[do_work_event], initial_state=start_state) fsm.activate() assert fsm.fire_event(do_work_event)
def test_firing_event_that_is_declined(self): def decline_event(transition): return False start_state = State(name='start') busy_state = State(name='busy') do_work_info = EventCallbacks(should_fire_event=decline_event) do_work_event = Event(name='do_work', source_states=[start_state], destination_state=busy_state, callbacks=do_work_info) fsm = StateMachine(states=[start_state, busy_state], events=[do_work_event], initial_state=start_state) fsm.activate() with pytest.raises(StateMachineEventError) as context_manager: fsm.fire_event(do_work_event) expected_exception = context_manager.value assert expected_exception.identifier == STATE_MACHINE.DECLINED_EVENT_IDENTIFIER
def test_event_did_fire_callback(self): start_state = State(name='start') busy_state = State(name='busy') did_fire_mock = Mock() do_work_info = EventCallbacks(did_fire_event=did_fire_mock) do_work_event = Event(name='do_work', source_states=[start_state], destination_state=busy_state, callbacks=do_work_info) fsm = StateMachine(states=set([start_state, busy_state]), events=set([do_work_event]), initial_state=start_state) fsm.activate() assert fsm.is_active assert fsm.fire_event(do_work_event) did_fire_mock.assert_called()
def test_event_should_fire_callback(self): start_state = State(name='start') busy_state = State(name='busy') def should_fire_callback(transition): return True do_work_info = EventCallbacks(should_fire_event=should_fire_callback) do_work_event = Event(name='do_work', source_states=[start_state], destination_state=busy_state, callbacks=do_work_info) fsm = StateMachine(states=set([start_state, busy_state]), events=set([do_work_event]), initial_state=start_state) fsm.activate() assert fsm.is_active assert fsm.fire_event(do_work_event)