| 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 |
from unittest import mock |
| 14 |
|
| 15 |
from neutron_lib.objects import utils as obj_utils |
| 16 |
from neutron_lib.tests import _base as base |
| 17 |
|
| 18 |
|
| 19 |
class TestUtils(base.BaseTestCase): |
| 20 |
|
| 21 |
def test_get_objects_with_filters_not_in(self): |
| 22 |
|
| 23 |
class FakeColumn: |
| 24 |
def __init__(self, column): |
| 25 |
self.column = column |
| 26 |
|
| 27 |
def in_(self, value): |
| 28 |
self.value = value |
| 29 |
return self |
| 30 |
|
| 31 |
def __invert__(self): |
| 32 |
return list(set(self.column) - set(self.value)) |
| 33 |
|
| 34 |
filter_obj = obj_utils.NotIn([1, 2, 3]) |
| 35 |
fake_column = FakeColumn([1, 2, 4, 5]) |
| 36 |
self.assertEqual([4, 5], |
| 37 |
sorted(filter_obj.filter(fake_column))) |
| 38 |
|
| 39 |
fake_column = FakeColumn([1, 2]) |
| 40 |
self.assertEqual([], filter_obj.filter(fake_column)) |
| 41 |
|
| 42 |
fake_column = FakeColumn([4, 5]) |
| 43 |
self.assertEqual([4, 5], |
| 44 |
sorted(filter_obj.filter(fake_column))) |
| 45 |
|
| 46 |
def test_get_objects_with_filters_not_equal(self): |
| 47 |
|
| 48 |
class FakeColumn: |
| 49 |
def __init__(self, column): |
| 50 |
self.column = column |
| 51 |
|
| 52 |
def __ne__(self, value): |
| 53 |
return [item for item in self.column if item != value] |
| 54 |
|
| 55 |
filter_obj = obj_utils.NotEqual(1) |
| 56 |
fake_column = FakeColumn([1, 2, 4, 5]) |
| 57 |
self.assertEqual([2, 4, 5], |
| 58 |
sorted(filter_obj.filter(fake_column))) |
| 59 |
|
| 60 |
fake_column = FakeColumn([1]) |
| 61 |
self.assertEqual([], filter_obj.filter(fake_column)) |
| 62 |
|
| 63 |
fake_column = FakeColumn([4, 5]) |
| 64 |
self.assertEqual([4, 5], |
| 65 |
sorted(filter_obj.filter(fake_column))) |
| 66 |
|
| 67 |
def test_get_updatable_fields(self): |
| 68 |
mock_class = mock.Mock() |
| 69 |
mock_class.fields_no_update = [0, 2, 6] |
| 70 |
|
| 71 |
mock_fields = mock.Mock() |
| 72 |
mock_fields.copy.return_value = {k: k for k in range(7)} |
| 73 |
|
| 74 |
updatable = obj_utils.get_updatable_fields(mock_class, mock_fields) |
| 75 |
self.assertEqual([1, 3, 4, 5], |
| 76 |
sorted(list(updatable.keys()))) |