diff --git a/mistral/tests/unit/utils/__init__.py b/mistral/tests/unit/utils/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/mistral/tests/unit/utils/test_yaql.py b/mistral/tests/unit/utils/test_yaql.py new file mode 100644 index 000000000..92dc1e486 --- /dev/null +++ b/mistral/tests/unit/utils/test_yaql.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2013 - Mirantis, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest2 + +from mistral.utils import yaql_utils + + +DATA = { + "server": { + "id": "03ea824a-aa24-4105-9131-66c48ae54acf", + "name": "cloud-fedora", + "status": "ACTIVE" + }, + "status": "OK" +} + + +SERVERS = { + "servers": [ + { + 'name': 'centos' + }, + { + 'name': 'ubuntu' + }, + { + 'name': 'fedora' + } + ] +} + + +class YaqlTest(unittest2.TestCase): + def test_expression_result(self): + res = yaql_utils.evaluate("$.server", DATA) + self.assertEqual(res, { + "id": "03ea824a-aa24-4105-9131-66c48ae54acf", + "name": "cloud-fedora", + "status": "ACTIVE" + }) + + res = yaql_utils.evaluate("$.server.id", DATA) + self.assertEqual(res, "03ea824a-aa24-4105-9131-66c48ae54acf") + + res = yaql_utils.evaluate("$.server.status = 'ACTIVE'", DATA) + self.assertTrue(res) + + def test_wrong_expression(self): + res = yaql_utils.evaluate("$.status = 'Invalid value'", DATA) + self.assertFalse(res) + + res = yaql_utils.evaluate("$.wrong_key", DATA) + self.assertIsNone(res) + + expression_str = "invalid_expression_string" + res = yaql_utils.evaluate(expression_str, DATA) + self.assertEqual(res, expression_str) + + def test_select_result(self): + res = yaql_utils.evaluate("$.servers[$.name = ubuntu]", SERVERS) + item = list(res)[0] + self.assertEqual(item, {'name': 'ubuntu'}) diff --git a/mistral/utils/yaql_utils.py b/mistral/utils/yaql_utils.py index dff8ed2bb..78f4159df 100644 --- a/mistral/utils/yaql_utils.py +++ b/mistral/utils/yaql_utils.py @@ -14,7 +14,9 @@ # See the License for the specific language governing permissions and # limitations under the License. +import yaql + def evaluate(expression_str, data): - #TODO(nmakhotkin) evaluate YAQL expression and return the result - pass + expression = yaql.parse(expression_str) + return expression.evaluate(data) diff --git a/requirements.txt b/requirements.txt index d368b50d5..4d4f83777 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,3 +13,4 @@ python-keystoneclient>=0.3.2 pika>=0.9.13 networkx SQLAlchemy +yaql==0.2.1