|
| 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 | + |
| 17 | +import unittest2 |
| 18 | + |
| 19 | +from st2common.util import jinja as jinja_utils |
| 20 | + |
| 21 | + |
| 22 | +class JinjaUtilsJsonpathQueryTestCase(unittest2.TestCase): |
| 23 | + |
| 24 | + def test_jsonpath_query_static(self): |
| 25 | + env = jinja_utils.get_jinja_environment() |
| 26 | + obj = {'people': [{'first': 'James', 'last': 'd'}, |
| 27 | + {'first': 'Jacob', 'last': 'e'}, |
| 28 | + {'first': 'Jayden', 'last': 'f'}, |
| 29 | + {'missing': 'different'}], |
| 30 | + 'foo': {'bar': 'baz'}} |
| 31 | + |
| 32 | + template = '{{ obj | jsonpath_query("people[*].first") }}' |
| 33 | + actual_str = env.from_string(template).render({'obj': obj}) |
| 34 | + actual = eval(actual_str) |
| 35 | + expected = ['James', 'Jacob', 'Jayden'] |
| 36 | + self.assertEqual(actual, expected) |
| 37 | + |
| 38 | + def test_jsonpath_query_dynamic(self): |
| 39 | + env = jinja_utils.get_jinja_environment() |
| 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 = '{{ obj | jsonpath_query(query) }}' |
| 48 | + actual_str = env.from_string(template).render({'obj': obj, |
| 49 | + 'query': query}) |
| 50 | + actual = eval(actual_str) |
| 51 | + expected = ['d', 'e', 'f'] |
| 52 | + self.assertEqual(actual, expected) |
| 53 | + |
| 54 | + def test_jsonpath_query_no_results(self): |
| 55 | + env = jinja_utils.get_jinja_environment() |
| 56 | + obj = {'people': [{'first': 'James', 'last': 'd'}, |
| 57 | + {'first': 'Jacob', 'last': 'e'}, |
| 58 | + {'first': 'Jayden', 'last': 'f'}, |
| 59 | + {'missing': 'different'}], |
| 60 | + 'foo': {'bar': 'baz'}} |
| 61 | + query = "query_returns_no_results" |
| 62 | + |
| 63 | + template = '{{ obj | jsonpath_query(query) }}' |
| 64 | + actual_str = env.from_string(template).render({'obj': obj, |
| 65 | + 'query': query}) |
| 66 | + actual = eval(actual_str) |
| 67 | + expected = None |
| 68 | + self.assertEqual(actual, expected) |
0 commit comments