PageRenderTime 11ms CodeModel.GetById 7ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/one_event_multiple_transitions_spec.py

http://github.com/nsi-iff/fluidity
Python | 53 lines | 38 code | 14 blank | 1 comment | 4 complexity | af4b3c8e48d2f60a13b407ba3ae30fce MD5 | raw file
Possible License(s): MIT
 1import unittest
 2from should_dsl import should, should_not
 3from fluidity import StateMachine, state, transition, ForkedTransition
 4
 5
 6class LoanRequest(StateMachine):
 7    state('pending')
 8    state('analyzing')
 9    state('refused')
10    state('accepted')
11    initial_state = 'pending'
12    transition(from_='pending', event='analyze', to='analyzing', action='input_data')
13    transition(from_='analyzing', event='forward_analysis_result',
14               guard='was_loan_accepted', to='accepted')
15    transition(from_='analyzing', event='forward_analysis_result',
16               guard='was_loan_refused', to='refused')
17
18    def input_data(self, accepted=True):
19        self.accepted = accepted
20
21    def was_loan_accepted(self):
22        return self.accepted or getattr(self, 'truify', False)
23
24    def was_loan_refused(self):
25        return not self.accepted or getattr(self, 'truify', False)
26
27
28class FluidityEventSupportsMultipleTransitions(unittest.TestCase):
29    '''Event chooses one of multiple transitions, based in their guards'''
30
31    def test_it_selects_the_transition_having_a_passing_guard(self):
32        request = LoanRequest()
33        request.analyze()
34        request.forward_analysis_result()
35        request.current_state |should| equal_to('accepted')
36
37        request = LoanRequest()
38        request.analyze(accepted=False)
39        request.forward_analysis_result()
40        request.current_state |should| equal_to('refused')
41
42    def test_it_raises_error_if_more_than_one_guard_passes(self):
43        request = LoanRequest()
44        request.analyze()
45        request.truify = True
46        request.forward_analysis_result |should| throw(
47            ForkedTransition, message="More than one transition was allowed for this event")
48
49
50
51if __name__ == '__main__':
52    unittest.main()
53