def test_execute(self): conditions1 = [ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='(.*)')]) ] conditions2 = [ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='(.*)')]), Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='a')]) ] inputs = [ ('name1', [], ActionResult('aaaa', 'Success'), True), ('name2', conditions1, ActionResult('anyString', 'Success'), True), ('name3', conditions2, ActionResult('anyString', 'Success'), True), ('name4', conditions2, ActionResult('bbbb', 'Success'), False), ('name4', conditions2, ActionResult('aaaa', 'Custom'), False) ] for name, conditions, input_str, expect_name in inputs: branch = Branch(source_uid="1", destination_uid="2", conditions=conditions) if expect_name: expected_name = branch.destination_uid self.assertEqual(branch.execute(input_str, {}), expected_name) else: self.assertIsNone(branch.execute(input_str, {}))
def test_init_with_flags(self): triggers = [ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='(.*)')]), Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='a')]) ] action = Action('HelloWorld', 'helloWorld', triggers=triggers) self.__compare_init(action, '', 'helloWorld', 'HelloWorld', triggers=['regMatch', 'regMatch'])
def test_init_with_conditions(self): conditions = [ Condition('HelloWorld', 'Top Condition'), Condition('HelloWorld', 'mod1_flag1') ] expected_condition_json = [{ 'action_name': 'Top Condition', 'args': [], 'filters': [] }, { 'action_name': 'mod1_flag1', 'args': [], 'filters': [] }] branch = Branch("1", "2", conditions=conditions) self.__compare_init(branch, "1", "2", expected_condition_json)
def test_init_with_uid(self): uid = uuid.uuid4().hex condition = Condition('HelloWorld', 'Top Condition', uid=uid) self.__compare_init(condition, 'HelloWorld', 'Top Condition', [], {}, uid=uid)
def test_init_with_arguments_with_routing(self): condition = Condition( 'HelloWorld', action_name='mod1_flag2', arguments=[Argument('arg1', reference='action2')]) self.__compare_init(condition, 'HelloWorld', 'mod1_flag2', [], [Argument('arg1', reference="action2")])
def test_execute_multiple_triggers(self): triggers = [ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='aaa')]) ] action = Action(app_name='HelloWorld', action_name='helloWorld', triggers=triggers) instance = AppInstance.create(app_name='HelloWorld', device_name='device1') action.send_data_to_trigger({"data_in": {"data": 'a'}}) trigger_taken = {'triggered': 0} trigger_not_taken = {'triggered': 0} def callback_is_sent(sender, **kwargs): if kwargs['event'] == WalkoffEvent.TriggerActionTaken: trigger_taken['triggered'] += 1 elif kwargs['event'] == WalkoffEvent.TriggerActionNotTaken: action.send_data_to_trigger({"data_in": {"data": 'aaa'}}) trigger_not_taken['triggered'] += 1 WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent) action.execute(instance.instance, {}) self.assertEqual(trigger_taken['triggered'], 1) self.assertEqual(trigger_not_taken['triggered'], 1)
def test_eq(self): conditions = [ Condition('HelloWorld', 'mod1_flag1'), Condition('HelloWorld', 'Top Condition') ] branches = [ Branch(source_uid="1", destination_uid="2"), Branch(source_uid="1", destination_uid="2", status='TestStatus'), Branch(source_uid="1", destination_uid="2", conditions=conditions) ] for i in range(len(branches)): for j in range(len(branches)): if i == j: self.assertEqual(branches[i], branches[j]) else: self.assertNotEqual(branches[i], branches[j])
def test_execute_action_with_valid_complex_arguments_valid_data(self): self.assertTrue( Condition('HelloWorld', action_name='mod1_flag3', arguments=[Argument('arg1', value={ 'a': '1', 'b': '5' })]).execute('some_long_string', {}))
def test_get_branch_invalid_action(self): flag = Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='aaa')]) branch = Branch(source_uid="1", destination_uid='next', conditions=[flag]) action = Action('HelloWorld', 'helloWorld', uid="2") action._output = ActionResult(result='bbb', status='Success') workflow = Workflow(actions=[action], branches=[branch]) self.assertIsNone(workflow.get_branch(action, {}))
def test_init_with_transforms(self): transforms = [ Transform('HelloWorld', action_name='mod1_filter2', arguments=[Argument('arg1', value='5.4')]), Transform(app_name='HelloWorld', action_name='Top Transform') ] condition = Condition('HelloWorld', action_name='Top Condition', transforms=transforms) self.__compare_init(condition, 'HelloWorld', 'Top Condition', transforms, {})
def test_execute_action_with_valid_arguments_and_transforms_valid_data( self): transforms = [ Transform('HelloWorld', action_name='mod1_filter2', arguments=[Argument('arg1', value='5')]), Transform('HelloWorld', action_name='Top Transform') ] # should go <input = 1> -> <mod1_filter2 = 5+1 = 6> -> <Top Transform 6=6> -> <mod1_flag2 4+6%2==0> -> True self.assertTrue( Condition('HelloWorld', action_name='mod1_flag2', arguments=[Argument('arg1', value=4)], transforms=transforms).execute('1', {}))
def test_execute_action_with_valid_arguments_and_transforms_invalid_data( self): transforms = [ Transform('HelloWorld', action_name='mod1_filter2', arguments=[Argument('arg1', value='5')]), Transform('HelloWorld', action_name='Top Transform') ] # should go <input = invalid> -> <mod1_filter2 with error = invalid> -> <Top Transform with error = invalid> # -> <mod1_flag2 4+invalid throws error> -> False self.assertFalse( Condition('HelloWorld', action_name='mod1_flag2', arguments=[Argument('arg1', value=4)], transforms=transforms).execute('invalid', {}))
def test_execute_action_with_valid_arguments_and_transforms_invalid_data_and_routing( self): transforms = [ Transform('HelloWorld', action_name='mod1_filter2', arguments=[Argument('arg1', reference='action1')]), Transform('HelloWorld', action_name='Top Transform') ] # should go <input = invalid> -> <mod1_filter2 with error = invalid> -> <Top Transform with error = invalid> # -> <mod1_flag2 4+invalid throws error> -> False accumulator = {'action1': '5', 'action2': 4} self.assertFalse( Condition('HelloWorld', action_name='mod1_flag2', arguments=[Argument('arg1', value=4)], transforms=transforms).execute('invalid', accumulator))
def test_branch_with_priority(self): flag = Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='aaa')]) branch_one = Branch(source_uid="1", destination_uid='five', conditions=[flag], priority="5") branch_two = Branch(source_uid="1", destination_uid='one', conditions=[flag], priority="1") action = Action('HelloWorld', 'helloWorld', uid="1") action._output = ActionResult(result='aaa', status='Success') workflow = Workflow(actions=[action], branches=[branch_one, branch_two]) self.assertEqual(workflow.get_branch(action, {}), "one")
def test_get_branch(self): flag = Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='aaa')]) branch = Branch(source_uid="1", destination_uid="2", conditions=[flag]) action = Action('HelloWorld', 'helloWorld', uid="1") action._output = ActionResult(result='aaa', status='Success') workflow = Workflow(actions=[action], branches=[branch]) result = {'triggered': False} def validate_sent_data(sender, **kwargs): if isinstance(sender, Branch): self.assertIn('event', kwargs) self.assertEqual(kwargs['event'], WalkoffEvent.BranchTaken) result['triggered'] = True WalkoffEvent.CommonWorkflowSignal.connect(validate_sent_data) self.assertEqual(workflow.get_branch(action, {}), '2') self.assertTrue(result['triggered'])
def test_execute_with_triggers(self): triggers = [ Condition('HelloWorld', action_name='regMatch', arguments=[Argument('regex', value='aaa')]) ] action = Action(app_name='HelloWorld', action_name='helloWorld', triggers=triggers) instance = AppInstance.create(app_name='HelloWorld', device_name='device1') action.send_data_to_trigger({"data_in": {"data": 'aaa'}}) result = {'triggered': False} def callback_is_sent(sender, **kwargs): if kwargs['event'] == WalkoffEvent.TriggerActionTaken: result['triggered'] = True WalkoffEvent.CommonWorkflowSignal.connect(callback_is_sent) action.execute(instance.instance, {}) self.assertTrue(result['triggered'])
def test_init_with_arguments_invalid_arg_type(self): with self.assertRaises(InvalidArgument): Condition('HelloWorld', action_name='mod1_flag2', arguments=[Argument('arg1', value='aaa')])
def test_init_with_arguments_no_conversion(self): condition = Condition('HelloWorld', action_name='mod1_flag2', arguments=[Argument('arg1', value='3')]) self.__compare_init(condition, 'HelloWorld', 'mod1_flag2', [], [Argument('arg1', value='3')])
def test_init_no_arguments_action_only(self): condition = Condition('HelloWorld', 'Top Condition') self.__compare_init(condition, 'HelloWorld', 'Top Condition', [], {})
def test_execute_action_with_valid_arguments_invalid_data(self): self.assertFalse( Condition('HelloWorld', action_name='mod1_flag2', arguments=[Argument('arg1', value=3)]).execute('invalid', {}))
def test_execute_action_only_no_arguments_invalid_data(self): self.assertFalse( Condition('HelloWorld', 'Top Condition').execute('invalid', {}))
def test_execute_action_only_no_arguments_valid_data_with_conversion(self): self.assertTrue( Condition('HelloWorld', 'Top Condition').execute('3.4', {}))