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.")
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.")
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
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.")
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.")
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.")
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.")
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.")
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.")
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.")
def test_this(): "The that function should create a DictRuleDsl that targets the root." element = this assert isinstance(element, DictRuleDsl) assert element.path == [] validator = ensure({"a": "b"}).respects(this.is_instance_of(dict)) validator.check()
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_unknown_path(): """Ensuring that the rule doesn't start if the path is unknown.""" obj = {} validator = ensure(obj).respects(that("x").exists()) with pytest.raises(CertumException) as error: validator.check() assert_error(error, "[x] => The path is missing.")
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.")
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").forsome(rule, keys=[0])) with pytest.raises(CertumException) as error: validator.check() assert_error(error, "[x] => The path is missing.")
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.")
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.")
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)
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)
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))
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()
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()
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))
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()
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()
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()
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()
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()
def test_using_list_fail(): """Using should return an error if an unknown strategy is provided inside a list.""" with pytest.raises(CertumException): ensure({}).using([3])