@@ -21,21 +21,15 @@ def test_valid(self):
2121
2222 def test_to_python_fail (self ):
2323 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 ):
2526 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- )
3027
3128 def test_validate_fail (self ):
3229 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 ):
3432 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- )
3933
4034 def test_validate_fail_base_field_error_params (self ):
4135 field = SimpleArrayField (forms .CharField (max_length = 2 ))
@@ -68,12 +62,9 @@ def test_validate_fail_base_field_error_params(self):
6862
6963 def test_validators_fail (self ):
7064 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 ):
7267 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- )
7768
7869 def test_delimiter (self ):
7970 field = SimpleArrayField (forms .CharField (), delimiter = "|" )
@@ -92,21 +83,15 @@ def test_prepare_value(self):
9283
9384 def test_max_length (self ):
9485 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 ):
9688 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- )
10189
10290 def test_min_length (self ):
10391 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 ):
10594 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- )
11095
11196 def test_min_length_singular (self ):
11297 field = SimpleArrayField (forms .IntegerField (), min_length = 2 )
@@ -117,9 +102,9 @@ def test_min_length_singular(self):
117102
118103 def test_required (self ):
119104 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 ):
121107 field .clean ("" )
122- self .assertEqual (cm .exception .messages [0 ], "This field is required." )
123108
124109 def test_model_field_formfield (self ):
125110 model_field = ArrayField (models .CharField (max_length = 27 ))
0 commit comments