Merge "Add initialization in StringMatchingFilterObj class"

This commit is contained in:
Zuul 2017-11-03 20:21:02 +00:00 committed by Gerrit Code Review
commit f72db04df7
3 changed files with 41 additions and 5 deletions

View File

@ -73,7 +73,7 @@ using example of DNSNameServer:
# for fetching objects with substrings in a string field:
from neutron.objects import utils as obj_utils
dnses = DNSNameServer.get_objects(context, address=obj_utils.StringMatchingContains('10.0.0'))
dnses = DNSNameServer.get_objects(context, address=obj_utils.StringContains('10.0.0'))
# will return list of all dns name servers from DB that has '10.0.0' in their addresses
# to update fields:

View File

@ -26,18 +26,17 @@ def convert_filters(**kwargs):
class StringMatchingFilterObj(object):
@property
def is_contains(self):
return bool(getattr(self, "contains"))
return bool(getattr(self, "contains", False))
@property
def is_starts(self):
return bool(getattr(self, "starts"))
return bool(getattr(self, "starts", False))
@property
def is_ends(self):
return bool(getattr(self, "ends"))
return bool(getattr(self, "ends", False))
class StringContains(StringMatchingFilterObj):

View File

@ -20,6 +20,7 @@ from neutron.db import _model_query as model_query
from neutron.db import models_v2
from neutron.objects import base
from neutron.objects.db import api
from neutron.objects import utils as obj_utils
from neutron.tests import base as test_base
from neutron.tests.unit import testlib_api
@ -98,6 +99,42 @@ class CRUDScenarioTestCase(testlib_api.SqlTestCase):
self.ctxt, self.model, name='foo', status=None)
self.assertEqual(obj, new_objs[0])
def test_get_objects_with_string_matching_filters_contains(self):
obj1 = api.create_object(self.ctxt, self.model, {'name': 'obj_con_1'})
obj2 = api.create_object(self.ctxt, self.model, {'name': 'obj_con_2'})
obj3 = api.create_object(self.ctxt, self.model, {'name': 'obj_3'})
objs = api.get_objects(
self.ctxt, self.model, name=obj_utils.StringContains('con'))
self.assertEqual(2, len(objs))
self.assertIn(obj1, objs)
self.assertIn(obj2, objs)
self.assertNotIn(obj3, objs)
def test_get_objects_with_string_matching_filters_starts(self):
obj1 = api.create_object(self.ctxt, self.model, {'name': 'pre_obj1'})
obj2 = api.create_object(self.ctxt, self.model, {'name': 'pre_obj2'})
obj3 = api.create_object(self.ctxt, self.model, {'name': 'obj_3'})
objs = api.get_objects(
self.ctxt, self.model, name=obj_utils.StringStarts('pre'))
self.assertEqual(2, len(objs))
self.assertIn(obj1, objs)
self.assertIn(obj2, objs)
self.assertNotIn(obj3, objs)
def test_get_objects_with_string_matching_filters_ends(self):
obj1 = api.create_object(self.ctxt, self.model, {'name': 'obj1_end'})
obj2 = api.create_object(self.ctxt, self.model, {'name': 'obj2_end'})
obj3 = api.create_object(self.ctxt, self.model, {'name': 'obj_3'})
objs = api.get_objects(
self.ctxt, self.model, name=obj_utils.StringEnds('end'))
self.assertEqual(2, len(objs))
self.assertIn(obj1, objs)
self.assertIn(obj2, objs)
self.assertNotIn(obj3, objs)
def test_get_object_create_update_delete(self):
obj = api.create_object(self.ctxt, self.model, {'name': 'foo'})