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))
def test_using():
    """Using should setup correctly the strategies by chaining using."""
    validator = ensure({})
    assert isinstance(validator.sorting, NoSorting)
    assert isinstance(validator.filtering, NoFiltering)
    assert isinstance(validator.printing, SimplePrinting)
    validator = (validator.using(AlphabeticalSorting()).using(
        FirstFiltering()).using(GroupedPrinting()))
    assert isinstance(validator.sorting, AlphabeticalSorting)
    assert isinstance(validator.filtering, FirstFiltering)
    assert isinstance(validator.printing, GroupedPrinting)
Exemple #3
0
def test_alphabetical_two_depth():
    """An alphabetical strategy should sort the messages alphabetically and respects
    path ordering for multiple depth level."""
    errors = [
        Error(["x"], ""),
        Error(["a"], ""),
        Error(["x", "a"], ""),
        Error(["x", "b"], ""),
        Error(["a", "b"], ""),
    ]
    paths = [error.path for error in AlphabeticalSorting().sort(errors)]
    assert paths == [["a"], ["a", "b"], ["x"], ["x", "a"], ["x", "b"]]
def test_using_list():
    """Using should setup correctly the strategies with a list of strategies."""
    validator = ensure({})
    assert isinstance(validator.sorting, NoSorting)
    assert isinstance(validator.filtering, NoFiltering)
    assert isinstance(validator.printing, SimplePrinting)
    validator = validator.using(
        [AlphabeticalSorting(),
         FirstFiltering(),
         GroupedPrinting()])
    assert isinstance(validator.sorting, AlphabeticalSorting)
    assert isinstance(validator.filtering, FirstFiltering)
    assert isinstance(validator.printing, GroupedPrinting)
Exemple #5
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))
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))
Exemple #7
0
def test_alphabetical_number():
    """An alphabetical strategy don't respects cardinality."""
    errors = [
        Error(["x"], ""),
        Error(["a"], ""),
        Error(["x", 11], ""),
        Error(["x", 2], ""),
        Error(["x", 7], ""),
        Error(["x", 128], ""),
        Error(["a", 210], ""),
        Error(["a", 3], ""),
    ]
    paths = [error.path for error in AlphabeticalSorting().sort(errors)]
    assert paths == [
        ["a"],
        ["a", 3],
        ["a", 210],
        ["x"],
        ["x", 2],
        ["x", 7],
        ["x", 11],
        ["x", 128],
    ]
Exemple #8
0
def test_alphabetical_one_depth():
    """An alphabetical strategy should sort the messages alphabetically for a depth of
    one."""
    errors = [Error("x", ""), Error("a", "")]
    paths = [error.path for error in AlphabeticalSorting().sort(errors)]
    assert paths == ["a", "x"]
def test_on_fail():
    """On should return an error if the new dictionary is not a dict."""
    obj = "hello"
    with pytest.raises(CertumException):
        using(AlphabeticalSorting()).on(obj)
def test_on():
    """On should assign a new dictionary to the validator."""
    obj = {"a": "b"}
    validator = using(AlphabeticalSorting()).on(obj)
    assert validator.dictionary == obj
Exemple #11
0
from random import shuffle

import pytest

from certum.strategy.sorting.alphabetical import AlphabeticalSorting
from tests.utils import generate_errors

startegies = [AlphabeticalSorting(), AlphabeticalSorting()]


@pytest.mark.parametrize("strategy", startegies)
def test_same_order(strategy):
    """A sorting strategy should always sort paths the same way even if the starting
    order is different (Except the NoSorting)."""

    for i in range(0, 50):
        errors = generate_errors(50, 5)
        shuffled = errors.copy()
        shuffle(shuffled)
        assert strategy.sort(errors) == strategy.sort(shuffled)


@pytest.mark.parametrize("strategy", startegies)
def test_same_number(strategy):
    """A sorting strategy should always conserve the number of errors."""

    for i in range(0, 50):
        errors = generate_errors(50, 5)
        assert len(strategy.sort(errors)) == len(errors)