def validate(self, current_data: Any) -> Report: report = Report(soft=True) for exp_data in self.expected_data: checker = Validator(expected_data=exp_data, report=report) checker.validate(current_data) if report.has_errors(): message = format_error_message(self, current_data) report.errors = ["Not valid data: %s" % message] return report
def test_merge_reports(): r1 = Report() r1.add("some error message #1") r2 = Report(soft=False) r2.add("some error message #2") assert r1.merge(r2) is True assert r1.errors == ["some error message #1", "some error message #2"] assert str(r1) == "some error message #1\nsome error message #2"
def validate(self, data: Any) -> Any: log.debug( "Checker settings: ignore_extra_keys=%s, soft=%s" % (self.ignore_extra_keys, self.soft) ) report = Report(self.soft) checker = Validator( expected_data=self.expected_data, report=report, ignore_extra_keys=self.ignore_extra_keys, ) checker.validate(data) if report.has_errors(): raise CheckerError(report) return data
def test_validator_positive_message(validator_data, current_data, expected_result): soft = True validator = Validator(validator_data, report=Report(soft), ignore_extra_keys=False) assert validator.validate(current_data) == expected_result
def test_soft_function_checker_with_not_valid_data(data): soft_report = Report(soft=True) c = FunctionChecker(foo, report=soft_report) result = c.validate(data) assert result == soft_report assert result.has_errors() assert str(result) == "function error: foo with data %s (int)" % data
def test_type_checker_positive(type_data, soft, current_data, expected_result): report = Report(soft=soft) type_checker = TypeChecker(expected_data=type_data, report=report) result = type_checker.validate(current_data) assert isinstance(result, Report) assert result == report assert result == expected_result
def test_list_checker_negative(list_data, current_data, ex_exception): soft = False list_checker = ListChecker( expected_data=list_data, report=Report(soft=soft) ) with pytest.raises(ex_exception): list_checker.validate(current_data)
def test_validator_miss_key(ex_data, cu_data, ex_exception): soft = False checker = DictChecker(ex_data, report=Report(soft), ignore_extra_keys=False) with pytest.raises(ex_exception): checker.validate(cu_data)
def test_dict_checker_assert(dict_data, current_data): soft = False checker = DictChecker(dict_data, report=Report(soft), ignore_extra_keys=False) with pytest.raises(DictCheckerError): checker.validate(current_data)
def test_function_checker_instance_with_custom_params(): soft_report = Report(soft=False) c = FunctionChecker(foo, report=soft_report, ignore_extra_keys=True) assert c.expected_data == foo assert c.report == soft_report assert c.ignore_extra_keys is True assert c.soft is False assert c.exception == FunctionCheckerError
def test_validator_some_dicts(): soft = False checker = Validator( expected_data=Or({"key1": int}, {"key2": str}), report=Report(soft), ignore_extra_keys=False, ) result = checker.validate({"key2": 12}) assert result == 'From key="key2": \n\tcurrent value 12 (int) is not str'
def test_soft_function_checker_with_invalid_data(data): exp_message = ("function error: foo with data '>' not supported " "between instances of 'int' and '%s'" % type(data).__name__) soft_report = Report(soft=True) c = FunctionChecker(foo, report=soft_report) result = c.validate(data) assert result == soft_report assert result.has_errors() assert str(result) == exp_message
def test_type_checker_instance_with_custom_param(): schema = [int] soft_report = Report(soft=False) c = TypeChecker(schema, report=soft_report, ignore_extra_keys=True) assert c.expected_data == schema assert c.report == soft_report assert c.ignore_extra_keys is True assert c.soft is False assert c.exception == TypeCheckerError
def test_validator_instance_with_default_param(): schema = [int] soft_report = Report(soft=True) c = Validator(schema, report=soft_report) assert c.expected_data == schema assert c.report == soft_report assert c.ignore_extra_keys is False assert c.soft is True assert c.exception == CheckerError
def validate(self, current_data: Any) -> Report: expected = list( filtered_by_type(self.expected_data, type(current_data))) if not expected and self.expected_data: report = Report(soft=True) message = format_error_message(self, current_data) report.add("Not valid data: %s" % message) return report results = {} for exp_data in expected: report = Report(soft=True) checker = Validator(expected_data=exp_data, report=report) checker.validate(current_data) if not report.has_errors(): return report results[len(report)] = report min_error = min(list(results.keys())) return results[min_error]
def test_report_length_without_errors(): r = Report() assert len(r) == 0
def test_report_has_errors(): r = Report(soft=True) r.add("some error message") assert r.has_errors()
def test_create_instance_with_default_params(): r = Report() assert r.soft is True assert r.errors == []
def test_report_without_errors(): r = Report(soft=True) assert not r.has_errors()
def test_soft_add_or_rise_error_to_report(): r = Report(soft=True) assert r.add_or_raise("test message", Exception) is True assert r.errors == ["test message"]
def test_add_or_rise_error_to_report(): r = Report(soft=False) with pytest.raises(KeyboardInterrupt): r.add_or_raise("test message", KeyboardInterrupt)
def test_add_error_to_report(): r = Report() assert r.add("test message") is True assert r.errors == ["test message"]
def test_report_instance_string_with_errors(): r = Report(soft=False) r.errors.extend(["error #1", "error #2"]) assert str(r) == "error #1\nerror #2"
def test_report_instance_string_with_custom_param(): r = Report(soft=False) assert str(r) == ""
def test_report_length_with_errors(): r = Report() r.errors.extend(["some error message #1", "some error message #2"]) assert len(r) == 2
def test_not_equal_report(exp_message): r = Report() r.add("some error message") assert r != exp_message
def test_equal_report(exp_message): r = Report() r.add("error") assert r == exp_message
def test_create_instance_with_custom_params(): r = Report(soft=False) assert r.soft is False assert r.errors == []
def test_contain_report(): errors = ["some error message #1", "some error message #2"] r = Report() r.errors.extend(errors) for error in errors: assert error in r
def test_report_instance_string(): r = Report() assert str(r) == ""