Merge pull request #226 from sartography/enum_default_value_expression_162

Enum default value expression 162
This commit is contained in:
Dan Funk 2021-01-27 10:43:46 -05:00 committed by GitHub
commit cccb722e07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 325 additions and 2 deletions

View File

@ -159,8 +159,12 @@ class WorkflowService(object):
if WorkflowService.evaluate_property(Task.FIELD_PROP_HIDE_EXPRESSION, field, task):
continue
# If there is a default value, set it.
if hasattr(field,'default_value') and field.default_value:
# A task should only have default_value **or** value expression, not both.
if field.has_property(Task.FIELD_PROP_VALUE_EXPRESSION) and (hasattr(field, 'default_value') and field.default_value):
raise ApiError(code='default value and value_expression',
message='This task has both a default_value and value_expression. Please fix this to only have one or the other.')
# If we have a default_value or value_expression, try to set the default
if field.has_property(Task.FIELD_PROP_VALUE_EXPRESSION) or (hasattr(field, 'default_value') and field.default_value):
form_data[field.id] = WorkflowService.get_default_value(field, task)
# If we are only populating required fields, and this isn't required. stop here.
@ -494,6 +498,9 @@ class WorkflowService(object):
task.form = spiff_task.task_spec.form
for i, field in enumerate(task.form.fields):
task.form.fields[i] = WorkflowService.process_options(spiff_task, field)
# If there is a default value, set it.
if field.id not in task.data and WorkflowService.get_default_value(field, spiff_task) is not None:
task.data[field.id] = WorkflowService.get_default_value(field, spiff_task)
task.documentation = WorkflowService._process_documentation(spiff_task)
# All ready tasks should have a valid name, and this can be computed for

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/DMN/20151101/dmn.xsd" xmlns:biodi="http://bpmn.io/schema/dmn/biodi/1.0" id="Definitions_4ddf19d" name="DRD" namespace="http://camunda.org/schema/1.0/dmn">
<decision id="Decision_Value_Expression" name="Decision 1">
<extensionElements>
<biodi:bounds x="157" y="81" width="180" height="80" />
</extensionElements>
<decisionTable id="decisionTable_1">
<input id="input_1" label="User Input">
<inputExpression id="inputExpression_1" typeRef="boolean">
<text>user_input</text>
</inputExpression>
</input>
<output id="output_1" label="Lookup Output" name="lookup_output" typeRef="string" />
<rule id="DecisionRule_1y5u0zy">
<inputEntry id="UnaryTests_10u8r2i">
<text>True</text>
</inputEntry>
<outputEntry id="LiteralExpression_1dshav8">
<text>'black'</text>
</outputEntry>
</rule>
<rule id="DecisionRule_0pnsbfu">
<inputEntry id="UnaryTests_1lasqlz">
<text>False</text>
</inputEntry>
<outputEntry id="LiteralExpression_1nrtq17">
<text>'white'</text>
</outputEntry>
</rule>
<rule id="DecisionRule_0ujcwwp">
<inputEntry id="UnaryTests_139wk1t">
<text></text>
</inputEntry>
<outputEntry id="LiteralExpression_0emea64">
<text>'grey'</text>
</outputEntry>
</rule>
</decisionTable>
</decision>
</definitions>

View File

