def validate_content(response, comparisions): """Asserts expected value with actual value using JMES path expression Args: response (Response): reqeusts.Response object. comparisions(list): A list of dict containing the following keys: 1. jmespath : JMES path expression to extract data from. 2. operator : Operator to use to compare data. 3. expected : The expected value to match for """ for each_comparision in comparisions: path, _operator, expected = validate_comparision(each_comparision) logger.debug("Searching for '%s' in '%s'", path, response.json()) actual = jmespath.search(path, response.json()) if actual is None: raise exceptions.JMESError( "JMES path '{}' not found in response".format(path)) expession = " ".join([str(path), str(_operator), str(expected)]) parsed_expession = " ".join( [str(actual), str(_operator), str(expected)]) if _operator == "eq" and 0: check_keys_match_recursive(expected, actual, []) else: try: actual_validation(_operator, actual, expected, parsed_expession, expession) except AssertionError as e: raise_from(exceptions.JMESError("Error validating JMES"), e)
def check_jmespath_match(parsed_response, query, expected=None): """ Check that the JMES path given in 'query' is present in the given response Args: parsed_response (dict, list): Response list or dict query (str): JMES query expected (str, optional): Possible value to match against. If None, 'query' will just check that _something_ is present """ actual = jmespath.search(query, parsed_response) msg = "JMES path '{}' not found in response".format(query) if actual is None: raise exceptions.JMESError(msg) if expected is not None: # Reuse dict util helper as it should behave the same check_keys_match_recursive(expected, actual, [], True) elif not actual and not (actual == expected): # pylint: disable=superfluous-parens # This can return an empty list, but it might be what we expect. if not, # raise an exception raise exceptions.JMESError(msg) return actual
def actual_validation(_operator, _actual, expected, _expression, expression): if not COMPARATORS[_operator](_actual, expected): raise exceptions.JMESError("Validation '{}' ({}) failed!".format( expression, _expression))