How to use _validate_structure method in localstack

Best Python code snippet using localstack_python

blocks.py

Source:blocks.py Github

copy

Full Screen

...119 return self.header.assign120 def validate(self, context):121 self._validate_body()122 if self.type == Token.IF:123 self._validate_structure()124 self._validate_end()125 if self.type == Token.INLINE_IF:126 self._validate_structure()127 self._validate_inline_if()128 def _validate_body(self):129 if not self.body:130 type = self.type if self.type != Token.INLINE_IF else 'IF'131 self.errors += (f'{type} branch cannot be empty.',)132 def _validate_structure(self):133 orelse = self.orelse134 else_seen = False135 while orelse:136 if else_seen:137 if orelse.type == Token.ELSE:138 error = 'Multiple ELSE branches.'139 else:140 error = 'ELSE IF after ELSE.'141 if error not in self.errors:142 self.errors += (error,)143 else_seen = else_seen or orelse.type == Token.ELSE144 orelse = orelse.orelse145 def _validate_end(self):146 if not self.end:147 self.errors += ('IF has no closing END.',)148 def _validate_inline_if(self):149 branch = self150 assign = branch.assign151 while branch:152 if branch.body:153 item = branch.body[0]154 if assign and item.type != Token.KEYWORD:155 self.errors += ('Inline IF with assignment can only contain '156 'keyword calls.',)157 if getattr(item, 'assign', None):158 self.errors += ('Inline IF branches cannot contain assignments.',)159 if item.type == Token.INLINE_IF:160 self.errors += ('Inline IF cannot be nested.',)161 branch = branch.orelse162class For(Block):163 _fields = ('header', 'body', 'end')164 def __init__(self, header, body=None, end=None, errors=()):165 self.header = header166 self.body = body or []167 self.end = end168 self.errors = errors169 @property170 def variables(self):171 return self.header.variables172 @property173 def values(self):174 return self.header.values175 @property176 def flavor(self):177 return self.header.flavor178 def validate(self, context):179 if not self.body:180 self.errors += ('FOR loop has empty body.',)181 if not self.end:182 self.errors += ('FOR loop has no closing END.',)183class Try(Block):184 _fields = ('header', 'body', 'next', 'end')185 def __init__(self, header, body=None, next=None, end=None, errors=()):186 self.header = header187 self.body = body or []188 self.next = next189 self.end = end190 self.errors = errors191 @property192 def type(self):193 return self.header.type194 @property195 def patterns(self):196 return getattr(self.header, 'patterns', ())197 @property198 def variable(self):199 return getattr(self.header, 'variable', None)200 def validate(self, context):201 self._validate_body()202 if self.type == Token.TRY:203 self._validate_structure()204 self._validate_end()205 def _validate_body(self):206 if not self.body:207 self.errors += (f'{self.type} branch cannot be empty.',)208 def _validate_structure(self):209 else_count = 0210 finally_count = 0211 except_count = 0212 empty_except_count = 0213 branch = self.next214 while branch:215 if branch.type == Token.EXCEPT:216 if else_count:217 self.errors += ('EXCEPT not allowed after ELSE.',)218 if finally_count:219 self.errors += ('EXCEPT not allowed after FINALLY.',)220 if branch.patterns and empty_except_count:221 self.errors += ('EXCEPT without patterns must be last.',)222 if not branch.patterns:...

Full Screen

Full Screen

response_validator.py

Source:response_validator.py Github

copy

Full Screen

...77 body_to_verify = getattr(self, f'{property_name}')78 model_data = getattr(model, property_name)79 # Verify basic validation rules and perform response validation80 if self._check_status_code:81 self._validate_structure('status_code', self.status_code, model.status_code)82 if self._check_headers:83 self._validate_structure('headers', self.headers, model.headers)84 if self._check_body:85 self._validate_structure(property_name, body_to_verify, model_data)86 if self.custom_checkers:87 self._run_checkers()88 if self.errors["default"] or self.errors["custom"]:89 return False90 return True91 def _validate_structure(self, field_name, data_to_verify, model_to_verify, list_position=None):92 """Recursively validates provided data_to_verify based on the model_to_verify93 If value is SKIP - exits from function94 If value is instance of dict - calls itself for each key95 If value is instance of list - calls itself for each element in list96 If value is custom object or primitive data type - asserts data with model using native __eq__97 In case if assertion fails - append error with field name to self.errors["default"] list98 Args:99 field_name (str): Field name to validated100 data_to_verify (Any): Data to verify101 model_to_verify (Any): Model to verify the data102 """103 if list_position is None:104 self.field_nesting.append(field_name)105 if model_to_verify == SKIP:106 if list_position is None:107 self.field_nesting.pop()108 return109 if isinstance(data_to_verify, dict):110 # if for cases when model or data is empty dict111 if model_to_verify and data_to_verify:112 for key, value in model_to_verify.items():113 try:114 self._validate_structure(key, data_to_verify[key], model_to_verify[key])115 except KeyError:116 # for case when key is absent in response. If _check_is_field_missing is False117 # logs warning message118 if self._check_is_field_missing:119 self.field_nesting.append(key)120 self.errors["default"].append((copy(self.field_nesting), model_to_verify[key],121 "field does not present in response"))122 self.field_nesting.pop()123 else:124 logger.warning(f"The field '{key}' is not present in response. Please verify your model")125 elif self._check_is_field_missing:126 self.errors["default"].append((copy(self.field_nesting), model_to_verify, data_to_verify))127 else:128 logger.warning(f"Expected '{model_to_verify}' in response, but got '{data_to_verify}'. "129 f"Please verify your model")130 elif isinstance(data_to_verify, list):131 # if for cases when model or data is empty list132 if model_to_verify and data_to_verify:133 for number, item in enumerate(model_to_verify):134 # add element position in list135 self.field_nesting.append(number)136 try:137 self._validate_structure(field_name, data_to_verify[number], model_to_verify[number], number)138 self.field_nesting.pop()139 except IndexError:140 # if there is no element in list141 self.errors["default"].append((copy(self.field_nesting), model_to_verify, data_to_verify))142 # for cases when expected empty list143 elif not model_to_verify and not data_to_verify:144 pass145 else:146 self.errors["default"].append((copy(self.field_nesting), model_to_verify, data_to_verify))147 else:148 if not model_to_verify == data_to_verify:149 self.errors["default"].append((copy(self.field_nesting), model_to_verify, data_to_verify))150 if list_position is None:151 self.field_nesting.pop()...

