Merge "objects: add validate_filters option for count() method."

This commit is contained in:
Jenkins 2016-10-21 14:30:07 +00:00 committed by Gerrit Code Review
commit 378d4625cd
2 changed files with 30 additions and 4 deletions

View File

@ -221,9 +221,9 @@ class NeutronObject(obj_base.VersionedObject,
raise NotImplementedError()
@classmethod
def count(cls, context, **kwargs):
def count(cls, context, validate_filters=True, **kwargs):
'''Count the number of objects matching filtering criteria.'''
return len(cls.get_objects(context, **kwargs))
return len(cls.get_objects(context, validate_filters, **kwargs))
def _detach_db_obj(func):
@ -609,15 +609,18 @@ class NeutronDbObject(NeutronObject):
self._captured_db_model = None
@classmethod
def count(cls, context, **kwargs):
def count(cls, context, validate_filters=True, **kwargs):
"""
Count the number of objects matching filtering criteria.
:param context:
:param validate_filters: Raises an error in case of passing an unknown
filter
:param kwargs: multiple keys defined by key=value pairs
:return: number of matching objects
"""
cls.validate_filters(**kwargs)
if validate_filters:
cls.validate_filters(**kwargs)
return obj_db_api.count(
context, cls.db_model, **cls.modify_fields_to_db(kwargs)
)

View File

@ -732,6 +732,15 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase):
self.assertItemsEqual(get_obj_db_fields(expected),
get_obj_db_fields(observed))
def test_count_validate_filters_false(self):
if not isinstance(self._test_class, base.NeutronDbObject):
self.skipTest('Class %s does not inherit from NeutronDbObject' %
self._test_class)
expected = 10
with mock.patch.object(obj_db_api, 'count', return_value=expected):
self.assertEqual(expected, self._test_class.count(self.context,
validate_filters=False, fake_field='xxx'))
def test_create(self):
with mock.patch.object(obj_db_api, 'create_object',
return_value=self.db_objs[0]) as create_mock:
@ -1388,6 +1397,20 @@ class BaseDbObjectTestCase(_BaseObjectTestCase,
self.assertEqual(
len(self.obj_fields), self._test_class.count(self.context))
def test_count_validate_filters_false(self):
for fields in self.obj_fields:
self._make_object(fields).create()
self.assertEqual(
len(self.obj_fields), self._test_class.count(self.context,
validate_filters=False, fake_filter='xxx'))
def test_count_invalid_filters(self):
for fields in self.obj_fields:
self._make_object(fields).create()
self.assertRaises(base.exceptions.InvalidInput,
self._test_class.count, self.context,
fake_field='xxx')
def test_db_obj(self):
obj = self._make_object(self.obj_fields[0])
self.assertIsNone(obj.db_obj)