Add YAQL expression evaluation

* YAQL evaluate
 * Unit tests

Change-Id: I0853a08234c75853875b42cd8e89819e9b1e431f
This commit is contained in:
Nikolay Mahotkin 2014-01-27 16:07:10 +04:00
parent 1020554f1c
commit 4ebc881b0c
4 changed files with 81 additions and 2 deletions

View File

View File

@ -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'})

View File

@ -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)

View File

@ -13,3 +13,4 @@ python-keystoneclient>=0.3.2
pika>=0.9.13
networkx
SQLAlchemy
yaql==0.2.1