Exemplo n.º 1
0
    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
Exemplo n.º 2
0
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"
Exemplo n.º 3
0
 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
Exemplo n.º 4
0
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
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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)
Exemplo n.º 8
0
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)
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
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
Exemplo n.º 11
0
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'
Exemplo n.º 12
0
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
Exemplo n.º 13
0
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
Exemplo n.º 14
0
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
Exemplo n.º 15
0
    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]
Exemplo n.º 16
0
def test_report_length_without_errors():
    r = Report()
    assert len(r) == 0
Exemplo n.º 17
0
def test_report_has_errors():
    r = Report(soft=True)
    r.add("some error message")
    assert r.has_errors()
Exemplo n.º 18
0
def test_create_instance_with_default_params():
    r = Report()
    assert r.soft is True
    assert r.errors == []
Exemplo n.º 19
0
def test_report_without_errors():
    r = Report(soft=True)
    assert not r.has_errors()
Exemplo n.º 20
0
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"]
Exemplo n.º 21
0
def test_add_or_rise_error_to_report():
    r = Report(soft=False)
    with pytest.raises(KeyboardInterrupt):
        r.add_or_raise("test message", KeyboardInterrupt)
Exemplo n.º 22
0
def test_add_error_to_report():
    r = Report()
    assert r.add("test message") is True
    assert r.errors == ["test message"]
Exemplo n.º 23
0
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"
Exemplo n.º 24
0
def test_report_instance_string_with_custom_param():
    r = Report(soft=False)
    assert str(r) == ""
Exemplo n.º 25
0
def test_report_length_with_errors():
    r = Report()
    r.errors.extend(["some error message #1", "some error message #2"])
    assert len(r) == 2
Exemplo n.º 26
0
def test_not_equal_report(exp_message):
    r = Report()
    r.add("some error message")
    assert r != exp_message
Exemplo n.º 27
0
def test_equal_report(exp_message):
    r = Report()
    r.add("error")
    assert r == exp_message
Exemplo n.º 28
0
def test_create_instance_with_custom_params():
    r = Report(soft=False)
    assert r.soft is False
    assert r.errors == []
Exemplo n.º 29
0
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
Exemplo n.º 30
0
def test_report_instance_string():
    r = Report()
    assert str(r) == ""