Example #1
0
 def test_children_have_their_errors_dicts_built(self):
     e1, e2 = (
         ValidationError("message 1", validator="foo", path=["bar", 0]),
         ValidationError("message 2", validator="quux", path=["bar", 0]),
     )
     tree = ErrorTree([e1, e2])
     self.assertEqual(tree["bar"][0].errors, {"foo": e1, "quux": e2})
Example #2
0
 def test_it_creates_a_child_tree_for_each_nested_path(self):
     errors = [
         ValidationError("a bar message", path=["bar"]),
         ValidationError("a bar -> 0 message", path=["bar", 0]),
     ]
     tree = ErrorTree(errors)
     self.assertIn(0, tree["bar"])
     self.assertNotIn(1, tree["bar"])
Example #3
0
 def setUp(self):
     self.error = ValidationError(
         message=self.message,
         validator="type",
         validator_value="string",
         instance=5,
         schema={"type": "string"},
     )
Example #4
0
    def test_unset_error(self):
        error = ValidationError("message")
        self.assertEqual(str(error), "message")

        kwargs = {
            "validator": "type",
            "validator_value": "string",
            "instance": 5,
            "schema": {
                "type": "string"
            }
        }
        # Just the message should show if any of the attributes are unset
        for attr in kwargs:
            k = dict(kwargs)
            del k[attr]
            error = ValidationError("message", **k)
            self.assertEqual(str(error), "message")
Example #5
0
    def test_if_its_in_the_tree_anyhow_it_does_not_raise_an_error(self):
        """
        If a validator is dumb (like :validator:`required` in draft 3) and
        refers to a path that isn't in the instance, the tree still properly
        returns a subtree for that path.

        """

        error = ValidationError(
            "a message",
            validator="foo",
            instance={},
            path=["foo"],
        )
        tree = ErrorTree([error])
        self.assertIsInstance(tree["foo"], ErrorTree)
Example #6
0
    def test_it_does_not_contain_subtrees_that_are_not_in_the_instance(self):
        error = ValidationError("a message", validator="foo", instance=[])
        tree = ErrorTree([error])

        with self.assertRaises(IndexError):
            tree[0]
Example #7
0
 def test_validators_that_failed_appear_in_errors_dict(self):
     error = ValidationError("a message", validator="foo")
     tree = ErrorTree([error])
     self.assertEqual(tree.errors, {"foo": error})
Example #8
0
 def test_it_does_not_contain_an_item_if_the_item_had_no_error(self):
     errors = [ValidationError("a message", path=["bar"])]
     tree = ErrorTree(errors)
     self.assertNotIn("foo", tree)
Example #9
0
 def test_it_contains_an_item_if_the_item_had_an_error(self):
     errors = [ValidationError("a message", path=["bar"])]
     tree = ErrorTree(errors)
     self.assertIn("bar", tree)