@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_889ace0" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="3.0.0-dev">
<bpmn:process id="Process_a7f16c2" isExecutable="true">
<bpmn:startEvent id="StartEvent_1">
<bpmn:outgoing>Flow_02xzhf3</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:sequenceFlow id="Flow_02xzhf3" sourceRef="StartEvent_1" targetRef="Activity_UserInput" />
<bpmn:sequenceFlow id="Flow_0d46qnz" sourceRef="Activity_UserInput" targetRef="Activity_DecisionLookup" />
<bpmn:sequenceFlow id="Flow_1d7sv9v" sourceRef="Activity_DecisionLookup" targetRef="Activity_PickColor" />
<bpmn:userTask id="Activity_UserInput" name="User Input" camunda:formKey="UserInput">
<bpmn:extensionElements>
<camunda:formData>
<camunda:formField id="user_input" label="Select Yes or No" type="boolean" defaultValue="True" />
</camunda:formData>
</bpmn:extensionElements>
<bpmn:incoming>Flow_02xzhf3</bpmn:incoming>
<bpmn:outgoing>Flow_0d46qnz</bpmn:outgoing>
</bpmn:userTask>
<bpmn:businessRuleTask id="Activity_DecisionLookup" name="Decision Lookup" camunda:resultVariable="lookup_output" camunda:decisionRef="Decision_Value_Expression">
<bpmn:incoming>Flow_0d46qnz</bpmn:incoming>
<bpmn:outgoing>Flow_1d7sv9v</bpmn:outgoing>
</bpmn:businessRuleTask>
<bpmn:userTask id="Activity_PickColor" name="Pick a Color" camunda:formKey="MyFormKey">
<bpmn:documentation>&lt;h1&gt;Hello {{ lookup_output }}&lt;/h1&gt;</bpmn:documentation>
<bpmn:extensionElements>
<camunda:formData>
<camunda:formField id="color_select" label="Pick a color" type="enum">
<camunda:properties>
<camunda:property id="value_expression" value="lookup_output" />
</camunda:properties>
<camunda:value id="black" name="Black" />
<camunda:value id="white" name="White" />
<camunda:value id="grey" name="Grey" />
<camunda:value id="gray" name="Gray" />
</camunda:formField>
</camunda:formData>
</bpmn:extensionElements>
<bpmn:incoming>Flow_1d7sv9v</bpmn:incoming>
<bpmn:outgoing>Flow_01x96w8</bpmn:outgoing>
</bpmn:userTask>
<bpmn:sequenceFlow id="Flow_01x96w8" sourceRef="Activity_PickColor" targetRef="Activity_Display_Color" />
<bpmn:manualTask id="Activity_Display_Color" name="Display Color">
<bpmn:documentation>&lt;h1&gt;Hello&lt;/h1&gt;
&lt;div&gt;You picked {{ color_select.label }}&lt;/div&gt;</bpmn:documentation>
<bpmn:incoming>Flow_01x96w8</bpmn:incoming>
<bpmn:outgoing>Flow_05tzoiy</bpmn:outgoing>
</bpmn:manualTask>
<bpmn:endEvent id="Event_074ypt0">
<bpmn:incoming>Flow_05tzoiy</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_05tzoiy" sourceRef="Activity_Display_Color" targetRef="Event_074ypt0" />
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_a7f16c2">
<bpmndi:BPMNEdge id="Flow_05tzoiy_di" bpmnElement="Flow_05tzoiy">
<di:waypoint x="850" y="177" />
<di:waypoint x="912" y="177" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_01x96w8_di" bpmnElement="Flow_01x96w8">
<di:waypoint x="690" y="177" />
<di:waypoint x="750" y="177" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_1d7sv9v_di" bpmnElement="Flow_1d7sv9v">
<di:waypoint x="530" y="177" />
<di:waypoint x="590" y="177" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_0d46qnz_di" bpmnElement="Flow_0d46qnz">
<di:waypoint x="370" y="177" />
<di:waypoint x="430" y="177" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_02xzhf3_di" bpmnElement="Flow_02xzhf3">
<di:waypoint x="215" y="177" />
<di:waypoint x="270" y="177" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
<dc:Bounds x="179" y="159" width="36" height="36" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_0xfaikf_di" bpmnElement="Activity_UserInput">
<dc:Bounds x="270" y="137" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_16u20va_di" bpmnElement="Activity_DecisionLookup">
<dc:Bounds x="430" y="137" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_0dpzpf2_di" bpmnElement="Activity_PickColor">
<dc:Bounds x="590" y="137" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_157v5xp_di" bpmnElement="Activity_Display_Color">
<dc:Bounds x="750" y="137" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_074ypt0_di" bpmnElement="Event_074ypt0">
<dc:Bounds x="912" y="159" width="36" height="36" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/DMN/20151101/dmn.xsd" xmlns:biodi="http://bpmn.io/schema/dmn/biodi/1.0" id="Definitions_4ddf19d" name="DRD" namespace="http://camunda.org/schema/1.0/dmn">
<decision id="Decision_Value_Expression" name="Decision 1">
<extensionElements>
<biodi:bounds x="157" y="81" width="180" height="80" />
</extensionElements>
<decisionTable id="decisionTable_1">
<input id="input_1" label="User Input">
<inputExpression id="inputExpression_1" typeRef="boolean">
<text>user_input</text>
</inputExpression>
</input>
<output id="output_1" label="Lookup Output" name="lookup_output" typeRef="string" />
<rule id="DecisionRule_1y5u0zy">
<inputEntry id="UnaryTests_10u8r2i">
<text>True</text>
</inputEntry>
<outputEntry id="LiteralExpression_1dshav8">
<text>'black'</text>
</outputEntry>
</rule>
<rule id="DecisionRule_0pnsbfu">
<inputEntry id="UnaryTests_1lasqlz">
<text>False</text>
</inputEntry>
<outputEntry id="LiteralExpression_1nrtq17">
<text>'white'</text>
</outputEntry>
</rule>
<rule id="DecisionRule_0ujcwwp">
<inputEntry id="UnaryTests_139wk1t">
<text></text>
</inputEntry>
<outputEntry id="LiteralExpression_0emea64">
<text>'grey'</text>
</outputEntry>
</rule>
</decisionTable>
</decision>
</definitions>

