| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
# |
| 13 |
|
| 14 |
"""API Utilities Library Tests""" |
| 15 |
|
| 16 |
import copy |
| 17 |
|
| 18 |
from osc_lib.api import api |
| 19 |
from osc_lib.api import utils as api_utils |
| 20 |
from osc_lib.tests.api import fakes as api_fakes |
| 21 |
|
| 22 |
|
| 23 |
class TestBaseAPIFilter(api_fakes.TestSession): |
| 24 |
"""The filters can be tested independently""" |
| 25 |
|
| 26 |
def setUp(self): |
| 27 |
super().setUp() |
| 28 |
self.api = api.BaseAPI( |
| 29 |
session=self.sess, |
| 30 |
endpoint=self.BASE_URL, |
| 31 |
) |
| 32 |
|
| 33 |
self.input_list = [ |
| 34 |
api_fakes.RESP_ITEM_1, |
| 35 |
api_fakes.RESP_ITEM_2, |
| 36 |
api_fakes.RESP_ITEM_3, |
| 37 |
] |
| 38 |
|
| 39 |
def test_simple_filter_none(self): |
| 40 |
output = api_utils.simple_filter() |
| 41 |
self.assertIsNone(output) |
| 42 |
|
| 43 |
def test_simple_filter_no_attr(self): |
| 44 |
output = api_utils.simple_filter( |
| 45 |
copy.deepcopy(self.input_list), |
| 46 |
) |
| 47 |
self.assertEqual(self.input_list, output) |
| 48 |
|
| 49 |
def test_simple_filter_attr_only(self): |
| 50 |
output = api_utils.simple_filter( |
| 51 |
copy.deepcopy(self.input_list), |
| 52 |
attr='status', |
| 53 |
) |
| 54 |
self.assertEqual(self.input_list, output) |
| 55 |
|
| 56 |
def test_simple_filter_attr_value(self): |
| 57 |
output = api_utils.simple_filter( |
| 58 |
copy.deepcopy(self.input_list), |
| 59 |
attr='status', |
| 60 |
value='', |
| 61 |
) |
| 62 |
self.assertEqual([], output) |
| 63 |
|
| 64 |
output = api_utils.simple_filter( |
| 65 |
copy.deepcopy(self.input_list), |
| 66 |
attr='status', |
| 67 |
value='UP', |
| 68 |
) |
| 69 |
self.assertEqual( |
| 70 |
[api_fakes.RESP_ITEM_1, api_fakes.RESP_ITEM_3], |
| 71 |
output, |
| 72 |
) |
| 73 |
|
| 74 |
output = api_utils.simple_filter( |
| 75 |
copy.deepcopy(self.input_list), |
| 76 |
attr='fred', |
| 77 |
value='UP', |
| 78 |
) |
| 79 |
self.assertEqual([], output) |
| 80 |
|
| 81 |
def test_simple_filter_prop_attr_only(self): |
| 82 |
output = api_utils.simple_filter( |
| 83 |
copy.deepcopy(self.input_list), |
| 84 |
attr='b', |
| 85 |
property_field='props', |
| 86 |
) |
| 87 |
self.assertEqual(self.input_list, output) |
| 88 |
|
| 89 |
output = api_utils.simple_filter( |
| 90 |
copy.deepcopy(self.input_list), |
| 91 |
attr='status', |
| 92 |
property_field='props', |
| 93 |
) |
| 94 |
self.assertEqual(self.input_list, output) |
| 95 |
|
| 96 |
def test_simple_filter_prop_attr_value(self): |
| 97 |
output = api_utils.simple_filter( |
| 98 |
copy.deepcopy(self.input_list), |
| 99 |
attr='b', |
| 100 |
value=2, |
| 101 |
property_field='props', |
| 102 |
) |
| 103 |
self.assertEqual( |
| 104 |
[api_fakes.RESP_ITEM_1, api_fakes.RESP_ITEM_2], |
| 105 |
output, |
| 106 |
) |
| 107 |
|
| 108 |
output = api_utils.simple_filter( |
| 109 |
copy.deepcopy(self.input_list), |
| 110 |
attr='b', |
| 111 |
value=9, |
| 112 |
property_field='props', |
| 113 |
) |
| 114 |
self.assertEqual([], output) |