forked from marshmallow-code/marshmallow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_exceptions.py
42 lines (32 loc) · 1.37 KB
/
test_exceptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import pytest
from marshmallow.exceptions import ValidationError
class TestValidationError:
def test_stores_message_in_list(self):
err = ValidationError("foo")
assert err.messages == ["foo"]
def test_can_pass_list_of_messages(self):
err = ValidationError(["foo", "bar"])
assert err.messages == ["foo", "bar"]
def test_stores_dictionaries(self):
messages = {"user": {"email": ["email is invalid"]}}
err = ValidationError(messages)
assert err.messages == messages
def test_can_store_field_name(self):
err = ValidationError("invalid email", field_name="email")
assert err.field_name == "email"
def test_str(self):
err = ValidationError("invalid email")
assert str(err) == "invalid email"
err2 = ValidationError("invalid email", "email")
assert str(err2) == "invalid email"
def test_stores_dictionaries_in_messages_dict(self):
messages = {"user": {"email": ["email is invalid"]}}
err = ValidationError(messages)
assert err.messages_dict == messages
def test_messages_dict_type_error_on_badval(self):
err = ValidationError("foo")
with pytest.raises(TypeError) as excinfo:
err.messages_dict
assert "cannot access 'messages_dict' when 'messages' is of type list" in str(
excinfo.value
)