Skip to content

Commit

Permalink
AbortTransition accepts a singlep param
Browse files Browse the repository at this point in the history
  • Loading branch information
Bibhas committed Mar 8, 2018
1 parent d02803a commit edebe2f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
11 changes: 6 additions & 5 deletions coaster/sqlalchemy/statemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def publish(self):
# success, message = post.publish()
success = False
message = "A transition is not desireable right now"
message = "A transition is not desirable right now"
raise AbortTransition(success, message)
# Any other exception will be raised
Expand Down Expand Up @@ -245,10 +245,11 @@ class StateTransitionError(BadRequest, TypeError):
class AbortTransition(Exception):
"""
Transitions may raise AbortTransition to return without changing state.
The parameters to the exception are returned as the transition’s result.
Accepts a single parameter `message` which is returned as the transition’s result.
Despite the name, `message` can be of any data type.
"""
pass

def __init__(self, message=None):
self.message = message

This comment has been minimized.

Copy link
@jace

jace Mar 8, 2018

Member

Isn't super() necessary?


# --- Classes -----------------------------------------------------------------

Expand Down Expand Up @@ -548,7 +549,7 @@ def __call__(self, *args, **kwargs):
result = self.statetransition.func(self.obj, *args, **kwargs)
except AbortTransition as e:
transition_exception.send(self.obj, transition=self.statetransition, exception=e)

This comment has been minimized.

Copy link
@jace

jace Mar 8, 2018

Member

Since you're passing the exception to a listener, it has to look like a regular exception to whoever is receiving it downstream.

This comment has been minimized.

Copy link
@iambibhas

iambibhas Mar 8, 2018

Contributor

@jace you mean like exception=Exception(e.message)?

This comment has been minimized.

Copy link
@jace

jace Mar 8, 2018

Member

No, exception=e, but the listener may be looking for e.args. You've ensured it will have no args.

This comment has been minimized.

Copy link
@iambibhas

iambibhas Mar 8, 2018

Contributor

@jace I'm adding the super there that fixes it. even if we dont pass args to super, it'll assign the value of message to args. Tested it.

return None if len(e.args) == 0 else e.args[0] if len(e.args) == 1 else e.args
return e.message
except Exception as e:
transition_exception.send(self.obj, transition=self.statetransition, exception=e)
raise
Expand Down
10 changes: 6 additions & 4 deletions tests/test_statemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ def abort(self, success=False, empty_abort=False):
if empty_abort:
raise AbortTransition()
else:
raise AbortTransition(success)
return success
raise AbortTransition((success, 'failed'))
return success, 'passed'

@with_roles(call={'author'})
@state.transition(state.PUBLISHED, state.PENDING)
Expand Down Expand Up @@ -535,16 +535,18 @@ def test_transition_aborterror(self):
self.post.submit() # This is to set an initial state for this test
self.assertTrue(self.post.state.PENDING)

success = self.post.abort(success=False)
success, message = self.post.abort(success=False)
self.assertEqual(success, False)
self.assertEqual(message, "failed")
self.assertTrue(self.post.state.PENDING) # state wont change

success = self.post.abort(success=False, empty_abort=True)
self.assertEqual(success, None)
self.assertTrue(self.post.state.PENDING) # state wont change

success = self.post.abort(success=True)
success, message = self.post.abort(success=True)
self.assertEqual(success, True)
self.assertEqual(message, 'passed')
self.assertTrue(self.post.state.PUBLISHED) # state will change

with self.assertRaises(RandomException):
Expand Down

0 comments on commit edebe2f

Please sign in to comment.