Exemplo n.º 1
0
    def test_event_object_repr_method(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)
        pattern = re.compile(r'\s0x.[a-z0-9]*')
        match = pattern.search(do_work_event.__repr__())
        result = do_work_event.__repr__().replace(match.group(0), '')

        assert result == '<Event> Do Work'
Exemplo n.º 2
0
    def test_initializing_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)

        assert do_work_event.name == 'do_work'
Exemplo n.º 3
0
    def test_event_object_str_method(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)

        assert str(do_work_event) == '<Event Do Work>'
Exemplo n.º 4
0
 def test_can_fire_event_with_name_failure(self):
     start_state = State(name='start')
     busy_state  = State(name='busy')
     do_work_event = Event(name='do_work', source_states=[busy_state], destination_state=busy_state)
     
     fsm = StateMachine(states=[start_state, busy_state], events=[do_work_event], initial_state=start_state)
     fsm.activate()
     
     assert fsm.can_fire_event_with_name('do_work') == False
Exemplo n.º 5
0
    def test_initializing_event_failure_with_invalid_event(self):
        with pytest.raises(StateMachineEventError) as context:
            do_work_event = Event(name='',
                                  source_states=[],
                                  destination_state=None)

        expected_exception = context.value

        assert expected_exception.identifier == STATE_MACHINE.INVALID_EVENT_IDENTIFIER
Exemplo n.º 6
0
 def test_initialize_state_machine_with_invalid_intial_state(self):
     start_state = State(name='start')
     do_work_event = Event(name='do_work', source_states=[start_state], destination_state=start_state)
     
     with pytest.raises(StateMachineError) as context_manager:
         fsm = StateMachine(states=[start_state], events=[do_work_event], initial_state=None)
     
     expected_exception = context_manager.value
     assert expected_exception.identifier == STATE_MACHINE.DOES_NOT_HAVE_INITIAL_STATE_IDENTIFIER
Exemplo n.º 7
0
 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)
Exemplo n.º 8
0
 def test_initial_state_is_not_a_valid_state(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)
     
     with pytest.raises(StateMachineError) as context_manager:
         fsm = StateMachine(states=[start_state], events=[do_work_event], initial_state=busy_state)
     
     expected_exception = context_manager.value
     assert expected_exception.identifier == STATE_MACHINE.INITIAL_STATE_MUST_BE_ONE_OF_PROVIDED_STATES_IDENTIFIER
Exemplo n.º 9
0
 def test_looking_up_event_by_name_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)
     
     found_event = fsm.event_by_name('do_work')
     
     assert found_event == do_work_event
Exemplo n.º 10
0
 def test_looking_up_state_by_name_returning_not_found(self):
     start_state = State(name='start')
     do_work_event = Event(name='do_work', source_states=[start_state], destination_state=start_state)
     
     fsm = StateMachine(states=[start_state], events=[do_work_event], initial_state=start_state)
     
     with pytest.raises(StateMachineError) as context_manager:
         fsm.state_by_name('busy')
     
     expected_exception = context_manager.value
     assert expected_exception.identifier == STATE_MACHINE.STATE_NOT_FOUND_IDENTIFIER
Exemplo n.º 11
0
 def test_state_machine_is_in_correct_state(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=set([start_state, busy_state]), 
                        events=set([do_work_event]), 
                        initial_state=start_state)
     
     fsm.activate()
     
     assert fsm.in_state(start_state)
Exemplo n.º 12
0
 def test_in_state_by_name_for_wrong_current_state(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=set([start_state, busy_state]), 
                        events=set([do_work_event]), 
                        initial_state=start_state)
     
     fsm.activate()
     
     assert fsm.in_state_by_name('busy') == False
Exemplo n.º 13
0
    def test_initializing_event_failure_with_invalid_source_state(self):
        start_state = State(name='start')
        busy_state = State(name='busy')

        with pytest.raises(StateMachineEventError) as context:
            do_work_event = Event(name='do_work',
                                  source_states=['A', 'B', start_state],
                                  destination_state=busy_state)

        expected_exception = context.value

        assert expected_exception.identifier == STATE_MACHINE.INVALID_EVENT_SOURCE_STATES_IDENTIFIER
Exemplo n.º 14
0
 def test_firing_event_with_name_with_invalid_event_name(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=busy_state)
     fsm.activate()
     
     with pytest.raises(StateMachineEventError) as context:
         fsm.fire_event_with_name('do_work')
     
     expected_exception = context.value
     assert expected_exception.identifier == STATE_MACHINE.INVALID_EVENT_IDENTIFIER
Exemplo n.º 15
0
    def test_did_enter_state_callback_is_invoked(self):
        did_enter_mock = Mock()
        callbacks = StateCallbacks(did_enter_state=did_enter_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()

        did_enter_mock.assert_called()
Exemplo n.º 16
0
 def test_in_state_by_name_for_an_invalid_state(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=set([start_state, busy_state]), 
                        events=set([do_work_event]), 
                        initial_state=start_state)
     
     fsm.activate()
     
     with pytest.raises(StateMachineError) as context:
         fsm.in_state_by_name('stop')
     
     expected_exception = context.value
     assert expected_exception.identifier == STATE_MACHINE.STATE_NOT_FOUND_IDENTIFIER
Exemplo n.º 17
0
    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()
Exemplo n.º 18
0
    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)
Exemplo n.º 19
0
 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