/usr/lib/python2.7/dist-packages/neutron_lbaas/tests/unit/services/loadbalancer/test_data_models.py is in python-neutron-lbaas 1:9.1.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | # 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 inspect
import mock
import testscenarios
from neutron_lbaas.services.loadbalancer import data_models
from neutron_lbaas.tests import base
from neutron_lbaas.tests import tools
load_tests = testscenarios.load_tests_apply_scenarios
class TestBaseDataModel(base.BaseTestCase):
def _get_fake_model_cls(self, fields_):
class FakeModel(data_models.BaseDataModel):
fields = fields_
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
return FakeModel
def test_from_dict(self):
fields_ = ['field1', 'field2']
dict_ = {field: tools.get_random_string()
for field in fields_}
model_cls = self._get_fake_model_cls(fields_)
model = model_cls.from_dict(dict_)
for field in fields_:
self.assertEqual(dict_[field], getattr(model, field))
def test_from_dict_filters_by_fields(self):
fields_ = ['field1', 'field2']
dict_ = {field: tools.get_random_string()
for field in fields_}
dict_['foo'] = 'bar'
model_cls = self._get_fake_model_cls(fields_)
model = model_cls.from_dict(dict_)
self.assertFalse(hasattr(model, 'foo'))
def _get_models():
models = []
for name, obj in inspect.getmembers(data_models):
if inspect.isclass(obj):
if issubclass(obj, data_models.BaseDataModel):
if type(obj) != data_models.BaseDataModel:
models.append(obj)
return models
class TestModels(base.BaseTestCase):
scenarios = [
(model.__name__, {'model': model})
for model in _get_models()
]
@staticmethod
def _get_iterable_mock(*args, **kwargs):
m = mock.create_autospec(dict, spec_set=True)
def _get_empty_iterator(*args, **kwargs):
return iter([])
m.__iter__ = _get_empty_iterator
m.pop = _get_empty_iterator
return m
def test_from_dict_filters_by_fields(self):
dict_ = {field: self._get_iterable_mock()
for field in self.model.fields}
dict_['foo'] = 'bar'
model = self.model.from_dict(dict_)
self.assertFalse(hasattr(model, 'foo'))
|