def test_convert_expr_bool(self):
     expr_bool = True
     result = ExpressionConverter.convert(expr_bool)
     self.assertEqual(result, True)
     expr_bool = False
     result = ExpressionConverter.convert(expr_bool)
     self.assertEqual(result, False)
 def test_convert_expr_int(self):
     expr_int = 0
     result = ExpressionConverter.convert(expr_int)
     self.assertEqual(result, 0)
     expr_int = 100
     result = ExpressionConverter.convert(expr_int)
     self.assertEqual(result, 100)
     expr_int = -1
     result = ExpressionConverter.convert(expr_int)
     self.assertEqual(result, -1)
 def test_convert_expr_obj_with_warning(self):
     expr_obj = object()
     expected_warning_regex = re.compile(
         r"Could not recognize expression '<object object at 0x[0-9a-f]+>'; "
         r"results may not be accurate.")
     with self.assertWarnsRegex(SyntaxWarning, expected_warning_regex):
         result = ExpressionConverter.convert(expr_obj)
     self.assertEqual(result, expr_obj)
示例#4
0
    def convert_task_transition_expr(self, task_name, expression_list, publish, orquesta_expr):
        # group all complex expressions by their common expression
        # this way we can keep all of the transitions with the same
        # expressions in the same `when:` condition
        #
        # on-success:
        #   - do_thing_a: "{{ _.x }}"
        #   - do_thing_b: "{{ _.x }}"
        #   - do_thing_c: "{{ not _.x }}"
        #
        # should produce the following in orquesta
        #
        # next:
        #   - when: "{{ succeeded() and _.x }}"
        #     do:
        #       - do_thing_a
        #       - do_thing_b
        #   - when: "{{ succeeded() and not _.x }}"
        #     do:
        #       - do_thing_c
        transitions = []
        for expr, task_list in six.iteritems(expression_list):
            expr_transition = ruamel.yaml.comments.CommentedMap()
            expr_converted = ExpressionConverter.convert(expr)

            # for some transitions (on-complete) the orquesta_expr may be empty
            # so only add it in, if it's necessary
            if orquesta_expr:
                converter = ExpressionConverter.get_converter(expr_converted)
                expr_converted = converter.unwrap_expression(expr_converted)
                o_expr = '{} and ({})'.format(orquesta_expr, expr_converted)
                o_expr = converter.wrap_expression(o_expr)
            else:
                o_expr = expr_converted

            expr_transition['when'] = o_expr
            if publish:
                converted_publish = ExpressionConverter.convert_dict(publish)
                expr_transition['publish'] = [{k: v} for k, v in converted_publish.items()]

                expr_transition['when'] = self.replace_immediately_referenced_variables(task_name,
                                                                                        expr_transition['when'],
                                                                                        converted_publish)

            expr_transition['do'] = task_list
            transitions.append(expr_transition)
        return transitions
 def test_convert_no_expression(self):
     expr = "data"
     result = ExpressionConverter.convert(expr)
     self.assertEqual(result, "data")
 def test_convert_expr_string(self):
     expr_dict = "{{ _.value }}"
     result = ExpressionConverter.convert(expr_dict)
     self.assertEqual(result, "{{ ctx().value }}")
 def test_convert_expr_list(self):
     expr_list = ["test", "{{ _.value }}"]
     result = ExpressionConverter.convert(expr_list)
     self.assertEqual(result, ["test", "{{ ctx().value }}"])
 def test_convert_expr_dict(self):
     expr_dict = {"test": "{{ _.value }}"}
     result = ExpressionConverter.convert(expr_dict)
     self.assertEqual(result, {"test": "{{ ctx().value }}"})
 def test_convert_expr_null(self):
     expr_none = None
     result = ExpressionConverter.convert(expr_none)
     self.assertEqual(result, None)