def do_validation(self, validator_dict): """ validate with functions """ comparator = utils.get_uniform_comparator(validator_dict["comparator"]) validate_func = self.testcase_parser.get_bind_item( "function", comparator) if not validate_func: raise exception.FunctionNotFound( "comparator not found: {}".format(comparator)) check_item = validator_dict["check"] check_value = validator_dict["check_value"] expect_value = validator_dict["expect"] if (check_value is None or expect_value is None) \ and comparator not in ["is", "eq", "equals", "=="]: raise exception.ParamsError( "Null value can only be compared with comparator: eq/equals/==" ) try: validate_func(validator_dict["check_value"], validator_dict["expect"]) except (AssertionError, TypeError): err_msg = "\n" + "\n".join([ "\tcheck item name: %s;" % check_item, "\tcheck item value: %s (%s);" % (check_value, type(check_value).__name__), "\tcomparator: %s;" % comparator, "\texpected value: %s (%s)." % (expect_value, type(expect_value).__name__) ]) raise exception.ValidationError(err_msg)
def do_validation(self, validator_dict): """ validate with functions """ # TODO: move comparator uniform to init_task_suite comparator = utils.get_uniform_comparator(validator_dict["comparator"]) validate_func = self.testcase_parser.get_bind_function(comparator) if not validate_func: raise exception.FunctionNotFound("comparator not found: {}".format(comparator)) check_item = validator_dict["check"] check_value = validator_dict["check_value"] expect_value = validator_dict["expect"] if (check_value is None or expect_value is None) \ and comparator not in ["is", "eq", "equals", "=="]: raise exception.ParamsError("Null value can only be compared with comparator: eq/equals/==") try: validate_func(validator_dict["check_value"], validator_dict["expect"]) except (AssertionError, TypeError): err_msg = "\n" + "\n".join([ "\tcheck item name: %s;" % check_item, "\tcheck item value: %s (%s);" % (check_value, type(check_value).__name__), "\tcomparator: %s;" % comparator, "\texpected value: %s (%s)." % (expect_value, type(expect_value).__name__) ]) raise exception.ValidationError(err_msg)
def do_validation(self, validator_dict): """ validate with functions """ comparator = utils.get_uniform_comparator(validator_dict["comparator"]) validate_func = self.testcase_parser.get_bind_item("function", comparator) if not validate_func: raise exception.FunctionNotFound("comparator not found: {}".format(comparator)) check_item = validator_dict["check_item"] check_value = validator_dict["check_value"] expect_value = validator_dict["expect_value"] try: if check_value is None or expect_value is None: assert comparator in ["is", "eq", "equals", "=="] validate_func(validator_dict["check_value"], validator_dict["expect_value"]) except (AssertionError, TypeError): err_msg = "\n" + "\n".join([ "\tcheck item name: %s;" % check_item, "\tcheck item value: %s (%s);" % (check_value, type(check_value).__name__), "\tcomparator: %s;" % comparator, "\texpected value: %s (%s)." % (expect_value, type(expect_value).__name__) ]) raise exception.ValidationError(err_msg)
def _do_validation(self, validator_dict): """ validate with functions Args: validator_dict (dict): validator dict { "check": "status_code", "check_value": 200, "expect": 201, "comparator": "eq" } """ # TODO: move comparator uniform to init_test_suites comparator = utils.get_uniform_comparator(validator_dict["comparator"]) validate_func = self.TESTCASE_SHARED_FUNCTIONS_MAPPING.get(comparator) if not validate_func: raise exceptions.FunctionNotFound( "comparator not found: {}".format(comparator)) check_item = validator_dict["check"] check_value = validator_dict["check_value"] expect_value = validator_dict["expect"] if (check_value is None or expect_value is None) \ and comparator not in ["is", "eq", "equals", "=="]: raise exceptions.ParamsError( "Null value can only be compared with comparator: eq/equals/==" ) validate_msg = "validate: {} {} {}({})".format( check_item, comparator, expect_value, type(expect_value).__name__) try: validator_dict["check_result"] = "pass" validate_func(check_value, expect_value) validate_msg += "\t==> pass" logger.log_debug(validate_msg) except (AssertionError, TypeError): validate_msg += "\t==> fail" validate_msg += "\n{}({}) {} {}({})".format( check_value, type(check_value).__name__, comparator, expect_value, type(expect_value).__name__) logger.log_error(validate_msg) validator_dict["check_result"] = "fail" raise exceptions.ValidationFailure(validate_msg)
def _do_validation(self, validator_dict): ''' validator with functions Args: validator_dict (dict): validator dict { 'check':'status_code', 'check_value':200, 'expect':201, 'comparator':'eq' } ''' comparator = utils.get_uniform_comparator(validator_dict['comparator']) validate_func = self.TESTCASE_SHARED_FUNCTIONS_MAPPING.get(comparator) if not validate_func: raise exceptions.FunctionNotFound( f'comparator not found: {comparator}') check_item = validator_dict['check'] check_value = validator_dict['check_value'] expect_value = validator_dict['expect'] if (check_value is None or expect_value is None) \ and comparator not in ['equals']: raise exceptions.ParamError( 'Null value can only be comparated with comparator: eq/equals/==/is' ) validate_msg = 'validator: {} {} {} {}'.format( check_item, comparator, expect_value, type(expect_value).__name__) try: validator_dict['check_result'] = 'pass' validate_func(check_value, expect_value) validate_msg += '\t==> pass' logger.log_debug(validate_msg) except (AssertionError, TypeError): validate_msg += '\t==> fail' validate_msg += '\n{}({}) {} {}({})'.format( check_value, type(check_value).__name__, comparator, expect_value, type(expect_value).__name__) logger.log_error(validate_msg) validator_dict['check_result'] = 'fail' raise exceptions.VaildationFailure(validate_msg)
def test_get_uniform_comparator(self): self.assertEqual(utils.get_uniform_comparator("eq"), "equals") self.assertEqual(utils.get_uniform_comparator("=="), "equals") self.assertEqual(utils.get_uniform_comparator("lt"), "less_than") self.assertEqual(utils.get_uniform_comparator("le"), "less_than_or_equals") self.assertEqual(utils.get_uniform_comparator("gt"), "greater_than") self.assertEqual(utils.get_uniform_comparator("ge"), "greater_than_or_equals") self.assertEqual(utils.get_uniform_comparator("ne"), "not_equals") self.assertEqual(utils.get_uniform_comparator("str_eq"), "string_equals") self.assertEqual(utils.get_uniform_comparator("len_eq"), "length_equals") self.assertEqual(utils.get_uniform_comparator("count_eq"), "length_equals") self.assertEqual(utils.get_uniform_comparator("len_gt"), "length_greater_than") self.assertEqual(utils.get_uniform_comparator("count_gt"), "length_greater_than") self.assertEqual(utils.get_uniform_comparator("count_greater_than"), "length_greater_than") self.assertEqual(utils.get_uniform_comparator("len_ge"), "length_greater_than_or_equals") self.assertEqual(utils.get_uniform_comparator("count_ge"), "length_greater_than_or_equals") self.assertEqual(utils.get_uniform_comparator("count_greater_than_or_equals"), "length_greater_than_or_equals") self.assertEqual(utils.get_uniform_comparator("len_lt"), "length_less_than") self.assertEqual(utils.get_uniform_comparator("count_lt"), "length_less_than") self.assertEqual(utils.get_uniform_comparator("count_less_than"), "length_less_than") self.assertEqual(utils.get_uniform_comparator("len_le"), "length_less_than_or_equals") self.assertEqual(utils.get_uniform_comparator("count_le"), "length_less_than_or_equals") self.assertEqual(utils.get_uniform_comparator("count_less_than_or_equals"), "length_less_than_or_equals")