Skip to content

Commit

Permalink
Fixes StateTransition.handle_kwargs
Browse files Browse the repository at this point in the history
This fixes StateTransition.handle_kwargs. It must be a class method for
this to work. It adds extra test case to test the handler_kwargs method.
  • Loading branch information
gvangool committed May 30, 2016
1 parent 3874e5a commit 9a136f5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
6 changes: 3 additions & 3 deletions django_states/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,6 @@ def get_name(cls):
"""
return cls.__name__

@property
def handler_kwargs(self):
return self.handler.__code__.co_varnames[3:]
@classmethod
def handler_kwargs(cls):
return cls.handler.__code__.co_varnames[3:]
23 changes: 19 additions & 4 deletions django_states/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,15 @@ class startup(StateTransition):
to_state = 'running'
description = 'Start up the machine!'

class crash(StateTransition):
'''Transition from stopped to running'''
from_states = ['running']
to_state = 'crashed'
description = 'Crash the machine!'

def handler(self, instance, user=None, reason=None):
pass

class working(StateGroup):
states = ['running']

Expand All @@ -343,18 +352,24 @@ class not_runing(StateGroup):

T3Machine.get_transition_from_states('stopped', 'running')
with self.assertRaises(TransitionNotFound):
T3Machine.get_transition_from_states('running', 'crashed')
T3Machine.get_transition_from_states('running', 'stopped')
self.assertTrue(T3Machine.has_transition('startup'))
self.assertFalse(T3Machine.has_transition('crash'))
self.assertFalse(T3Machine.has_transition('stop'))
trion = T3Machine.get_transitions('startup')
self.assertFalse(hasattr(trion, 'from_state'))
self.assertEqual(trion.from_states[0], 'stopped')
self.assertEqual(trion.to_state, 'running')
trion = T3Machine.get_transitions('crash')
self.assertFalse(hasattr(trion, 'from_state'))
self.assertEqual(trion.from_states[0], 'running')
self.assertEqual(trion.to_state, 'crashed')
self.assertEqual(len(trion.handler_kwargs()), 1)
self.assertEqual(trion.handler_kwargs()[0], 'reason')
with self.assertRaises(KeyError):
T3Machine.get_transitions('crash')
T3Machine.get_transitions('stop')
# Admin actions
actions = T3Machine.get_admin_actions()
self.assertEqual(len(actions), 1)
self.assertEqual(len(actions), 2)
action = actions[0]
self.assertEqual(action.__name__, 'state_transition_startup')
self.assertTrue('stopped' in action.short_description)
Expand Down

0 comments on commit 9a136f5

Please sign in to comment.