def test_from_json_with_inputs_invalid_format(self): self.basic_input_json['action'] = 'Add Three' self.basic_input_json['input'] = { 'num1': '-5.6', 'num2': '4.3', 'num3': 'invalid' } with self.assertRaises(InvalidInput): Step.from_json(self.basic_input_json, {})
def test_from_json(self): next_step_names = ['next1', 'next2'] error_names = ['error1', 'error2'] inputs = [{'name': 'name1', 'action': 'action1', 'app': 'app1', 'device': 'dev1', 'next': [], 'error': []}, {'name': 'name2', 'action': 'action2', 'app': 'app2', 'device': 'opt2', 'next': next_step_names, 'error': []}, {'name': 'name3', 'action': 'action3', 'app': 'app3', 'device': 'opt3', 'next': [], 'error': error_names}, {'name': 'name4', 'action': 'action4', 'app': 'app4', 'device': 'dev4', 'next': next_step_names, 'error': error_names}] for input_params in inputs: step = Step(name=input_params['name'], action=input_params['action'], app=input_params['app'], device=input_params['device']) step.conditionals = [NextStep(name=name, parent_name=step.name, ancestry=list(step.ancestry)) for name in input_params['next']] step.errors = [NextStep(name=name, parent_name=step.name, ancestry=list(step.ancestry)) for name in input_params['error']] step_json = step.as_json() derived_step = Step.from_json(step_json, parent_name=step.parent_name, ancestry=list(step.ancestry)[:-1]) self.assertDictEqual(derived_step.as_json(), step_json) self.assertEqual(step.parent_name, derived_step.parent_name) self.assertListEqual(step.ancestry, derived_step.ancestry) # check the ancestry of the next_steps original_next_step_ancestries = [list(next_step.ancestry) for next_step in step.conditionals] derived_next_step_ancestries = [list(next_step.ancestry) for next_step in derived_step.conditionals] self.assertEqual(len(original_next_step_ancestries), len(derived_next_step_ancestries)) for original_next_step_ancestry, derived_next_step_ancestry in zip(original_next_step_ancestries, derived_next_step_ancestries): self.assertListEqual(derived_next_step_ancestry, original_next_step_ancestry) # check the ancestry of the next_steps original_error_ancestries = [list(error.ancestry) for error in step.errors] derived_error_ancestries = [list(error.ancestry) for error in derived_step.errors] self.assertEqual(len(original_error_ancestries), len(derived_error_ancestries)) for original_error_ancestry, derived_error_ancestry in zip(original_error_ancestries, derived_error_ancestries): self.assertListEqual(derived_error_ancestry, original_error_ancestry)
def test_from_json_with_risk(self): self.basic_input_json['risk'] = 132.3 step = Step.from_json(self.basic_input_json, {}) self.__compare_init(step, '', '', 'helloWorld', 'HelloWorld', '', {}, [], ['', ''], [], risk=132.3)
def from_cytoscape_data(self, data): self.steps = {} for node in data: if 'source' not in node['data'] and 'target' not in node['data']: step_data = node['data'] step_name = step_data['parameters']['name'] self.steps[step_name] = Step.from_json(step_data['parameters'], node['position'], parent_name=self.name, ancestry=self.ancestry)
def test_from_json_with_next_steps(self): next_steps = [ NextStep(), NextStep(name='name'), NextStep(name='name2') ] next_steps_json = [next_step.as_json() for next_step in next_steps] self.basic_input_json['next'] = next_steps_json step = Step.from_json(self.basic_input_json, {}) self.__compare_init(step, '', '', 'helloWorld', 'HelloWorld', '', {}, next_steps_json, ['', ''], [])
def test_from_json_with_position(self): step = Step.from_json(self.basic_input_json, {'x': 125.3, 'y': 198.7}) self.__compare_init(step, '', '', 'helloWorld', 'HelloWorld', '', {}, [], ['', ''], [], position={ 'x': '125.3', 'y': '198.7' })
def test_from_json_with_widgets(self): widget_json = [{ 'name': 'widget_name', 'app': 'app1' }, { 'name': 'w2', 'app': 'app2' }] widget_tuples = [('app1', 'widget_name'), ('app2', 'w2')] self.basic_input_json['widgets'] = widget_json step = Step.from_json(self.basic_input_json, {}) self.__compare_init(step, '', '', 'helloWorld', 'HelloWorld', '', {}, [], ['', ''], widget_tuples)
def test_from_json_with_step_routing(self): self.basic_input_json['action'] = 'Add Three' self.basic_input_json['input'] = { 'num1': '-5.6', 'num2': '@step1', 'num3': '@step2' } step = Step.from_json(self.basic_input_json, {}) self.__compare_init(step, '', '', 'Add Three', 'HelloWorld', '', { 'num1': -5.6, 'num2': '@step1', 'num3': '@step2' }, [], ['', ''], [])
def test_from_json_with_inputs(self): self.basic_input_json['action'] = 'Add Three' self.basic_input_json['input'] = { 'num1': '-5.6', 'num2': '4.3', 'num3': '-10.265' } step = Step.from_json(self.basic_input_json, {}) self.__compare_init(step, '', '', 'Add Three', 'HelloWorld', '', { 'num1': -5.6, 'num2': 4.3, 'num3': -10.265 }, [], ['', ''], [])
def from_cytoscape_data(self, data): """Reconstruct a Workflow object based on cytoscape data. Args: data (JSON dict): The cytoscape data to be parsed and reconstructed into a Workflow object. """ self.steps = {} for node in data: if 'source' not in node['data'] and 'target' not in node['data']: step_data = node['data'] step_name = step_data['parameters']['name'] self.steps[step_name] = Step.from_json(step_data['parameters'], node['position'], parent_name=self.name, ancestry=self.ancestry)
def from_cytoscape_data(self, data): """Reconstruct a Workflow object based on cytoscape data. Args: data (JSON dict): The cytoscape data to be parsed and reconstructed into a Workflow object. """ backup_steps = deepcopy(self.steps) self.steps = {} try: for node in data: if 'source' not in node['data'] and 'target' not in node[ 'data']: step_data = node['data'] step_name = step_data['parameters']['name'] self.steps[step_name] = Step.from_json( step_data['parameters'], node['position'], parent_name=self.name, ancestry=self.ancestry) except (UnknownApp, UnknownAppAction): self.steps = backup_steps raise
def test_from_json_invalid_app(self): self.basic_input_json['app'] = 'Invalid' with self.assertRaises(UnknownApp): Step.from_json(self.basic_input_json, {})
def test_from_json_invalid_action(self): self.basic_input_json['action'] = 'invalid' with self.assertRaises(UnknownAppAction): Step.from_json(self.basic_input_json, {})
def test_from_json__with_parent_name(self): step = Step.from_json(self.basic_input_json, {}, parent_name='parent') self.__compare_init(step, '', 'parent', 'helloWorld', 'HelloWorld', '', {}, [], ['parent', ''], [])
def test_from_json_with_parent_name_and_ancestry(self): step = Step.from_json(self.basic_input_json, {}, parent_name='parent', ancestry=['a', 'b']) self.__compare_init(step, '', 'parent', 'helloWorld', 'HelloWorld', '', {}, [], ['a', 'b', ''], [])
def test_from_json_app_and_action_only(self): step = Step.from_json(self.basic_input_json, {}) self.__compare_init(step, '', '', 'helloWorld', 'HelloWorld', '', {}, [], ['', ''], [])
def test_from_json_with_device_is_none_string(self): self.basic_input_json['device'] = 'None' step = Step.from_json(self.basic_input_json, {}) self.__compare_init(step, '', '', 'helloWorld', 'HelloWorld', '', {}, [], ['', ''], [])
def test_from_json_with_name(self): self.basic_input_json['name'] = 'name1' step = Step.from_json(self.basic_input_json, {}) self.__compare_init(step, 'name1', '', 'helloWorld', 'HelloWorld', '', {}, [], ['', 'name1'], [])