Ensure that a validation result to_dict is available.
(self)
| 2076 | assert error_dict["color"] == COLOR_MESSAGE |
| 2077 | |
| 2078 | def test_recursive_validation(self): |
| 2079 | """Ensure that a validation result to_dict is available.""" |
| 2080 | |
| 2081 | class Author(EmbeddedDocument): |
| 2082 | name = StringField(required=True) |
| 2083 | |
| 2084 | class Comment(EmbeddedDocument): |
| 2085 | author = EmbeddedDocumentField(Author, required=True) |
| 2086 | content = StringField(required=True) |
| 2087 | |
| 2088 | class Post(Document): |
| 2089 | title = StringField(required=True) |
| 2090 | comments = ListField(EmbeddedDocumentField(Comment)) |
| 2091 | |
| 2092 | bob = Author(name="Bob") |
| 2093 | post = Post(title="hello world") |
| 2094 | post.comments.append(Comment(content="hello", author=bob)) |
| 2095 | post.comments.append(Comment(author=bob)) |
| 2096 | |
| 2097 | with pytest.raises(ValidationError): |
| 2098 | post.validate() |
| 2099 | try: |
| 2100 | post.validate() |
| 2101 | except ValidationError as error: |
| 2102 | # ValidationError.errors property |
| 2103 | assert hasattr(error, "errors") |
| 2104 | assert isinstance(error.errors, dict) |
| 2105 | assert "comments" in error.errors |
| 2106 | assert 1 in error.errors["comments"] |
| 2107 | assert isinstance(error.errors["comments"][1]["content"], ValidationError) |
| 2108 | |
| 2109 | # ValidationError.schema property |
| 2110 | error_dict = error.to_dict() |
| 2111 | assert isinstance(error_dict, dict) |
| 2112 | assert "comments" in error_dict |
| 2113 | assert 1 in error_dict["comments"] |
| 2114 | assert "content" in error_dict["comments"][1] |
| 2115 | assert error_dict["comments"][1]["content"] == "Field is required" |
| 2116 | |
| 2117 | post.comments[1].content = "here we go" |
| 2118 | post.validate() |
| 2119 | |
| 2120 | def test_tuples_as_tuples(self): |
| 2121 | """Ensure that tuples remain tuples when they are inside |