Ensure a ValidationError handles error to_dict correctly.
(self)
| 9 | |
| 10 | class TestValidatorError(MongoDBTestCase): |
| 11 | def test_to_dict(self): |
| 12 | """Ensure a ValidationError handles error to_dict correctly.""" |
| 13 | error = ValidationError("root") |
| 14 | assert error.to_dict() == {} |
| 15 | |
| 16 | # 1st level error schema |
| 17 | error.errors = {"1st": ValidationError("bad 1st")} |
| 18 | assert "1st" in error.to_dict() |
| 19 | assert error.to_dict()["1st"] == "bad 1st" |
| 20 | |
| 21 | # 2nd level error schema |
| 22 | error.errors = { |
| 23 | "1st": ValidationError( |
| 24 | "bad 1st", errors={"2nd": ValidationError("bad 2nd")} |
| 25 | ) |
| 26 | } |
| 27 | assert "1st" in error.to_dict() |
| 28 | assert isinstance(error.to_dict()["1st"], dict) |
| 29 | assert "2nd" in error.to_dict()["1st"] |
| 30 | assert error.to_dict()["1st"]["2nd"] == "bad 2nd" |
| 31 | |
| 32 | # moar levels |
| 33 | error.errors = { |
| 34 | "1st": ValidationError( |
| 35 | "bad 1st", |
| 36 | errors={ |
| 37 | "2nd": ValidationError( |
| 38 | "bad 2nd", |
| 39 | errors={ |
| 40 | "3rd": ValidationError( |
| 41 | "bad 3rd", errors={"4th": ValidationError("Inception")} |
| 42 | ) |
| 43 | }, |
| 44 | ) |
| 45 | }, |
| 46 | ) |
| 47 | } |
| 48 | assert "1st" in error.to_dict() |
| 49 | assert "2nd" in error.to_dict()["1st"] |
| 50 | assert "3rd" in error.to_dict()["1st"]["2nd"] |
| 51 | assert "4th" in error.to_dict()["1st"]["2nd"]["3rd"] |
| 52 | assert error.to_dict()["1st"]["2nd"]["3rd"]["4th"] == "Inception" |
| 53 | |
| 54 | assert error.message == "root(2nd.3rd.4th.Inception: ['1st'])" |
| 55 | |
| 56 | def test_model_validation(self): |
| 57 | class User(Document): |
nothing calls this directly
no test coverage detected