A quick fix to enum_label script.

This commit is contained in:
Dan 2021-10-26 12:59:43 -04:00
parent 539174d4c4
commit d0446cbc8a
2 changed files with 33 additions and 25 deletions

View File

@ -19,18 +19,16 @@ class EnumLabel(Script):
Example:
pet_label = enum_label('task_pet_form', 'pet', '1') // might return 'Dog' which has the value of 1
alternately, you can use named parameters:
pet_label = enum_label(task='task_pet_form',field='pet',value='1') // might return 'Dog' which has the value of 1
"""
def do_task_validate_only(self, task, study_id, workflow_id, *args, **kwargs):
return self.do_task(task, study_id, workflow_id, *args, **kwargs)
def do_task(self, task, study_id, workflow_id, *args, **kwargs):
def do_task(self, spiff_task, study_id, workflow_id, *args, **kwargs):
self.validate_arguments(**kwargs)
task_name = kwargs['task_name']
field_name = kwargs['field']
value = kwargs['value']
task_name, field_name, value = self.validate_arguments(*args, **kwargs)
# get the field information for the provided task_name (NOT the current task)
workflow_model = db.session.query(WorkflowModel).filter(WorkflowModel.id == workflow_id).first()
@ -44,7 +42,7 @@ pet_label = enum_label('task_pet_form', 'pet', '1') // might return 'Dog' whi
elif field.type == Task.FIELD_TYPE_ENUM and hasattr(field, 'options'):
return self.enum_with_options_label(field, value)
elif field.has_property(Task.FIELD_PROP_DATA_NAME):
return self.enum_from_task_data_label(task, field, value)
return self.enum_from_task_data_label(spiff_task, field, value)
def autocomplete_label(self, workflow_model, task_name, field, value):
label_column = field.get_property(Task.FIELD_PROP_LABEL_COLUMN)
@ -70,16 +68,21 @@ pet_label = enum_label('task_pet_form', 'pet', '1') // might return 'Dog' whi
return d[label_column]
return self.UNKNOWN
def validate_arguments(self, **kwargs):
if len(kwargs) != 3:
def validate_arguments(self, *args, **kwargs):
if len(args) != 3 and len(kwargs) != 3:
raise ApiError(code="invalid_argument",
message="enum_label requires three arguments: Task id, Field id, and the selected value.")
if not 'task_name' in kwargs:
raise ApiError(code="invalid_argument",
message="you must specify the 'task_name', that is the name of the task with a form and field")
if not 'field' in kwargs:
raise ApiError(code="invalid_argument",
message="you must specify the 'field', that is the name of the field with an enum.")
if not 'value' in kwargs:
raise ApiError(code="invalid_argument",
message="you must specify the 'value', that is the value of the enum you wish to get a label for.")
elif len(args) == 3:
return args
else:
if not 'task' in kwargs:
raise ApiError(code="invalid_argument",
message="you must specify the 'task_name', that is the name of the task with a form and field")
if not 'field' in kwargs:
raise ApiError(code="invalid_argument",
message="you must specify the 'field', that is the name of the field with an enum.")
if not 'value' in kwargs:
raise ApiError(code="invalid_argument",
message="you must specify the 'value', that is the value of the enum you wish to get a label for.")
return kwargs['task'], kwargs['field'], kwargs['value']

View File

@ -19,38 +19,43 @@ class TestGetEnumLabel(BaseTest):
def test_get_enum_label_for_ldap(self):
result = self.labelScript.do_task(self.task, self.workflow_api.study_id, self.workflow_api.id,
task_name='myFormTask', field='ldap', value='dhf8r')
task='myFormTask', field='ldap', value='dhf8r')
self.assertEqual("Dan Funk", result)
def test_get_enum_label_for_standard_enum(self):
result = self.labelScript.do_task(self.task, self.workflow_api.study_id, self.workflow_api.id,
task_name='myFormTask', field='standard_enum', value='one')
task='myFormTask', field='standard_enum', value='one')
self.assertEqual('1', result)
def test_get_enum_label_using_unnamed_args(self):
result = self.labelScript.do_task(self.task, self.workflow_api.study_id, self.workflow_api.id,
'myFormTask', 'standard_enum', 'one')
self.assertEqual('1', result)
def test_get_enum_label_for_spreadsheet(self):
result = self.labelScript.do_task(self.task, self.workflow_api.study_id, self.workflow_api.id,
task_name='myFormTask', field='spreadsheet', value='2')
task='myFormTask', field='spreadsheet', value='2')
self.assertEqual('T-shirts', result)
def test_get_enum_label_for_data(self):
result = self.labelScript.do_task(self.task, self.workflow_api.study_id, self.workflow_api.id,
task_name='myFormTask', field='data', value='simo')
task='myFormTask', field='data', value='simo')
self.assertEqual('Simo', result)
def test_get_enum_label_for_checkbox(self):
result = self.labelScript.do_task(self.task, self.workflow_api.study_id, self.workflow_api.id,
task_name='myFormTask', field='checkbox', value='simo')
task='myFormTask', field='checkbox', value='simo')
self.assertEqual('Simo', result)
def test_get_invalid_spec_name(self):
with self.assertRaises(ApiError) as ctx:
ldap_result = self.labelScript.do_task(self.task, self.workflow_api.study_id, self.workflow_api.id,
task_name='myWrongFormTask', field='standard_enum', value='one')
task='myWrongFormTask', field='standard_enum', value='one')
self.assertEqual("ApiError: Unable to find a task in the workflow called 'myWrongFormTask'. ", str(ctx.exception))
def test_get_invalid_field_name(self):
with self.assertRaises(ApiError) as ctx:
ldap_result = self.labelScript.do_task(self.task, self.workflow_api.study_id, self.workflow_api.id,
task_name='myFormTask', field='made_up_enum', value='one')
task='myFormTask', field='made_up_enum', value='one')
self.assertEqual("ApiError: The task 'myFormTask' has no field named 'made_up_enum'. ", str(ctx.exception))