def test_block_eval(self):
        expr = '{% for i in _.x %}{{ i }}{% endfor %}'

        data = {
            'x': ['a', 'b', 'c']
        }

        self.assertEqual('abc', expressions.evaluate(expr, data))
    def test_multi_eval(self):
        expr = '{{ _.foo }} and {{ _.marco }}'

        data = {
            'foo': 'bar',
            'marco': 'polo'
        }

        self.assertEqual('bar and polo', expressions.evaluate(expr, data))
    def test_type_string_detection(self):
        expr = '<% $.foo %> -> <% $.bar %>'

        data = {
            'foo': 101,
            'bar': 201
        }

        self.assertEqual('101 -> 201', expressions.evaluate(expr, data))
    def test_type_string_detection(self):
        expr = '{{ _.foo }} -> {{ _.bar }}'

        data = {
            'foo': 101,
            'bar': 201
        }

        self.assertEqual('101 -> 201', expressions.evaluate(expr, data))
    def test_multi_eval(self):
        expr = '<% $.foo %> and <% $.marco %>'

        data = {
            'foo': 'bar',
            'marco': 'polo'
        }

        self.assertEqual('bar and polo', expressions.evaluate(expr, data))
    def test_nested_eval(self):
        expr = '{{ _.nested.foo }}'

        data = {
            'nested': {
                'foo': 'bar'
            }
        }

        self.assertEqual('bar', expressions.evaluate(expr, data))
    def test_mix_block_and_expr_eval(self):
        expr = '{{ _.a }}{% for i in _.x %}{{ i }}{% endfor %}{{ _.d }}'

        data = {
            'x': ['b', 'c'],
            'a': 'a',
            'd': 'd'
        }

        self.assertEqual('abcd', expressions.evaluate(expr, data))
    def test_eval_recursive(self):
        expr = '{{ _.fee }}'

        data = {
            'fee': '{{ _.fi }}',
            'fi': '{{ _.fo }}',
            'fo': '{{ _.fum }}',
            'fum': 'fee-fi-fo-fum'
        }

        self.assertEqual('fee-fi-fo-fum', expressions.evaluate(expr, data))
    def test_eval_recursive(self):
        expr = '<% $.fee %>'

        data = {
            'fee': '<% $.fi %>',
            'fi': '<% $.fo %>',
            'fo': '<% $.fum %>',
            'fum': 'fee-fi-fo-fum'
        }

        self.assertEqual('fee-fi-fo-fum', expressions.evaluate(expr, data))
    def test_multi_block_eval(self):
        expr = (
            '{% for i in _.x %}{{ i }}{% endfor %}'
            '{% for i in _.y %}{{ i }}{% endfor %}'
        )

        data = {
            'x': ['a', 'b', 'c'],
            'y': ['d', 'e', 'f']
        }

        self.assertEqual('abcdef', expressions.evaluate(expr, data))
    def test_type_preservation(self):
        data = {
            'k1': 101,
            'k2': 1.999,
            'k3': True,
            'k4': [1, 2],
            'k5': {'k': 'v'},
            'k6': None
        }

        self.assertEqual(
            data['k1'],
            expressions.evaluate('{{ _.k1 }}', data)
        )

        self.assertEqual(
            data['k2'],
            expressions.evaluate('{{ _.k2 }}', data)
        )

        self.assertTrue(expressions.evaluate('{{ _.k3 }}', data))

        self.assertListEqual(
            data['k4'],
            expressions.evaluate('{{ _.k4 }}', data)
        )

        self.assertDictEqual(
            data['k5'],
            expressions.evaluate('{{ _.k5 }}', data)
        )

        self.assertIsNone(expressions.evaluate('{{ _.k6 }}', data))
    def test_multi_eval_recursive(self):
        expr = '<% $.fee %> <% $.im %>'

        data = {
            'fee': '<% $.fi %>',
            'fi': '<% $.fo %>',
            'fo': '<% $.fum %>',
            'fum': 'fee-fi-fo-fum!',
            'im': '<% $.hungry %>',
            'hungry': 'i\'m hungry!'
        }

        self.assertEqual(
            'fee-fi-fo-fum! i\'m hungry!',
            expressions.evaluate(expr, data)
        )
    def test_multi_eval_recursive(self):
        expr = '{{ _.fee }} {{ _.im }}'

        data = {
            'fee': '{{ _.fi }}',
            'fi': '{{ _.fo }}',
            'fo': '{{ _.fum }}',
            'fum': 'fee-fi-fo-fum!',
            'im': '{{ _.hungry }}',
            'hungry': 'i\'m hungry!'
        }

        self.assertEqual(
            'fee-fi-fo-fum! i\'m hungry!',
            expressions.evaluate(expr, data)
        )
Example #14
0
    def on_task_complete(self, task, context=None):
        self.wf_ex_graph.update_task(task['id'], state=task['state'])

        context = utils.merge_dicts(
            context or {},
            {'__task_states': self.wf_ex_graph.get_task_attributes('state')},
            overwrite=True
        )

        tasks = []
        outbounds = []

        for seq in self.wf_ex_graph.get_next_sequences(task['id']):
            evaluated_criteria = [
                expressions.evaluate(criterion, context)
                for criterion in seq[3]['criteria']
            ]

            if all(evaluated_criteria):
                outbounds.append(seq)

        for seq in outbounds:
            next_task_id, seq_key, attrs = seq[1], seq[2], seq[3]
            next_task = self.wf_ex_graph.get_task(next_task_id)

            if not attrs.get('satisfied', False):
                self.wf_ex_graph.update_sequence(
                    task['id'],
                    next_task_id,
                    key=seq_key,
                    satisfied=True
                )

            join_spec = next_task.get('join')

            if join_spec:
                inbounds = self.wf_ex_graph.get_prev_sequences(next_task_id)
                satisfied = [s for s in inbounds if s[3].get('satisfied')]
                join_spec = len(inbounds) if join_spec == 'all' else join_spec

                if len(satisfied) < join_spec:
                    continue

            tasks.append({'id': next_task_id, 'name': next_task['name']})

        return sorted(tasks, key=lambda x: x['name'])
    def test_block_eval_recursive(self):
        expr = '{% for i in _.x %}{{ i }}{% endfor %}'

        data = {
            'x': [
                '{{ _.a }}',
                '{{ _.b }}',
                '{{ _.c }}'
                '{% for j in _.y %}{{ j }}{% endfor %}'
            ],
            'a': 'a',
            'b': 'b',
            'c': 'c',
            'y': ['d', 'e', 'f']
        }

        self.assertEqual('abcdef', expressions.evaluate(expr, data))
    def test_custom_function(self):
        expr = '{{ json(\'{"a": 123}\') }}'

        self.assertDictEqual({'a': 123}, expressions.evaluate(expr))
    def test_basic_eval(self):
        expr = '{{ _.foo }}'

        data = {'foo': 'bar'}

        self.assertEqual('bar', expressions.evaluate(expr, data))