get_random_object_fields() for setting object attr

This patch is to introduce a modified get_random_fields() method to
set the attributes of an object as currently get_random_fields() returns
the modified fields which are suitable to be stored in the database,
however when fields of the objects are assigned with these values
the type won't match. For example, if an object has a custom type field
like DictOfStringField, get_random_fields() generates dict of string
values, however while returning the values it converts the dict to
a string but the object still expects a DictOfStringField type.

Also, everytime get_random_fields() is used for initializing the fields
of an object, modify_fields_to_db() gets called twice, once in the
get_random_fields() method and once while creating the object.

Change-Id: I8a5fc6603e625b0b98d330f6abd8c6588b1a8c31
This commit is contained in:
Sindhu Devale 2016-12-05 14:58:12 +00:00 committed by Artur Korzeniewski
parent ef6a3d8789
commit ec153ee9d9
4 changed files with 20 additions and 20 deletions

View File

@ -40,15 +40,15 @@ class QosPolicyObjectTestCase(test_base.BaseObjectIfaceTestCase):
super(QosPolicyObjectTestCase, self).setUp()
# qos_policy_ids will be incorrect, but we don't care in this test
self.db_qos_bandwidth_rules = [
self.get_random_fields(rule.QosBandwidthLimitRule)
self.get_random_db_fields(rule.QosBandwidthLimitRule)
for _ in range(3)]
self.db_qos_dscp_rules = [
self.get_random_fields(rule.QosDscpMarkingRule)
self.get_random_db_fields(rule.QosDscpMarkingRule)
for _ in range(3)]
self.db_qos_minimum_bandwidth_rules = [
self.get_random_fields(rule.QosMinimumBandwidthRule)
self.get_random_db_fields(rule.QosMinimumBandwidthRule)
for _ in range(3)]
self.model_map.update({
@ -137,7 +137,7 @@ class QosPolicyDbObjectTestCase(test_base.BaseDbObjectTestCase,
rules = []
for obj_cls in (RULE_OBJ_CLS.get(rule_type)
for rule_type in rule_type):
rule_fields = self.get_random_fields(obj_cls=obj_cls)
rule_fields = self.get_random_object_fields(obj_cls=obj_cls)
rule_fields['qos_policy_id'] = policy_obj.id
rule_obj = obj_cls(self.context, **rule_fields)
rule_obj.create()

View File

@ -464,7 +464,7 @@ class _BaseObjectTestCase(object):
utils.import_modules_recursively(os.path.dirname(objects.__file__))
self.context = context.get_admin_context()
self.db_objs = [
self._test_class.db_model(**self.get_random_fields())
self._test_class.db_model(**self.get_random_db_fields())
for _ in range(3)
]
@ -485,14 +485,13 @@ class _BaseObjectTestCase(object):
self.obj_registry.register(FakeSmallNeutronObject)
self.obj_registry.register(FakeWeirdKeySmallNeutronObject)
self.obj_registry.register(FakeNeutronObjectMultipleForeignKeys)
synthetic_obj_fields = self.get_random_fields(FakeSmallNeutronObject)
synthetic_obj_fields = self.get_random_db_fields(
FakeSmallNeutronObject)
self.model_map = {
self._test_class.db_model: self.db_objs,
ObjectFieldsModel: [ObjectFieldsModel(**synthetic_obj_fields)]}
# TODO(ihrachys): rename the method to explicitly reflect it returns db
# attributes not object fields
def get_random_fields(self, obj_cls=None):
def get_random_object_fields(self, obj_cls=None):
obj_cls = obj_cls or self._test_class
fields = {}
ip_version = tools.get_random_ip_version()
@ -500,7 +499,12 @@ class _BaseObjectTestCase(object):
if field not in obj_cls.synthetic_fields:
generator = FIELD_TYPE_VALUE_GENERATOR_MAP[type(field_obj)]
fields[field] = get_value(generator, ip_version)
return obj_cls.modify_fields_to_db(fields)
return fields
def get_random_db_fields(self, obj_cls=None):
obj_cls = obj_cls or self._test_class
return obj_cls.modify_fields_to_db(
self.get_random_object_fields(obj_cls))
def update_obj_fields(self, values_dict,
db_objs=None, obj_fields=None, objs=None):
@ -981,7 +985,7 @@ class BaseObjectIfaceTestCase(_BaseObjectTestCase, test_base.BaseTestCase):
child = objclass(
self.context, **objclass.modify_fields_from_db(
self.get_random_fields(obj_cls=objclass))
self.get_random_db_fields(obj_cls=objclass))
)
child_dict = child.to_dict()
if isinstance(cls_.fields[field], obj_fields.ListOfObjectsField):
@ -1119,7 +1123,7 @@ class BaseDbObjectMultipleParentsForForeignKeysTestCase(
fake_children = [
child_cls(
self.context, **child_cls.modify_fields_from_db(
self.get_random_fields(obj_cls=child_cls))
self.get_random_db_fields(obj_cls=child_cls))
)
for _ in range(5)
]
@ -1142,7 +1146,7 @@ class BaseDbObjectTestCase(_BaseObjectTestCase,
if not objclass:
continue
for db_obj in self.db_objs:
objclass_fields = self.get_random_fields(objclass)
objclass_fields = self.get_random_db_fields(objclass)
if isinstance(self._test_class.fields[synth_field],
obj_fields.ObjectField):
db_obj[synth_field] = objclass.db_model(**objclass_fields)

View File

@ -132,7 +132,7 @@ class PortBindingVifDetailsTestCase(testscenarios.WithScenarios,
# the null case for vif_details in our db model is an
# empty string. add that here to simulate it correctly
# in the tests
kwargs = self.get_random_fields()
kwargs = self.get_random_db_fields()
kwargs['vif_details'] = ''
db_obj = self._test_class.db_model(**kwargs)
obj_fields = self._test_class.modify_fields_from_db(db_obj)

View File

@ -110,9 +110,7 @@ class DefaultSecurityGroupDbObjTestCase(test_base.BaseDbObjectTestCase,
def setUp(self):
super(DefaultSecurityGroupDbObjTestCase, self).setUp()
sg_db_obj = self.get_random_fields(securitygroup.SecurityGroup)
sg_fields = securitygroup.SecurityGroup.modify_fields_from_db(
sg_db_obj)
sg_fields = self.get_random_object_fields(securitygroup.SecurityGroup)
self.sg_obj = securitygroup.SecurityGroup(self.context, **sg_fields)
self.sg_obj.create()
self.update_obj_fields({'security_group_id': self.sg_obj['id']})
@ -130,9 +128,7 @@ class SecurityGroupRuleDbObjTestCase(test_base.BaseDbObjectTestCase,
def setUp(self):
super(SecurityGroupRuleDbObjTestCase, self).setUp()
sg_db_obj = self.get_random_fields(securitygroup.SecurityGroup)
sg_fields = securitygroup.SecurityGroup.modify_fields_from_db(
sg_db_obj)
sg_fields = self.get_random_object_fields(securitygroup.SecurityGroup)
self.sg_obj = securitygroup.SecurityGroup(self.context, **sg_fields)
self.sg_obj.create()
self.update_obj_fields({'security_group_id': self.sg_obj['id'],