def test_expand_parameter_value(value, args, result): """Test expand parameter reference function.""" parameters = ParameterIndex() parameters['A'] = String(name='A', label='P1', index=0) parameters['B'] = String(name='B', label='P2', index=0) parameters['C'] = String(name='C', label='P3', index=2, default='default') assert tp.expand_value(value, args, parameters) == result
def test_add_workflow_parameter(): """Test adding template parameters to a serial workflow.""" wf = SerialWorkflow() wf = wf.add_parameter(String('a')) assert len(wf.parameters) == 1 wf = wf.add_parameter(String('b')).add_parameter(String('a')) assert len(wf.parameters) == 2
def test_string_parameter_from_dict(): """Test generating a string parameter declaration from a dictionary serialization. """ para = String.from_dict( String.to_dict( String.from_dict({ 'name': '0000', 'dtype': PARA_STRING, 'label': 'Firstname', 'index': 1, 'help': 'Your first name', 'defaultValue': 'Alice', 'isRequired': True, 'group': 'person' }))) assert para.is_string() assert para.name == '0000' assert para.dtype == PARA_STRING assert para.label == 'Firstname' assert para.index == 1 assert para.help == 'Your first name' assert para.default == 'Alice' assert para.required assert para.group == 'person'
def test_initialize_workflow(): """Test initializing the serial workflow with different argument combinations.""" # -- Initialize without arguments ----------------------------------------- wf = SerialWorkflow() assert len(wf.steps) == 0 assert len(wf.parameters) == 0 assert wf.workers is not None # -- Set workflow steps at initialization --------------------------------- wf = SerialWorkflow( steps=[ ContainerStep(identifier='s1', image='test_1'), ContainerStep(identifier='s2', image='test_2') ] ) assert len(wf.steps) == 2 assert [s.image for s in wf] == ['test_1', 'test_2'] assert wf.steps[0].image == 'test_1' assert wf.steps[1].image == 'test_2' assert len(wf.parameters) == 0 assert wf.workers is not None # -- Set template parameters at initialization ---------------------------- wf = SerialWorkflow(parameters=[String('a'), String('b')]) assert len(wf.steps) == 0 assert len(wf.parameters) == 2 assert 'a' in wf.parameters assert 'b' in wf.parameters assert wf.workers is not None # -- Error when initializing parameters with duplicate names -------------- with pytest.raises(err.InvalidTemplateError): SerialWorkflow(parameters=[String('a'), String('a')])
def test_read_string_parameters(): """Test reading lists of string parameters.""" parameters = [ String(name='A', index=0), String(name='B', index=1, default='ABC') ] sc = Scanner(reader=ListReader(['true', ''])) arguments = cli.read(parameters, sc) assert len(arguments) == 2 assert arguments['A'] == 'true' assert arguments['B'] == 'ABC'
def test_string_parameter_value(): """Test getting argument value for a string parameter.""" para = String('0000', 0) assert para.cast(2) == '2' assert para.cast('ABC') == 'ABC' assert para.cast(None) == 'None' para = String('0000', 0, required=True) assert para.cast(2) == '2' assert para.cast('ABC') == 'ABC'
def test_validate_arguments(): """Test validating a given set of arguments against the parameters in a workflow template. """ parameters = ParameterIndex.from_dict([ String(name='A', label='P1', index=0, required=True).to_dict(), String(name='B', label='P2', index=1, default='X').to_dict() ]) template = WorkflowTemplate(workflow_spec=dict(), parameters=parameters) template.validate_arguments({'A': 1, 'B': 0}) template.validate_arguments({'A': 1}) with pytest.raises(err.MissingArgumentError): template.validate_arguments({'B': 1})
def test_invalid_serialization(): """Test errors for invalid serializations.""" with pytest.raises(err.InvalidParameterError): String.from_dict({ 'name': '0000', 'dtype': PARA_STRING, }) String.from_dict( { 'name': '0000', 'dtype': 'unknown', 'index': 0, 'label': 'Name', 'isRequired': True }, validate=False) with pytest.raises(ValueError): String.from_dict({ 'name': '0000', 'dtype': 'unknown', 'index': 0, 'label': 'Name', 'isRequired': True }) with pytest.raises(err.InvalidParameterError): String.from_dict({ 'dtype': PARA_STRING, 'label': 'Firstname', 'index': 1, 'help': 'Your first name', 'defaultValue': 'Alice', 'isRequired': True, 'group': 'person' })
def test_parameter_index_serialization(): """Test generating parameter index from serializations.""" p1 = String(name='0', label='P1', index=1) p2 = String(name='1', label='P2', index=0) doc = ParameterIndex.from_dict([p1.to_dict(), p2.to_dict()]).to_dict() parameters = ParameterIndex.from_dict(doc) assert len(parameters) == 2 assert '0' in parameters assert '1' in parameters assert [p.name for p in parameters.sorted()] == ['1', '0'] # Error case: Duplicate parameter. with pytest.raises(err.InvalidTemplateError): ParameterIndex.from_dict([p1.to_dict(), p1.to_dict()]) # Error case: Unknown parameter type. doc = p1.to_dict() doc['dtype'] = 'unknown' with pytest.raises(err.InvalidParameterError): ParameterIndex.from_dict([doc])
def test_replace_arguments(spec): """Test getting parameter references for a workflow specification.""" parameters = ParameterIndex() parameters['A'] = String(name='A', label='P1', index=0) parameters['B'] = String(name='B', label='P2', index=1) parameters['C'] = String(name='C', label='P3', index=2) parameters['D'] = String(name='D', label='P4', index=3, default='default') parameters['E'] = String(name='E', label='P5', index=4) parameters['F'] = String(name='F', label='P6', index=5) parameters['G'] = String(name='G', label='P7', index=6) doc = tp.replace_args(spec, arguments={ 'A': 'x', 'B': 'y', 'C': 'z', 'E': True, 'F': 'b', 'G': 'c' }, parameters=parameters) assert doc == { "name": "myname", "var1": "x", "var2": "x", "var3": "e1", "values": [{ "name": "name", "el1": "y", "el2": None, "el3": "b", "nest": { "var": "default" } }, "z", "y", 3, "E"], "count": 2 } # Error for missing parameter value. with pytest.raises(err.MissingArgumentError): tp.replace_args(spec, {'A': 'x', 'B': 'y'}, parameters) # Error for nested lists. with pytest.raises(err.InvalidTemplateError): spec = {'values': ['A', [2, 3]]} tp.replace_args(spec, {'A': 'x', 'B': 'y'}, parameters)
def test_init_parameter_index(): """Test initializing the parameter index from a given list of parameters.""" assert len(ParameterIndex()) == 0 assert len(ParameterIndex(parameters=[String('A'), String('B')])) == 2 with pytest.raises(err.InvalidTemplateError): ParameterIndex(parameters=[String('A'), String('B'), String('A')])
def test_template_serialization(): """Test creating template instances from serializations.""" # Minimal template specification. doc = {'workflow': dict()} doc = WorkflowTemplate.from_dict(doc).to_dict() template = WorkflowTemplate.from_dict(doc) assert template.workflow_spec == dict() assert template.parameters == ParameterIndex() # Maximal template specification. doc = { 'workflow': { 'inputs': [tp.VARIABLE('A'), 'B', 'C'] }, 'parameters': [String(name='A', label='P1', index=0).to_dict()], 'parameterGroups': [{ 'name': '0', 'title': 'G1', 'index': 0 }, { 'name': '1', 'title': 'G2', 'index': 1 }], 'postproc': { 'workflow': dict(), 'inputs': { 'files': ['D', 'E'] } }, 'results': { 'file': 'results/analytics.json', 'schema': [{ 'name': '0', 'label': 'col0', 'dtype': PARA_STRING }] } } doc = WorkflowTemplate.from_dict(doc).to_dict() template = WorkflowTemplate.from_dict(doc) assert template.workflow_spec == {'inputs': [tp.VARIABLE('A'), 'B', 'C']} assert len(template.parameters) == 1 assert len(template.parameter_groups) == 2 assert template.postproc_spec['workflow'] == dict() # No error for invalid document only if validate is not set to False. para = String(name='0', label='P1', index=0).to_dict() para['addOn'] = 1 doc = { 'workflow': { 'inputs': ['A', 'B', 'C'] }, 'parameters': [para], 'parameterGroups': [{ 'name': '0', 'title': 'G1', 'index': 0, 'sortDesc': True }, { 'name': '1', 'title': 'G2', 'index': 1 }], 'postproc': { 'inputs': { 'files': ['D', 'E'] } } } WorkflowTemplate.from_dict(doc, validate=False) with pytest.raises(err.InvalidParameterError): WorkflowTemplate.from_dict(doc) # Error for missing workflow specification. with pytest.raises(err.InvalidTemplateError): WorkflowTemplate.from_dict(dict()) # Error for unknown parameter. with pytest.raises(err.UnknownParameterError): doc = { 'workflow': { 'inputs': [tp.VARIABLE('0'), 'B', 'C'] }, 'parameters': [String(name='A', label='P1', index=0).to_dict()] } WorkflowTemplate.from_dict(doc)