@@ -21,21 +21,15 @@ def test_valid(self):
21
21
22
22
def test_to_python_fail (self ):
23
23
field = SimpleArrayField (forms .IntegerField ())
24
- with self .assertRaises (exceptions .ValidationError ) as cm :
24
+ msg = "Item 1 in the array did not validate: Enter a whole number."
25
+ with self .assertRaisesMessage (exceptions .ValidationError , msg ):
25
26
field .clean ("a,b,9" )
26
- self .assertEqual (
27
- cm .exception .messages [0 ],
28
- "Item 1 in the array did not validate: Enter a whole number." ,
29
- )
30
27
31
28
def test_validate_fail (self ):
32
29
field = SimpleArrayField (forms .CharField (required = True ))
33
- with self .assertRaises (exceptions .ValidationError ) as cm :
30
+ msg = "Item 3 in the array did not validate: This field is required."
31
+ with self .assertRaisesMessage (exceptions .ValidationError , msg ):
34
32
field .clean ("a,b," )
35
- self .assertEqual (
36
- cm .exception .messages [0 ],
37
- "Item 3 in the array did not validate: This field is required." ,
38
- )
39
33
40
34
def test_validate_fail_base_field_error_params (self ):
41
35
field = SimpleArrayField (forms .CharField (max_length = 2 ))
@@ -68,12 +62,9 @@ def test_validate_fail_base_field_error_params(self):
68
62
69
63
def test_validators_fail (self ):
70
64
field = SimpleArrayField (forms .RegexField ("[a-e]{2}" ))
71
- with self .assertRaises (exceptions .ValidationError ) as cm :
65
+ msg = "Item 1 in the array did not validate: Enter a valid value."
66
+ with self .assertRaisesMessage (exceptions .ValidationError , msg ):
72
67
field .clean ("a,bc,de" )
73
- self .assertEqual (
74
- cm .exception .messages [0 ],
75
- "Item 1 in the array did not validate: Enter a valid value." ,
76
- )
77
68
78
69
def test_delimiter (self ):
79
70
field = SimpleArrayField (forms .CharField (), delimiter = "|" )
@@ -92,21 +83,15 @@ def test_prepare_value(self):
92
83
93
84
def test_max_length (self ):
94
85
field = SimpleArrayField (forms .CharField (), max_length = 2 )
95
- with self .assertRaises (exceptions .ValidationError ) as cm :
86
+ msg = "List contains 3 items, it should contain no more than 2."
87
+ with self .assertRaisesMessage (exceptions .ValidationError , msg ):
96
88
field .clean ("a,b,c" )
97
- self .assertEqual (
98
- cm .exception .messages [0 ],
99
- "List contains 3 items, it should contain no more than 2." ,
100
- )
101
89
102
90
def test_min_length (self ):
103
91
field = SimpleArrayField (forms .CharField (), min_length = 4 )
104
- with self .assertRaises (exceptions .ValidationError ) as cm :
92
+ msg = "List contains 3 items, it should contain no fewer than 4."
93
+ with self .assertRaisesMessage (exceptions .ValidationError , msg ):
105
94
field .clean ("a,b,c" )
106
- self .assertEqual (
107
- cm .exception .messages [0 ],
108
- "List contains 3 items, it should contain no fewer than 4." ,
109
- )
110
95
111
96
def test_min_length_singular (self ):
112
97
field = SimpleArrayField (forms .IntegerField (), min_length = 2 )
@@ -117,9 +102,9 @@ def test_min_length_singular(self):
117
102
118
103
def test_required (self ):
119
104
field = SimpleArrayField (forms .CharField (), required = True )
120
- with self .assertRaises (exceptions .ValidationError ) as cm :
105
+ msg = "This field is required."
106
+ with self .assertRaisesMessage (exceptions .ValidationError , msg ):
121
107
field .clean ("" )
122
- self .assertEqual (cm .exception .messages [0 ], "This field is required." )
123
108
124
109
def test_model_field_formfield (self ):
125
110
model_field = ArrayField (models .CharField (max_length = 27 ))
0 commit comments