Skip to content

Commit

Permalink
Added tests for optional inputs. "If then *" tests now also work.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ark-kun committed Nov 12, 2018
1 parent 0a8f727 commit 141a1e1
Showing 1 changed file with 82 additions and 6 deletions.
88 changes: 82 additions & 6 deletions sdk/python/tests/components/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,61 @@ def test_automatic_output_resolving(self):

self.assertEqual(len(task1.arguments), 2)

def test_optional_inputs_reordering(self):
'''Tests optional input reordering.
In python signature, optional arguments must come after the required arguments.
'''
component_text = '''\
inputs:
- {name: in1}
- {name: in2, optional: true}
- {name: in3}
implementation:
dockerContainer:
image: busybox
'''
task_factory1 = comp.load_component_from_text(component_text)
import inspect
signature = inspect.signature(task_factory1)
actual_signature = list(signature.parameters.keys())
self.assertSequenceEqual(actual_signature, ['in1', 'in3', 'in2'], str)

def test_missing_optional_input_value_argument(self):
'''Missing optional inputs should resolve to nothing'''
component_text = '''\
inputs:
- {name: input 1, optional: true}
implementation:
dockerContainer:
image: busybox
command:
- a
- {value: input 1}
- z
'''
task_factory1 = comp.load_component_from_text(component_text)
task1 = task_factory1()

self.assertEqual(task1.command, ['a', 'z'])

def test_missing_optional_input_file_argument(self):
'''Missing optional inputs should resolve to nothing'''
component_text = '''\
inputs:
- {name: input 1, optional: true}
implementation:
dockerContainer:
image: busybox
command:
- a
- {file: input 1}
- z
'''
task_factory1 = comp.load_component_from_text(component_text)
task1 = task_factory1()

self.assertEqual(task1.command, ['a', 'z'])

def test_command_concat(self):
component_text = '''\
inputs:
Expand Down Expand Up @@ -428,9 +483,8 @@ def test_command_if_is_present_then(self):
task_then = task_factory1('data')
self.assertEqual(task_then.arguments, ['--in', 'data'])

#TODO: Fix optional arguments
#task_else = task_factory1() #Error: TypeError: Component() missing 1 required positional argument: 'in'
#self.assertEqual(task_else.arguments, [])
task_else = task_factory1()
self.assertEqual(task_else.arguments, [])

def test_command_if_is_present_then_else(self):
component_text = '''\
Expand All @@ -450,9 +504,31 @@ def test_command_if_is_present_then_else(self):
task_then = task_factory1('data')
self.assertEqual(task_then.arguments, ['--in', 'data'])

#TODO: Fix optional arguments
#task_else = task_factory1() #Error: TypeError: Component() missing 1 required positional argument: 'in'
#self.assertEqual(task_else.arguments, ['--no-in'])
task_else = task_factory1()
self.assertEqual(task_else.arguments, ['--no-in'])


def test_command_if_input_value_then(self):
component_text = '''\
inputs:
- {name: Do test, type: boolean, optional: true}
- {name: Test data, optional: true}
- {name: Test parameter 1, optional: true}
implementation:
dockerContainer:
image: busybox
arguments:
- if:
cond: {value: Do test}
then: [--test-data, {value: Test data}, --test-param1, {value: Test parameter 1}]
'''
task_factory1 = comp.load_component(text=component_text)

task_then = task_factory1(True, 'test_data.txt', 42)
self.assertEqual(task_then.arguments, ['--test-data', 'test_data.txt', '--test-param1', '42'])

task_else = task_factory1()
self.assertEqual(task_else.arguments, [])


if __name__ == '__main__':
Expand Down

0 comments on commit 141a1e1

Please sign in to comment.