|
| 1 | +# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
| 2 | +# contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright ownership. |
| 4 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +# (the "License"); you may not use this file except in compliance with |
| 6 | +# the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import jsonpath_rw |
| 17 | + |
| 18 | +from st2mistral.tests.unit import test_function_base as base |
| 19 | + |
| 20 | +from yaql.language import factory |
| 21 | +YAQL_ENGINE = factory.YaqlFactory().create() |
| 22 | + |
| 23 | + |
| 24 | +class JinjaDataTestCase(base.JinjaFunctionTestCase): |
| 25 | + |
| 26 | + def test_function_jsonpath_query_static(self): |
| 27 | + obj = {'people': [{'first': 'James', 'last': 'd'}, |
| 28 | + {'first': 'Jacob', 'last': 'e'}, |
| 29 | + {'first': 'Jayden', 'last': 'f'}, |
| 30 | + {'missing': 'different'}], |
| 31 | + 'foo': {'bar': 'baz'}} |
| 32 | + |
| 33 | + template = '{{ jsonpath_query(_.obj, "people[*].first") }}' |
| 34 | + result = self.eval_expression(template, {"obj": obj}) |
| 35 | + actual = eval(result) |
| 36 | + expected = ['James', 'Jacob', 'Jayden'] |
| 37 | + self.assertEqual(actual, expected) |
| 38 | + |
| 39 | + def test_function_jsonpath_query_dynamic(self): |
| 40 | + obj = {'people': [{'first': 'James', 'last': 'd'}, |
| 41 | + {'first': 'Jacob', 'last': 'e'}, |
| 42 | + {'first': 'Jayden', 'last': 'f'}, |
| 43 | + {'missing': 'different'}], |
| 44 | + 'foo': {'bar': 'baz'}} |
| 45 | + query = "people[*].last" |
| 46 | + |
| 47 | + template = '{{ jsonpath_query(_.obj, _.query) }}' |
| 48 | + result = self.eval_expression(template, {"obj": obj, |
| 49 | + 'query': query}) |
| 50 | + actual = eval(result) |
| 51 | + expected = ['d', 'e', 'f'] |
| 52 | + self.assertEqual(actual, expected) |
| 53 | + |
| 54 | + def test_function_jsonpath_query_no_results(self): |
| 55 | + obj = {'people': [{'first': 'James', 'last': 'd'}, |
| 56 | + {'first': 'Jacob', 'last': 'e'}, |
| 57 | + {'first': 'Jayden', 'last': 'f'}, |
| 58 | + {'missing': 'different'}], |
| 59 | + 'foo': {'bar': 'baz'}} |
| 60 | + query = "query_returns_no_results" |
| 61 | + |
| 62 | + template = '{{ jsonpath_query(_.obj, _.query) }}' |
| 63 | + result = self.eval_expression(template, {"obj": obj, |
| 64 | + 'query': query}) |
| 65 | + actual = eval(result) |
| 66 | + expected = None |
| 67 | + self.assertEqual(actual, expected) |
| 68 | + |
| 69 | + |
| 70 | +class YAQLDataTestCase(base.YaqlFunctionTestCase): |
| 71 | + |
| 72 | + def test_function_jsonpath_query_static(self): |
| 73 | + obj = {'people': [{'first': 'James', 'last': 'd'}, |
| 74 | + {'first': 'Jacob', 'last': 'e'}, |
| 75 | + {'first': 'Jayden', 'last': 'f'}, |
| 76 | + {'missing': 'different'}], |
| 77 | + 'foo': {'bar': 'baz'}} |
| 78 | + result = YAQL_ENGINE('jsonpath_query($.obj, "people[*].first")').evaluate( |
| 79 | + context=self.get_yaql_context({'obj': obj}) |
| 80 | + ) |
| 81 | + expected = ['James', 'Jacob', 'Jayden'] |
| 82 | + self.assertEqual(result, expected) |
| 83 | + |
| 84 | + def test_function_jsonpath_query_dynamic(self): |
| 85 | + obj = {'people': [{'first': 'James', 'last': 'd'}, |
| 86 | + {'first': 'Jacob', 'last': 'e'}, |
| 87 | + {'first': 'Jayden', 'last': 'f'}, |
| 88 | + {'missing': 'different'}], |
| 89 | + 'foo': {'bar': 'baz'}} |
| 90 | + query = "people[*].last" |
| 91 | + result = YAQL_ENGINE('jsonpath_query($.obj, $.query)').evaluate( |
| 92 | + context=self.get_yaql_context({'obj': obj, |
| 93 | + 'query': query}) |
| 94 | + ) |
| 95 | + expected = ['d', 'e', 'f'] |
| 96 | + self.assertEqual(result, expected) |
| 97 | + |
| 98 | + def test_function_jsonpath_query_no_results(self): |
| 99 | + obj = {'people': [{'first': 'James', 'last': 'd'}, |
| 100 | + {'first': 'Jacob', 'last': 'e'}, |
| 101 | + {'first': 'Jayden', 'last': 'f'}, |
| 102 | + {'missing': 'different'}], |
| 103 | + 'foo': {'bar': 'baz'}} |
| 104 | + query = "query_returns_no_results" |
| 105 | + result = YAQL_ENGINE('jsonpath_query($.obj, $.query)').evaluate( |
| 106 | + context=self.get_yaql_context({'obj': obj, |
| 107 | + 'query': query}) |
| 108 | + ) |
| 109 | + expected = None |
| 110 | + self.assertEqual(result, expected) |
0 commit comments