コード例 #1
0
def test_one():
    """Ensure that certum accumulate multiple type of errors together."""
    obj = {
        "a": 2,
        "b": {
            "c": [],
        },
        "d": "e",
    }

    validator = (ensure(obj).respects(
        that("a").equals(2),
        that("b").is_instance_of(list),
        that("b", "c").is_instance_of(int),
        that("x", "a").equals(2),
        that("x", "b").equals(4),
    ).using(AlphabeticalSorting()))

    with pytest.raises(CertumException) as error:
        validator.check()

    errors = [
        "[b] => The value is instance of dict, expected list.",
        "[b -> c] => The value is instance of list, expected int.",
        "[x] => The path is missing.",
    ]
    assert_error(error, "\n".join(errors))
コード例 #2
0
def test_respects():
    """Respects should add more rule to the pool."""
    validator = ensure({})
    validator = validator.respects([that("x").equals(2), that("y").is_empty()])
    assert len(validator.rules) == 2
    validator = validator.respects([that("z").equals(4)])
    assert len(validator.rules) == 3
コード例 #3
0
ファイル: test_foreach.py プロジェクト: dylandoamaral/certum
def test_unknown_path():
    """Ensuring that the rule doesn't start if the path is unknown."""
    obj = {}
    rule = that("y").equals(2)
    validator = ensure(obj).respects(that("x").foreach(rule))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The path is missing.")
コード例 #4
0
def test_has_unique_elements_list_failure():
    """Ensuring that foreach is applied correctly with a failure for a dict."""
    obj = {"x": [2, 2, 3]}
    validator = ensure(obj).respects(that("x").has_unique_elements())
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The row 0 and 1 are the same.")
コード例 #5
0
ファイル: test_is_empty.py プロジェクト: dylandoamaral/certum
def test_is_empty_failure():
    """Ensuring that x is not empty."""
    obj = {"x": 2}
    validator = ensure(obj).respects(that("x").is_empty())
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The target is not empty.")
コード例 #6
0
def test_is_instance_of_failure():
    """Ensuring that x is empty."""
    obj = {"x": 2}
    validator = ensure(obj).respects(that("x").is_instance_of(str))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The value is instance of int, expected str.")
コード例 #7
0
def test_exists_nested_failure():
    """Ensuring that exists error should return an assertion error."""
    obj = {}
    validator = ensure(obj).respects(that("x", "b").exists())
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The path is missing.")
コード例 #8
0
def test_is_one_of_failure():
    """Ensuring that x is equal to 2 throw an assertion error."""
    obj = {"x": 4}
    validator = ensure(obj).respects(that("x").is_one_of([1, 2, 3]))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The value is 4, expected one of 1, 2, 3.")
コード例 #9
0
def test_apply_fail():
    """Ensuring that you can apply a function correctly."""
    obj = {"x": 2}
    validator = ensure(obj).respects(that("x").apply(fn))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => 2 != 1.")
コード例 #10
0
def test_equals_failure():
    """Ensuring that x is equal to 2 throw an assertion error."""
    obj = {"x": 3}
    validator = ensure(obj).respects(that("x").equals(2))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The value is 3, expected 2.")
コード例 #11
0
def test_has_length_of_dict_failure():
    """Ensuring that x is equal a dict of size 2."""
    obj = {"x": {"x": 2, "y": 3, "z": 4}}
    validator = ensure(obj).respects(that("x").has_length_of(2))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The length is 3, expected 2.")
コード例 #12
0
def test_has_length_of_list_failure():
    """Ensuring that x is equal a list of size 2 throw an assertion error."""
    obj = {"x": [2, 3, 4]}
    validator = ensure(obj).respects(that("x").has_length_of(2))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The length is 3, expected 2.")
コード例 #13
0
ファイル: test_foreach.py プロジェクト: dylandoamaral/certum
def test_type_failure():
    """Ensuring that foreach is not working if not a dict or a list."""
    obj = {"x": 2}
    rule = this.equals(2)
    validator = ensure(obj).respects(that("x").foreach(rule))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x] => The path should be instance of dict or list.")
コード例 #14
0
ファイル: test_foreach.py プロジェクト: dylandoamaral/certum
def test_foreach_list_failure():
    """Ensuring that foreach is applied correctly with a failure for a dict."""
    obj = {"x": [2, 2, 3]}
    rule = this.equals(2)
    validator = ensure(obj).respects(that("x").foreach(rule))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x -> 2] => The value is 3, expected 2.")
コード例 #15
0
def test_forsome_dict_failure():
    """Ensuring that forsome is applied correctly with a failure for a dict."""
    obj = {"x": {"a": 2, "b": 2, "c": 3}}
    rule = this.equals(2)
    validator = ensure(obj).respects(
        that("x").forsome(rule, keys=["a", "b", "c"]))
    with pytest.raises(CertumException) as error:
        validator.check()
    assert_error(error, "[x -> c] => The value is 3, expected 2.")