Full Screen

Full Screen

mapval.py

Source:mapval.py Github

copy

Full Screen

...54 paths.extend(MappingValidator._find_all_paths(sample[key],55 previous_path=current_path))56 paths.append(current_path)57 return sorted(paths, key=lambda k: len(k))58 def _validate_structure(self, mapping):59 """60 Controls the style of the structure-validation(min,max,eq)61 ComparisonStyle.minimum: Are all paths of the reference included in the mapping?62 ComparisonStyle.maximum: Are all paths of the mapping included in the reference?63 ComparisonStyle.equity: Are the paths of the mapping and the reference equal?64 :param mapping: Mapping that is checked.65 :return: True if the structure matches, false otherwise.66 """67 #Call _compare_structure according to comparison_style68 if self._comparison_style == ComparisonStyle.minimum:69 return MappingValidator._compare_structure(mapping, self._reference)70 elif self._comparison_style == ComparisonStyle.maximum:71 return MappingValidator._compare_structure(self._reference, mapping)72 else:73 return MappingValidator._compare_structure(mapping, self._reference) \74 and MappingValidator._compare_structure(self._reference,75 mapping)76 @staticmethod77 def _compare_structure(sample, reference):78 """79 Checks if the structure of sample at most matches the structure of reference.80 :param sample: The mapping that is going to be validate81 :param reference: The mapping that specifies the structure82 :return: True if sample matches the structure of reference, false otherwise83 """84 paths = MappingValidator._find_all_paths(reference)85 result = True86 for path in paths:87 result = result and MappingValidator._validate_key(sample, path)88 if not result:89 break90 return result91 @staticmethod92 def _validate_key(sample, path):93 """94 Checks if the given path exists in sample95 :param path: Path to a key in the mapping.96 :return: True if path exists, false otherwise.97 """98 mapping_tmp = sample99 for key in path:100 try:101 mapping_tmp = mapping_tmp[key]102 except KeyError:103 return False104 except TypeError:105 return False106 return True107 def _validate_values(self, sample):108 """109 Validate all values in mapping if they match their reference-value.110 :param sample: Mapping that is going to be validated.111 :return: True if all values in sample.112 """113 result = True114 paths = []115 #Search vor necessary paths accorduing to comparison_style116 if self._comparison_style == ComparisonStyle.minimum:117 paths = self._find_all_paths(self._reference)118 else:119 paths = self._find_all_paths(sample)120 # For every path, if it is endling in an key, validate the key121 for path in paths:122 reference_value = MappingValidator._get_value(self._reference,123 list(path))124 mapping_value = MappingValidator._get_value(sample, list(path))125 if isinstance(mapping_value, abc.Mapping):126 continue127 elif isinstance(reference_value, type):128 result = result and isinstance(mapping_value, reference_value)129 elif callable(reference_value):130 result = result and bool(reference_value(mapping_value))131 elif isinstance(reference_value, re._pattern_type):132 result = result and bool(reference_value.match(mapping_value))133 elif isinstance(reference_value, list):134 list_contains_sample_val = False135 for possibility in reference_value:136 if possibility == mapping_value:137 list_contains_sample_val = True138 break139 result = result and list_contains_sample_val140 elif reference_value is Ellipsis:141 result = result and True142 else:143 result = result and False144 if not result:145 break146 return result147 @staticmethod148 def _get_value(sample, path):149 """150 Search the the value from the given path in the given mapping151 :param sample: Mapping to get the value from.152 :param path: Path to the key.153 :return: The value stored in the mapping at position key.154 """155 if len(path) > 1:156 return MappingValidator._get_value(sample[path.pop(0)], path)157 else:158 return sample[path.pop(0)]159 def validate(self, sample, validate_content=True):160 """161 Validate sample against reference.162 :param sample: Mapping that is going to be validated.163 :param validate_content: Validate structure and the values, or just validate the structure.164 :return: True if sample is valid, False otherwise.165 """166 if validate_content:167 result = self._validate_structure(sample) and self._validate_values(sample)168 return result169 else:170 return self._validate_structure(sample)171if __name__ == '__main__':...

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 localstack 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