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_grouped_two_group(): """A grouped strategy should group messages by path and separate paths.""" errors = [ Error(["x"], "My message"), Error(["x"], "My second message"), Error(["y"], "My third message very long message"), ] result = ( "x => My message\n" " My second message\n" "y => My third message very long message\n" ) assert GroupedPrinting().print(errors) == result
def test_grouped_padding(): """A grouped strategy should add padding for smaller path length.""" errors = [ Error(["x", "y"], "My message"), Error(["x", "y"], "My second message"), Error(["y"], "My third message very long message"), ] result = ( "x -> y => My message\n" " My second message\n" "y => My third message very long message\n" ) assert GroupedPrinting().print(errors) == result
def error(self, message: str) -> Error: """Format error message throws by the assertion. :param message: The cause of the error. :type message: str :return: The formatted message. :rtype: str """ path = [str(key) for key in self.path] return Error(path, message)
def check(self, dictionary: Dict[str, Any]) -> List[Error]: """Check if the rule is respected. :raises AssertionError: if the rule is not respected. :param dictionary: [description] :type dictionary: Dict[str, Any] """ for index, key in enumerate(self.path, 1): try: _target(self.path[:index], dictionary) except CertumException: error = Error(self.path[:index], "The path is missing.") return [error] return []
def generate_errors(number_of_errors: int, depth: int) -> List[Error]: """Generate a large number of random errors. :param number_of_errors: The number of errors to generate. :type number_of_errors: int :param depth: The path depth of the generated input, this is a maximum so you can have a path with a lower depth. :type depth: int :return: [description] :rtype: List[Error] """ errors = [] for _ in range(0, number_of_errors): possibilities = string.ascii_letters + str(range(0, 20)) keys = [choice(possibilities) for i in range(0, randint(1, depth))] errors.append(Error(" -> ".join(keys), "")) return errors
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], ]
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_grouped_simple(): """A grouped strategy show a path followed by a message by default.""" errors = [Error(["x"], "My message")] result = """x => My message\n""" assert GroupedPrinting().print(errors) == result
import pytest from certum.error import Error from certum.strategy.filtering.thunk import ThunkFiltering @pytest.mark.parametrize( "errors, result", [ ([Error(["x"], "my message")], 1), ([Error(["x", "b"], "my message")], 1), ([Error(["x"], "my message"), Error(["x", "b"], "my message")], 1), ( [ Error(["x"], "my message"), Error(["x", "b"], "my message"), Error(["x", "b", "e"], "my message"), ], 1, ), ( [ Error(["x"], "my message"), Error(["x", "b"], "my message"), Error(["y"], "my message"), ], 2, ), ], )