| 1 |
# |
| 2 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 |
# not use this file except in compliance with the License. You may obtain |
| 4 |
# a copy of the License at |
| 5 |
# |
| 6 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
# |
| 8 |
# Unless required by applicable law or agreed to in writing, software |
| 9 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 |
# License for the specific language governing permissions and limitations |
| 12 |
# under the License. |
| 13 |
|
| 14 |
from oslotest import base |
| 15 |
|
| 16 |
from aodhclient import utils |
| 17 |
|
| 18 |
|
| 19 |
class SearchQueryBuilderTest(base.BaseTestCase): |
| 20 |
def _do_test(self, expr, expected): |
| 21 |
req = utils.search_query_builder(expr) |
| 22 |
self.assertEqual(expected, req) |
| 23 |
|
| 24 |
def test_search_query_builder(self): |
| 25 |
self._do_test('foo=bar', {"=": {"foo": "bar"}}) |
| 26 |
self._do_test('foo!=1', {"!=": {"foo": 1.0}}) |
| 27 |
self._do_test('foo=True', {"=": {"foo": True}}) |
| 28 |
self._do_test('foo=null', {"=": {"foo": None}}) |
| 29 |
self._do_test('foo="null"', {"=": {"foo": "null"}}) |
| 30 |
|
| 31 |
self._do_test('not (foo="quote" or foo="what!" ' |
| 32 |
'or bar="who?")', |
| 33 |
{"not": {"or": [ |
| 34 |
{"=": {"bar": "who?"}}, |
| 35 |
{"=": {"foo": "what!"}}, |
| 36 |
{"=": {"foo": "quote"}}, |
| 37 |
]}}) |
| 38 |
|
| 39 |
self._do_test('(foo="quote" or not foo="what!" ' |
| 40 |
'or bar="who?") and cat="meme"', |
| 41 |
{"and": [ |
| 42 |
{"=": {"cat": "meme"}}, |
| 43 |
{"or": [ |
| 44 |
{"=": {"bar": "who?"}}, |
| 45 |
{"not": {"=": {"foo": "what!"}}}, |
| 46 |
{"=": {"foo": "quote"}}, |
| 47 |
]} |
| 48 |
]}) |
| 49 |
|
| 50 |
self._do_test('foo="quote" or foo="what!" ' |
| 51 |
'or bar="who?" and cat="meme"', |
| 52 |
{"or": [ |
| 53 |
{"and": [ |
| 54 |
{"=": {"cat": "meme"}}, |
| 55 |
{"=": {"bar": "who?"}}, |
| 56 |
]}, |
| 57 |
{"=": {"foo": "what!"}}, |
| 58 |
{"=": {"foo": "quote"}}, |
| 59 |
]}) |
| 60 |
|
| 61 |
self._do_test('foo="quote" and foo="what!" ' |
| 62 |
'or bar="who?" or cat="meme"', |
| 63 |
{'or': [ |
| 64 |
{'=': {'cat': 'meme'}}, |
| 65 |
{'=': {'bar': 'who?'}}, |
| 66 |
{'and': [ |
| 67 |
{'=': {'foo': 'what!'}}, |
| 68 |
{'=': {'foo': 'quote'}} |
| 69 |
]} |
| 70 |
]}) |
| 71 |
|
| 72 |
|
| 73 |
class CliQueryToArray(base.BaseTestCase): |
| 74 |
def test_cli_query_to_arrary(self): |
| 75 |
cli_query = "this<=34;that=string::foo" |
| 76 |
ret_array = utils.cli_to_array(cli_query) |
| 77 |
expected_query = [ |
| 78 |
{"field": "this", "type": "", "value": "34", "op": "le"}, |
| 79 |
{"field": "that", "type": "string", "value": "foo", "op": "eq"}] |
| 80 |
self.assertEqual(expected_query, ret_array) |