View File

@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_889ace0" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.2.0">
<bpmn:process id="Process_a7f16c2" isExecutable="true">
<bpmn:startEvent id="StartEvent_1">
<bpmn:outgoing>Flow_02xzhf3</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:sequenceFlow id="Flow_02xzhf3" sourceRef="StartEvent_1" targetRef="Activity_UserInput" />
<bpmn:sequenceFlow id="Flow_0d46qnz" sourceRef="Activity_UserInput" targetRef="Activity_DecisionLookup" />
<bpmn:sequenceFlow id="Flow_1d7sv9v" sourceRef="Activity_DecisionLookup" targetRef="Activity_PickColor" />
<bpmn:userTask id="Activity_UserInput" name="User Input" camunda:formKey="UserInput">
<bpmn:extensionElements>
<camunda:formData>
<camunda:formField id="user_input" label="Select Yes or No" type="boolean" defaultValue="True" />
</camunda:formData>
</bpmn:extensionElements>
<bpmn:incoming>Flow_02xzhf3</bpmn:incoming>
<bpmn:outgoing>Flow_0d46qnz</bpmn:outgoing>
</bpmn:userTask>
<bpmn:businessRuleTask id="Activity_DecisionLookup" name="Decision Lookup" camunda:resultVariable="lookup_output" camunda:decisionRef="Decision_Value_Expression">
<bpmn:incoming>Flow_0d46qnz</bpmn:incoming>
<bpmn:outgoing>Flow_1d7sv9v</bpmn:outgoing>
</bpmn:businessRuleTask>
<bpmn:userTask id="Activity_PickColor" name="Pick a Color" camunda:formKey="MyFormKey">
<bpmn:documentation>&lt;h1&gt;Hello {{ lookup_output }}&lt;/h1&gt;</bpmn:documentation>
<bpmn:extensionElements>
<camunda:formData>
<camunda:formField id="color_select" label="Pick a color" type="enum" defaultValue="white">
<camunda:properties>
<camunda:property id="value_expression" value="lookup_output" />
</camunda:properties>
<camunda:value id="black" name="Black" />
<camunda:value id="white" name="White" />
<camunda:value id="grey" name="Grey" />
<camunda:value id="gray" name="Gray" />
</camunda:formField>
</camunda:formData>
</bpmn:extensionElements>
<bpmn:incoming>Flow_1d7sv9v</bpmn:incoming>
<bpmn:outgoing>Flow_01x96w8</bpmn:outgoing>
</bpmn:userTask>
<bpmn:sequenceFlow id="Flow_01x96w8" sourceRef="Activity_PickColor" targetRef="Activity_Display_Color" />
<bpmn:manualTask id="Activity_Display_Color" name="Display Color">
<bpmn:documentation>&lt;h1&gt;Hello&lt;/h1&gt;
&lt;div&gt;You picked {{ color_select.label }}&lt;/div&gt;</bpmn:documentation>
<bpmn:incoming>Flow_01x96w8</bpmn:incoming>
<bpmn:outgoing>Flow_05tzoiy</bpmn:outgoing>
</bpmn:manualTask>
<bpmn:endEvent id="Event_074ypt0">
<bpmn:incoming>Flow_05tzoiy</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_05tzoiy" sourceRef="Activity_Display_Color" targetRef="Event_074ypt0" />
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_a7f16c2">
<bpmndi:BPMNEdge id="Flow_05tzoiy_di" bpmnElement="Flow_05tzoiy">
<di:waypoint x="850" y="117" />
<di:waypoint x="912" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_01x96w8_di" bpmnElement="Flow_01x96w8">
<di:waypoint x="690" y="117" />
<di:waypoint x="750" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_1d7sv9v_di" bpmnElement="Flow_1d7sv9v">
<di:waypoint x="530" y="117" />
<di:waypoint x="590" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_0d46qnz_di" bpmnElement="Flow_0d46qnz">
<di:waypoint x="370" y="117" />
<di:waypoint x="430" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_02xzhf3_di" bpmnElement="Flow_02xzhf3">
<di:waypoint x="215" y="117" />
<di:waypoint x="270" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
<dc:Bounds x="179" y="99" width="36" height="36" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_0xfaikf_di" bpmnElement="Activity_UserInput">
<dc:Bounds x="270" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_16u20va_di" bpmnElement="Activity_DecisionLookup">
<dc:Bounds x="430" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_0dpzpf2_di" bpmnElement="Activity_PickColor">
<dc:Bounds x="590" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_157v5xp_di" bpmnElement="Activity_Display_Color">
<dc:Bounds x="750" y="77" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_074ypt0_di" bpmnElement="Event_074ypt0">
<dc:Bounds x="912" y="99" width="36" height="36" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>

