Merge "Fix a bug with empty context"

This commit is contained in:
Jenkins
2014-03-03 15:48:14 +00:00
committed by Gerrit Code Review
2 changed files with 17 additions and 13 deletions

View File

@@ -27,32 +27,27 @@ class Execution(base.Resource):
class ExecutionManager(base.ResourceManager):
resource_class = Execution
def _validate_context_str(self, context):
def _get_context_as_str(self, context):
msg = 'Context must be a dictionary or json compatible string.'
if not isinstance(context, str):
raise ex.IllegalArgumentException(msg)
context_as_str = str(context)
try:
json.loads(context)
json.loads(context_as_str)
except Exception as e:
raise ex.IllegalArgumentException(msg + e)
return context_as_str
def create(self, workbook_name, task, context=None):
self._ensure_not_empty(workbook_name=workbook_name, task=task)
if isinstance(context, dict):
context_str = str(context)
else:
self._validate_context_str(context)
context_str = context
data = {
'workbook_name': workbook_name,
'task': task,
'context': context_str
'task': task
}
if context is not None:
data['context'] = self._get_context_as_str(context)
return self._create('/workbooks/%s/executions' % workbook_name, data)
def update(self, workbook_name, id, state):

View File

@@ -53,6 +53,15 @@ class TestExecutions(base.BaseClientTest):
self.assertEqual(EXECS[0]['state'], ex.state)
self.assertEqual(EXECS[0]['context'], ex.context)
def test_create_with_empty_context(self):
execs = EXECS[0].copy()
execs.pop('context')
self.mock_http_post(json=execs)
ex = self.executions.create(execs['workbook_name'],
execs['target_task'])
with self.assertRaises(AttributeError):
ex.context
@unittest2.expectedFailure
def test_create_failure1(self):
self.executions.create(EXECS[0]['workbook_name'],