コード例 #16
0
def test_two():
    """Ensure that certum accumulate multiple type of errors together + strategies."""
    my_obj = {"name": 2, "entities": [1, 3, 3], "nested": {"value": 2}}

    validator = (ensure(my_obj).respects(
        that("name").is_instance_of(str),
        that("name").equals("Hello"),
        that("entities").foreach(this.equals(1)),
        that("nested", "value").equals(4),
    ).using(GroupedPrinting(), AlphabeticalSorting()))

    with pytest.raises(CertumException) as error:
        validator.check()

    errors = [
        "entities -> 1   => The value is 3, expected 1.",
        "entities -> 2   => The value is 3, expected 1.",
        "name            => The value is 2, expected Hello.",
        "                   The value is instance of int, expected str.",
        "nested -> value => The value is 2, expected 4.",
    ]
    assert_error(error, "\n".join(errors))
コード例 #17
0
def test_forsome_hybrid_success_and_failure():
    """Ensuring that forsome is applied correctly for a list."""
    obj = {"x": {"a": 2, "b": 3, "c": 3}}
    rule = this.equals(2)
    validator = (ensure(obj).respects(
        that("x").forsome(rule, keys=["a", "b",
                                      "d"])).using(AlphabeticalSorting()))
    with pytest.raises(CertumException) as error:
        validator.check()
    errors = [
        "[x -> b] => The value is 3, expected 2.",
        "[x -> d] => The path is missing.",
    ]
    assert_error(error, "\n".join(errors))
コード例 #18
0
def test_has_unique_elements_other_success():
    """Ensuring that foreach is applied correctly for any other value."""
    obj = {"x": 2}
    validator = ensure(obj).respects(that("x").has_unique_elements())
    validator.check()
コード例 #19
0
ファイル: test_foreach.py プロジェクト: dylandoamaral/certum
def test_foreach_list_path_success():
    """Ensuring that foreach is applied correctly for a list."""
    obj = {"x": [{"y": 2}, {"y": 2}, {"y": 2}]}
    rule = that("y").equals(2)
    validator = ensure(obj).respects(that("x").foreach(rule))
    validator.check()
コード例 #20
0
def test_equals_success():
    """Ensuring that x is equal to 2."""
    obj = {"x": 2}
    validator = ensure(obj).respects(that("x").equals(2))
    validator.check()
コード例 #21
0
def test_keys_no_list_failure():
    """Ensuring that the rule need a 'keys' parameter of type list to work."""
    obj = {}
    rule = that("y").equals(2)
    with pytest.raises(CertumException):
        ensure(obj).respects(that("x").forsome(rule, keys=2))
コード例 #22
0
def test_forsome_dict_success():
    """Ensuring that forsome is applied correctly for a dict."""
    obj = {"x": {"a": 2, "b": 2, "c": 3}}
    rule = this.equals(2)
    validator = ensure(obj).respects(that("x").forsome(rule, keys=["a", "b"]))
    validator.check()
コード例 #23
0
def test_forsome_list_success():
    """Ensuring that forsome is applied correctly for a list."""
    obj = {"x": [2, 2, 3]}
    rule = this.equals(2)
    validator = ensure(obj).respects(that("x").forsome(rule, keys=[0, 1]))
    validator.check()
コード例 #24
0
def test_apply_success():
    """Ensuring that you can apply a function correctly."""
    obj = {"x": 1}
    validator = ensure(obj).respects(that("x").apply(fn))
    validator.check()
コード例 #25
0
def test_is_one_of_success():
    """Ensuring that x is equal to 2."""
    obj = {"x": 2}
    validator = ensure(obj).respects(that("x").is_one_of([1, 2, 3]))
    validator.check()
コード例 #26
0
def test_is_instance_of_success():
    """Ensuring that x is not empty."""
    obj = {"x": 2}
    validator = ensure(obj).respects(that("x").is_instance_of(int))
    validator.check()
コード例 #27
0
ファイル: test_is_empty.py プロジェクト: dylandoamaral/certum
def test_is_empty_success(empty):
    """Ensuring that x is empty."""
    obj = {"x": empty}
    validator = ensure(obj).respects(that("x").is_empty())
    validator.check()
コード例 #28
0
def test_has_unique_elements_list_success():
    """Ensuring that foreach is applied correctly for a list."""
    obj = {"x": [2, 3, 4]}
    validator = ensure(obj).respects(that("x").has_unique_elements())
    validator.check()
コード例 #29
0
ファイル: test_foreach.py プロジェクト: dylandoamaral/certum
def test_foreach_dict_success():
    """Ensuring that foreach is applied correctly for a dict."""
    obj = {"x": {"a": 2, "b": 2, "c": 2}}
    rule = this.equals(2)
    validator = ensure(obj).respects(that("x").foreach(rule))
    validator.check()
コード例 #30
0
def test_has_unique_elements_dict_success():
    """Ensuring that foreach is applied correctly for a dict."""
    obj = {"x": {"a": 2, "b": 3, "c": 4}}
    validator = ensure(obj).respects(that("x").has_unique_elements())
    validator.check()