View File

@ -0,0 +1,46 @@
from tests.base_test import BaseTest
import json
class TestWorkflowEnumDefault(BaseTest):
def test_enum_default_from_value_expression(self):
workflow = self.create_workflow('enum_value_expression')
first_task = self.get_workflow_api(workflow).next_task
self.assertEqual('Activity_UserInput', first_task.name)
workflow_api = self.get_workflow_api(workflow)
result = self.complete_form(workflow_api, first_task, {'user_input': True})
self.assertIn('user_input', result.next_task.data)
self.assertEqual(True, result.next_task.data['user_input'])
self.assertIn('lookup_output', result.next_task.data)
self.assertEqual('black', result.next_task.data['lookup_output'])
workflow_api = self.get_workflow_api(workflow)
self.assertEqual('Activity_PickColor', self.get_workflow_api(workflow_api).next_task.name)
self.assertEqual({'value': 'black', 'label': 'Black'}, workflow_api.next_task.data['color_select'])
#
workflow = self.create_workflow('enum_value_expression')
first_task = self.get_workflow_api(workflow).next_task
self.assertEqual('Activity_UserInput', first_task.name)
workflow_api = self.get_workflow_api(workflow)
result = self.complete_form(workflow_api, first_task, {'user_input': False})
self.assertIn('user_input', result.next_task.data)
self.assertEqual(False, result.next_task.data['user_input'])
self.assertIn('lookup_output', result.next_task.data)
self.assertEqual('white', result.next_task.data['lookup_output'])
workflow_api = self.get_workflow_api(workflow)
self.assertEqual('Activity_PickColor', self.get_workflow_api(workflow_api).next_task.name)
self.assertEqual({'value': 'white', 'label': 'White'}, workflow_api.next_task.data['color_select'])
def test_enum_value_expression_and_default(self):
spec_model = self.load_test_spec('enum_value_expression_fail')
rv = self.app.get('/v1.0/workflow-specification/%s/validate' % spec_model.id, headers=self.logged_in_headers())
json_data = json.loads(rv.get_data(as_text=True))
self.assertEqual(json_data[0]['code'], 'default value and value_expression')