def mock_action(action_name):
 def decorator(func):
  def wrapper(*args, **kwargs):
   mock_action = mock.MagicMock()
   from ckan.logic import get_action as original_get_action
   def side_effect(called_action_name):
    if (called_action_name == action_name):
     return mock_action
    else:
     return original_get_action(called_action_name)
   try:
    with mock.patch('ckan.logic.get_action') as mock_get_action:
     with mock.patch('ckan.plugins.toolkit.get_action') as mock_get_action_toolkit:
      mock_get_action.side_effect = side_effect
      mock_get_action_toolkit.side_effect = side_effect
      new_args = (args + tuple([mock_action]))
      return_value = func(*new_args, **kwargs)
   finally:
    mock_action.stop()
   return return_value
  return nose.tools.make_decorator(func)(wrapper)
 return decorator

def mock_action ( action_name ) : def decorator ( func ) : def wrapper ( * args , ** kwargs ) : mock_action = mock . MagicMock ( ) from ckan . logic import get_action as original_get_action def side_effect ( called_action_name ) : if ( called_action_name == action_name ) : return mock_action else : return original_get_action ( called_action_name ) try : with mock . patch ( 'ckan.logic.get_action' ) as mock_get_action : with mock . patch ( 'ckan.plugins.toolkit.get_action' ) as mock_get_action_toolkit : mock_get_action . side_effect = side_effect mock_get_action_toolkit . side_effect = side_effect new_args = ( args + tuple ( [ mock_action ] ) ) return_value = func ( * new_args , ** kwargs ) finally : mock_action . stop ( ) return return_value return nose . tools . make_decorator ( func ) ( wrapper ) return decorator
decorator to easily mock a ckan action in the context of a test function it adds a mock object for the provided action as a parameter to the test function .