def test_from_function(self): def dummy_function(input1, input2, param1, param2): return input1 + input2 + [param1] + [param2] workflow = Workflow.from_function(dummy_function, inputs={ 'input1': DummyType, 'input2': DummyType, 'param1': Int, 'param2': Int }, outputs=collections.OrderedDict([ ('concatenated_inputs', DummyType) ]), name='Concatenate things', doc="Let's concatenate some things!") expected = Workflow(signature=Signature( name='Concatenate things', inputs={ 'input1': DummyType, 'input2': DummyType, 'param1': Int, 'param2': Int, }, outputs=collections.OrderedDict([('concatenated_inputs', DummyType) ])), template=expected_template, id_='dummy_function') self.assertEqual(workflow, expected)
def _test_to_script_or_to_markdown(self, to_method, template_lines): # These methods are so similar that it makes sense to have a helper # that can test either one instead of duplicating a bunch of code. workflow = Workflow.from_markdown(self.markdown_fp) artifact_fp1 = os.path.join(self.test_dir.name, 'artifact1.qtf') artifact_fp2 = os.path.join(self.test_dir.name, 'artifact2.qtf') Artifact.save([-1, 42, 0, 43, 43], DummyType, None, artifact_fp1) Artifact.save([1, 2, 100], DummyType, None, artifact_fp2) artifact_fp3 = os.path.join(self.test_dir.name, 'artifact3.qtf') job = to_method( workflow, input_artifact_filepaths={ 'input1': artifact_fp1, 'input2': artifact_fp2 }, parameter_references={ 'param1': 99, 'param2': -999, }, output_artifact_filepaths={'concatenated_inputs': artifact_fp3}) provenance_lines = [ "provenance = Provenance(", "parameters={", "'param2': -999" ] setup_lines = [ "input1 = Artifact(%r).data" % artifact_fp1, "input2 = Artifact(%r).data" % artifact_fp2, "param1 = 99", "param2 = -999" ] teardown_lines = [ "Artifact.save(concatenated_inputs, DummyType, provenance, " "%r)" % artifact_fp3 ] for expected_lines in (provenance_lines, setup_lines, template_lines, teardown_lines): for expected_line in expected_lines: self.assertIn(expected_line, job.code) self.assertIsInstance(job.uuid, uuid.UUID) self.assertEqual(job.uuid.version, 4) self.assertEqual(job.input_artifact_filepaths, { 'input1': artifact_fp1, 'input2': artifact_fp2 }) self.assertEqual(job.parameter_references, { 'param1': 99, 'param2': -999 }) self.assertEqual(job.output_artifact_filepaths, {'concatenated_inputs': artifact_fp3})
def test_from_markdown(self): workflow = Workflow.from_markdown(self.markdown_fp) expected = Workflow(signature=Signature( name='Dummy markdown workflow', inputs={ 'input1': DummyType, 'input2': DummyType, 'param1': Int, 'param2': Int, }, outputs=collections.OrderedDict([('concatenated_inputs', DummyType) ])), template=frontmatter.parse(markdown_template)[1], id_='dummy_markdown_workflow') self.assertEqual(workflow, expected)
def test_register_function_and_workflow(self): self.assertEqual(self.plugin.workflows, {}) self.plugin.register_function( name='Dummy function', function=dummy_function, inputs={}, outputs=[('answer', Int)], doc='Computes the answer to life, the universe, and everything') with unittest.mock.patch.object(pkg_resources, 'resource_filename', return_value=self.markdown_fp): self.plugin.register_workflow(self.markdown_fp) workflows = { 'dummy_function': Workflow(signature=Signature(name='Dummy function', inputs={}, outputs=collections.OrderedDict([ ('answer', Int) ])), template=expected_dummy_function_template, id_='dummy_function'), 'dummy_markdown_workflow': Workflow(signature=Signature(name='Dummy markdown workflow', inputs={ 'param1': Int, 'param2': Int }, outputs=collections.OrderedDict([ ('the_sum', Int) ])), template=frontmatter.parse(markdown_template)[1], id_='dummy_markdown_workflow') } self.assertEqual(self.plugin.workflows, workflows)
def test_register_function(self): self.assertEqual(self.plugin.workflows, {}) self.plugin.register_function( name='Dummy function', function=dummy_function, inputs={}, outputs=[('answer', Int)], doc='Computes the answer to life, the universe, and everything') self.plugin.register_function( name='Dummy function', function=other_dummy_function, inputs={}, outputs=[('answer', Int)], doc='Computes the answer to life, the universe, and everything') workflows = { 'dummy_function': Workflow(signature=Signature(name='Dummy function', inputs={}, outputs=collections.OrderedDict([ ('answer', Int) ])), template=expected_dummy_function_template, id_='dummy_function'), 'other_dummy_function': Workflow(signature=Signature(name='Dummy function', inputs={}, outputs=collections.OrderedDict([ ('answer', Int) ])), template=expected_other_dummy_function_template, id_='other_dummy_function') } self.assertEqual(self.plugin.workflows, workflows)
def test_signature(self): workflow = Workflow.from_markdown(self.markdown_fp) signature = Signature(name='Dummy markdown workflow', inputs={ 'input1': DummyType, 'input2': DummyType, 'param1': Int, 'param2': Int, }, outputs=collections.OrderedDict([ ('concatenated_inputs', DummyType) ])) self.assertEqual(workflow.signature, signature)
def test_register_workflow(self): self.assertEqual(self.plugin.workflows, {}) with unittest.mock.patch.object(pkg_resources, 'resource_filename', return_value=self.markdown_fp): self.plugin.register_workflow(self.markdown_fp) workflows = { 'dummy_markdown_workflow': Workflow(signature=Signature(name='Dummy markdown workflow', inputs={ 'param1': Int, 'param2': Int }, outputs=collections.OrderedDict([ ('the_sum', Int) ])), template=frontmatter.parse(markdown_template)[1], id_='dummy_markdown_workflow') } self.assertEqual(self.plugin.workflows, workflows)
def setUp(self): # TODO standardize temporary directories created by QIIME self.test_dir = tempfile.TemporaryDirectory(prefix='qiime2-temp-') self.workflow = Workflow.from_function( dummy_function, inputs={ 'input1': DummyType, 'input2': DummyType, 'param1': Int, 'param2': Int }, outputs=collections.OrderedDict([ ('concatenated_inputs', DummyType) ]), name='Concatenate things', doc="Let's concatenate some things!" ) self.artifact_fp1 = os.path.join(self.test_dir.name, 'artifact1.qtf') self.artifact_fp2 = os.path.join(self.test_dir.name, 'artifact2.qtf') Artifact.save([-1, 42, 0, 43, 43], DummyType, None, self.artifact_fp1) Artifact.save([1, 2, 100], DummyType, None, self.artifact_fp2) self.artifact_fp3 = os.path.join(self.test_dir.name, 'artifact3.qtf')
def test_reference(self): workflow = Workflow.from_markdown(self.markdown_fp) self.assertTrue(workflow.reference.startswith(workflow.id))
def test_name(self): workflow = Workflow.from_markdown(self.markdown_fp) self.assertEqual(workflow.name, 'Dummy markdown workflow')
def test_id(self): workflow = Workflow.from_markdown(self.markdown_fp) self.assertEqual(workflow.id, 'dummy_markdown_workflow')
def test_template(self): workflow = Workflow.from_markdown(self.markdown_fp) self.assertEqual(workflow.template, frontmatter.parse(markdown_template)[1])