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_as_json_with_children_with_inputs(self): step = Step(app='HelloWorld', action='returnPlusOne', inputs={'number': '-5.6'}) self.basic_json['action'] = 'returnPlusOne' self.basic_json['input'] = {'number': -5.6} self.assertDictEqual(step.as_json(with_children=True), self.basic_json)
def test_as_json_without_children_with_input_routing(self): step = Step(app='HelloWorld', action='returnPlusOne', inputs={'number': '@step1'}) self.basic_json['action'] = 'returnPlusOne' self.basic_json['input'] = {'number': '@step1'} self.assertDictEqual(step.as_json(with_children=False), self.basic_json)
def test_as_json_with_children_with_widgets(self): widgets = [('aaa', 'bbb'), ('ccc', 'ddd'), ('eee', 'fff')] step = Step(app='HelloWorld', action='helloWorld', widgets=widgets) self.basic_json['widgets'] = [{ 'app': widget[0], 'name': widget[1] } for widget in widgets] self.assertDictEqual(step.as_json(with_children=True), self.basic_json)
def test_as_json_with_children_with_position(self): step = Step(app='HelloWorld', action='helloWorld', position={ 'x': -12.3, 'y': 485 }) self.basic_json['position'] = {'x': -12.3, 'y': 485} self.assertDictEqual(step.as_json(with_children=True), self.basic_json)
def test_init_with_error_steps(self): next_steps = [ NextStep(), NextStep(name='name'), NextStep(name='name2') ] step = Step(app='HelloWorld', action='helloWorld', errors=next_steps) self.__compare_init(step, '', '', 'helloWorld', 'HelloWorld', '', {}, [], [step.as_json() for step in next_steps], ['', ''], [])
def test_as_json_after_executed(self): step = Step(app='HelloWorld', action='helloWorld') instance = Instance.create(app_name='HelloWorld', device_name='device1') step.execute(instance.instance, {}) step_json = step.as_json() self.assertDictEqual( step_json['output'], ActionResult({ 'message': 'HELLO WORLD' }, 'Success').as_json())
def test_as_json_with_children_with_error_steps(self): next_steps = [ NextStep(), NextStep(name='name'), NextStep(name='name2') ] step = Step(app='HelloWorld', action='helloWorld', errors=next_steps) self.basic_json['errors'] = [ next_step.as_json() for next_step in next_steps ] self.assertDictEqual(step.as_json(with_children=True), self.basic_json)
def test_get_children(self): step = Step() names = ['step1', 'step2', 'step3'] for name in names: self.assertIsNone(step.get_children([name])) self.assertDictEqual(step.get_children([]), step.as_json(with_children=False)) next_steps = [ NextStep(name='name1'), NextStep(name='name2'), NextStep(name='name3') ] names = ['name1', 'name2', 'name3'] step = Step(next_steps=next_steps) for i, name in enumerate(names): self.assertDictEqual(step.get_children([name]), next_steps[i].as_json()) step = Step(errors=next_steps) for i, name in enumerate(names): self.assertDictEqual(step.get_children([name]), next_steps[i].as_json()) errors = [ NextStep(name='name1'), NextStep(name='error1'), NextStep(name='error2'), NextStep(name='name2') ] next_names = list(names) error_names = ['error1', 'error2'] all_names = list(next_names) all_names.extend(error_names) step = Step(next_steps=next_steps, errors=errors) for i, name in enumerate(all_names): if not name.startswith('error'): self.assertDictEqual(step.get_children([name]), next_steps[i].as_json()) else: self.assertDictEqual(step.get_children([name]), errors[i - 2].as_json()) flags = [Flag(), Flag(action='action1'), Flag(action='action2')] next_steps = [NextStep(name='name1', flags=flags)] names = ['', 'action1', 'action2'] step = Step(next_steps=next_steps) ancestries = [[name, 'name1'] for name in names] for i, ancestry in enumerate(ancestries): self.assertDictEqual(step.get_children(ancestry), flags[i].as_json())
def test_as_json_without_children_with_next_steps(self): next_steps = [ NextStep(), NextStep(name='name'), NextStep(name='name2') ] step = Step(app='HelloWorld', action='helloWorld', next_steps=next_steps) self.basic_json['next'] = [next_step.name for next_step in next_steps] self.assertDictEqual(step.as_json(with_children=False), self.basic_json)
def test_as_json_with_children_with_name(self): step = Step(app='HelloWorld', action='helloWorld', name='name') self.basic_json['name'] = 'name' self.assertDictEqual(step.as_json(with_children=True), self.basic_json)
def test_to_from_xml(self): inputs = {'in1': 'a', 'in2': 3, 'in3': u'abc'} inputs = { arg_name: Argument(key=arg_name, value=arg_value, format=type(arg_value).__name__) for arg_name, arg_value in inputs.items() } flags = [Flag(), Flag(action='action')] next_steps = [ NextStep(), NextStep(name='name'), NextStep(name='name', parent_name='parent', flags=flags, ancestry=['a', 'b']) ] error_flags = [Flag(), Flag(action='action'), Flag()] errors = [ NextStep(), NextStep(name='name'), NextStep(name='name', parent_name='parent', flags=error_flags, ancestry=['a', 'b']) ] widgets = [('aaa', 'bbb'), ('ccc', 'ddd'), ('eee', 'fff')] steps = [ Step(), Step(name='name'), Step(name='name', parent_name='parent_name'), Step(name='name', parent_name='parent_name', action='action'), Step(name='name', parent_name='parent_name', action='action', app='app'), Step(name='name', parent_name='parent_name', action='action', app='app', device='device'), Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs), Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs, next_steps=next_steps), Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs, next_steps=next_steps, errors=errors), Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs, next_steps=next_steps, errors=errors, position={ 'x': 5, 'y': -40.3 }), Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs, next_steps=next_steps, errors=errors, position={ 'x': 5, 'y': -40.3 }, risk=10), Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs, next_steps=next_steps, errors=errors, position={ 'x': 5, 'y': -40.3 }, risk=-10), Step(name='name', parent_name='parent_name', action='action', app='app', device='device', inputs=inputs, next_steps=next_steps, errors=errors, widgets=widgets) ] for step in steps: original_json = step.as_json() new_step = Step(xml=step.to_xml()) new_json = new_step.as_json() self.assertDictEqual(new_json, original_json)
def test_get_children_no_ancestry(self): step = Step(app='HelloWorld', action='helloWorld') self.assertDictEqual(step.get_children([]), step.as_json(with_children=False))
def __assert_xml_is_convertible(self, step): original_json = step.as_json() original_xml = step.to_xml() new_step = Step(xml=original_xml) self.assertDictEqual(new_step.as_json(), original_json)
def test_as_json_without_children_with_device(self): step = Step(app='HelloWorld', action='helloWorld', device='device') self.basic_json['device'] = 'device' self.assertDictEqual(step.as_json(with_children=False), self.basic_json)
def test_as_json_with_children_with_risk(self): step = Step(app='HelloWorld', action='helloWorld', risk=120.6) self.basic_json['risk'] = 120.6 self.assertDictEqual(step.as_json(with_children=True), self.basic_json)
def test_as_json_without_children_with_risk(self): step = Step(app='HelloWorld', action='helloWorld', risk=169.5) self.basic_json['risk'] = 169.5 self.assertDictEqual(step.as_json(with_children=False), self.basic_json)