From 2676372f261da202f41ab8e2d175be27b9405261 Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Sat, 10 Sep 2016 06:07:58 +0000 Subject: [PATCH] tests: don't override base object test class attributes DNSNameServerDbObjectTestCase was overriding self.db_objs and self.obj_fields if the attributes did not have unique order/address fields generated by get_random_fields. But since Id1ca4ce7b134d9729e68661cedb2f5556e58d6ff landed, we should have also updated self.objs, otherwise test_filtering_by_fields will fail later when it will try to find an object with attributes that were not used when creating the object targeted by the filtering attempt. Instead of adding the update for self.objs in the DNSNameServerDbObjectTestCase test class, I went with an alternative approach, getting rid of overriding logic completely. The rationale for the path is that there is nothing wrong in duplicate address and order field values (at least as per underlying model definition), and hence our tests should be resilient against that kind of scenario. So instead of comparing all fields for an object, just make sure that the order monotonically goes up/down in the sorted result, and ignore other fields to be strictly ordered. Change-Id: Ic956072de5dab336f83b04bddfa9da967b2865b2 Closes-Bug: #1623425 --- neutron/tests/unit/objects/test_subnet.py | 36 +++++------------------ 1 file changed, 7 insertions(+), 29 deletions(-) diff --git a/neutron/tests/unit/objects/test_subnet.py b/neutron/tests/unit/objects/test_subnet.py index 0706c4512c0..e0f5dc4a68c 100644 --- a/neutron/tests/unit/objects/test_subnet.py +++ b/neutron/tests/unit/objects/test_subnet.py @@ -11,7 +11,6 @@ # under the License. import itertools -from operator import itemgetter from oslo_utils import uuidutils @@ -60,24 +59,11 @@ class DNSNameServerDbObjectTestCase(obj_test_base.BaseDbObjectTestCase, def setUp(self): super(DNSNameServerDbObjectTestCase, self).setUp() - # (NOTE) If two object have the same value for a field and - # they are sorted using that field, the order is not deterministic. - # To avoid breaking the tests we ensure unique values for every field - while not self._is_objects_unique(): - self.db_objs = list(self.get_random_fields() for _ in range(3)) - self.obj_fields = [self._test_class.modify_fields_from_db(db_obj) - for db_obj in self.db_objs] self._create_test_network() self._create_test_subnet(self._network) for obj in itertools.chain(self.db_objs, self.obj_fields, self.objs): obj['subnet_id'] = self._subnet['id'] - def _is_objects_unique(self): - order_set = set([x['order'] for x in self.db_objs]) - subnet_id_set = set([x['subnet_id'] for x in self.db_objs]) - address_set = set([x['address'] for x in self.db_objs]) - return 3 == len(order_set) == len(subnet_id_set) == len(address_set) - def _create_dnsnameservers(self): for obj in self.obj_fields: dns = self._make_object(obj) @@ -86,32 +72,24 @@ class DNSNameServerDbObjectTestCase(obj_test_base.BaseDbObjectTestCase, def test_get_objects_sort_by_order_asc(self): self._create_dnsnameservers() objs = self._test_class.get_objects(self.context) - fields_sorted = sorted([dict(obj) for obj in self.obj_fields], - key=itemgetter('order')) - self.assertEqual( - fields_sorted, - [obj_test_base.get_obj_db_fields(obj) for obj in objs]) + fields_sorted = sorted([obj['order'] for obj in self.obj_fields]) + self.assertEqual(fields_sorted, [obj.order for obj in objs]) def test_get_objects_sort_by_order_desc(self): self._create_dnsnameservers() pager = obj_base.Pager(sorts=[('order', False)]) objs = self._test_class.get_objects(self.context, _pager=pager, subnet_id=self._subnet.id) - fields_sorted = sorted([dict(obj) for obj in self.obj_fields], - key=itemgetter('order'), reverse=True) - self.assertEqual( - fields_sorted, - [obj_test_base.get_obj_db_fields(obj) for obj in objs]) + fields_sorted = sorted([obj['order'] for obj in self.obj_fields], + reverse=True) + self.assertEqual(fields_sorted, [obj.order for obj in objs]) def test_get_objects_sort_by_address_asc_using_pager(self): self._create_dnsnameservers() pager = obj_base.Pager(sorts=[('address', True)]) objs = self._test_class.get_objects(self.context, _pager=pager) - fields_sorted = sorted([dict(obj) for obj in self.obj_fields], - key=itemgetter('address')) - self.assertEqual( - fields_sorted, - [obj_test_base.get_obj_db_fields(obj) for obj in objs]) + fields_sorted = sorted([obj['address'] for obj in self.obj_fields]) + self.assertEqual(fields_sorted, [obj.address for obj in objs]) class RouteObjectIfaceTestCase(obj_test_base.BaseObjectIfaceTestCase):