How to use provide_default_values method in autotest

Best Python code snippet using autotest_python

rdb_model_extensions.py

Source:rdb_model_extensions.py Github

copy

Full Screen

...57 if type(field) == dbmodels.BooleanField and field.name in data:58 data[field.name] = bool(data[field.name])59 # TODO(showard) - is there a way to not have to do this?60 @classmethod61 def provide_default_values(cls, data):62 """\63 Provide default values for fields with default values which have64 nothing passed in.65 For CharField and TextField fields with "blank=True", if nothing66 is passed, we fill in an empty string value, even if there's no67 :retab default set.68 """69 new_data = dict(data)70 field_dict = cls.get_field_dict()71 for name, obj in field_dict.iteritems():72 if data.get(name) is not None:73 continue74 if obj.default is not dbmodels.fields.NOT_PROVIDED:75 new_data[name] = obj.default76 elif (isinstance(obj, dbmodels.CharField) or77 isinstance(obj, dbmodels.TextField)):78 new_data[name] = ''79 return new_data80 @classmethod81 def validate_field_names(cls, data):82 'Checks for extraneous fields in data.'83 errors = {}84 field_dict = cls.get_field_dict()85 for field_name in data:86 if field_name not in field_dict:87 errors[field_name] = 'No field of this name'88 return errors89 @classmethod90 def prepare_data_args(cls, data):91 'Common preparation for add_object and update_object'92 # must check for extraneous field names here, while we have the93 # data in a dict94 errors = cls.validate_field_names(data)95 if errors:96 raise django_exceptions.ValidationError(errors)97 return data98 @classmethod99 def _get_required_field_names(cls):100 """Get the fields without which we cannot create a host.101 @return: A list of field names that cannot be blank on host creation.102 """103 return [field.name for field in cls._meta.fields if not field.blank]104 @classmethod105 def get_basic_field_names(cls):106 """Get all basic fields of the Model.107 This method returns the names of all fields that the client can provide108 a value for during host creation. The fields not included in this list109 are those that we can leave blank. Specifying non-null values for such110 fields only makes sense as an update to the host.111 @return A list of basic fields.112 Eg: set([hostname, locked, leased, status, invalid,113 protection, lock_time, dirty])114 """115 return [field.name for field in cls._meta.fields116 if field.has_default()] + cls._get_required_field_names()117 @classmethod118 def validate_model_fields(cls, data):119 """Validate parameters needed to create a host.120 Check that all required fields are specified, that specified fields121 are actual model values, and provide defaults for the unspecified122 but unrequired fields.123 @param dict: A dictionary with the args to create the model.124 @raises dajngo_exceptions.ValidationError: If either an invalid field125 is specified or a required field is missing.126 """127 missing_fields = set(cls._get_required_field_names()) - set(data.keys())128 if missing_fields:129 raise django_exceptions.ValidationError('%s required to create %s, '130 'supplied %s ' % (missing_fields, cls.__name__, data))131 data = cls.prepare_data_args(data)132 data = cls.provide_default_values(data)133 return data134class AbstractHostModel(dbmodels.Model, ModelValidators):135 """Abstract model specifying all fields one can use to create a host.136 This model enforces consistency between the host models of the rdb and137 their representation on the client side.138 Internal fields:139 status: string describing status of host140 invalid: true if the host has been deleted141 protection: indicates what can be done to this host during repair142 lock_time: DateTime at which the host was locked143 dirty: true if the host has been used without being rebooted144 lock_reason: The reason for locking the host.145 """146 Status = host_states.Status...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful