Commits

Ian George committed 708b1e5

Chasing some bugs out of the cancel / redirect stuff and adding a couple of tests

Comments (0)

Files changed (2)

     """
     Raised when a change to a non-existent state is requested.
     """
+    def __init__(self, state):
+        super(FSM_StateDoesNotExist, self).__init__()
+        self.state = state
+
+    def __str__(self):
+        return  "%s does not exist" % self.state.name
 
 
 class FSM_NotAllowed(FSM_Exception):
     """
     Raise in enter state action to redirect to an alternate state
     """
+    def __init__(self, state, message=None):
+        super(FSM_RedirectTransition, self).__init__()
+        self.state = state
+        self.message = message or ""
+
 
 
 class FSM_VerificationError(FSM_Exception):
         except FSM_CancelTransition:
             return self.state
         except FSM_RedirectTransition as e:
-            return self.change(e.state, *args, **kwargs)
+            return self.change(e.state.name, *args, **kwargs)
 
         # run the enter state functions, checking for cancellation or redirection
         try:
         except FSM_CancelTransition:
             return self.state
         except FSM_RedirectTransition as e:
-            return self.change(e.state, *args, **kwargs)
+            return self.change(e.state.name, *args, **kwargs)
 
         # safe to change the state if we get this far
         self.__state = new_state

statemachine/tests.py

     start = fsm.State(exits_to=['step1a', 'step1b', 'step1c'])
     step1a = fsm.State(exits_to=['step2', ])
     step1b = fsm.State(exits_to=['step2', ])
-    step1c = fsm.State(exits_to=['step2', ])
+    step1c = fsm.State(exits_to=['step2', 'end'])
     step2 = fsm.State(exits_to=['end', 'step3', ])
     step3 = fsm.State(exits_to=['end', ])
     end = fsm.State()
 
     def test_cancel_action(self):
         def cancel_change(from_state):
-            if from_state == "step1c":
-                raise fsm.CancelTransition("Didn't want to continue")
-
-        def redirect_change(from_state):
-            if from_state == "step1c":
-                raise fsm.RedirectTransition("end")
+            if from_state.name == "step1c":
+                raise fsm.FSM_CancelTransition("Didn't want to continue")
 
         testmachine = TestStateMachine(field1=100, field2="LALALALA")
         testmachine.save(None)
         testmachine.status.change("step1c")
         testmachine.status.change("step2")
 
-        self.assertEqual(testmachine.status.state.name, "step2")
+        self.assertEqual(testmachine.status.state.name, "step1c")
+
+        testmachine2 = TestStateMachine(field1=100, field2="LALALALA")
+        testmachine2.save(None)
+        testmachine2.status.change("step1b")
+        testmachine2.status.change("step2")
+
+        self.assertEqual(testmachine2.status.state.name, "step2")
+
+
+    def test_redirect_action(self):
+        def redirect_change(from_state):
+            if from_state.name == "step1c":
+                import ipdb; ipdb.set_trace()
+                raise fsm.FSM_RedirectTransition(testmachine.status.states["end"])
+
+        testmachine = TestStateMachine(field1=100, field2="LALALALA")
+        testmachine.save(None)
+        testmachine.status.states["step2"].entry_action = redirect_change
+        testmachine.status.change("step1c")
+        testmachine.status.change("step2")
+       
+        self.assertEqual(testmachine.status.state.name, "end")
+