Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Clean up unique validation tests to not use bare excepts and use asse…

…rtRaises. Refs [16345].

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16348 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit da0c7cd7778ee99a31587276faee30e94a8255ae 1 parent dce2786
Andrew Godwin authored June 09, 2011

Showing 1 changed file with 12 additions and 36 deletions. Show diff stats Hide diff stats

  1. 48  tests/modeltests/validation/test_unique.py
48  tests/modeltests/validation/test_unique.py
@@ -83,12 +83,9 @@ def test_unique_for_date(self):
83 83
             slug="Django 1.0", subtitle="Finally", posted=datetime.date(2008, 9, 3))
84 84
 
85 85
         p = Post(title="Django 1.0 is released", posted=datetime.date(2008, 9, 3))
86  
-        try:
  86
+        with self.assertRaises(ValidationError) as cm:
87 87
             p.full_clean()
88  
-        except ValidationError, e:
89  
-            self.assertEqual(e.message_dict, {'title': [u'Title must be unique for Posted date.']})
90  
-        else:
91  
-            self.fail('unique_for_date checks should catch this.')
  88
+        self.assertEqual(cm.exception.message_dict, {'title': [u'Title must be unique for Posted date.']})
92 89
 
93 90
         # Should work without errors
94 91
         p = Post(title="Work on Django 1.1 begins", posted=datetime.date(2008, 9, 3))
@@ -99,28 +96,19 @@ def test_unique_for_date(self):
99 96
         p.full_clean()
100 97
 
101 98
         p = Post(slug="Django 1.0", posted=datetime.datetime(2008, 1, 1))
102  
-        try:
  99
+        with self.assertRaises(ValidationError) as cm:
103 100
             p.full_clean()
104  
-        except ValidationError, e:
105  
-            self.assertEqual(e.message_dict, {'slug': [u'Slug must be unique for Posted year.']})
106  
-        else:
107  
-            self.fail('unique_for_year checks should catch this.')
  101
+        self.assertEqual(cm.exception.message_dict, {'slug': [u'Slug must be unique for Posted year.']})
108 102
 
109 103
         p = Post(subtitle="Finally", posted=datetime.datetime(2008, 9, 30))
110  
-        try:
  104
+        with self.assertRaises(ValidationError) as cm:
111 105
             p.full_clean()
112  
-        except ValidationError, e:
113  
-            self.assertEqual(e.message_dict, {'subtitle': [u'Subtitle must be unique for Posted month.']})
114  
-        else:
115  
-            self.fail('unique_for_month checks should catch this.')
  106
+        self.assertEqual(cm.exception.message_dict, {'subtitle': [u'Subtitle must be unique for Posted month.']})
116 107
 
117 108
         p = Post(title="Django 1.0 is released")
118  
-        try:
  109
+        with self.assertRaises(ValidationError) as cm:
119 110
             p.full_clean()
120  
-        except ValidationError, e:
121  
-            self.assertEqual(e.message_dict, {'posted': [u'This field cannot be null.']})
122  
-        else:
123  
-            self.fail("Model validation shouldn't allow an absent value for a DateField without null=True.")
  111
+        self.assertEqual(cm.exception.message_dict, {'posted': [u'This field cannot be null.']})
124 112
 
125 113
     def test_unique_for_date_with_nullable_date(self):
126 114
         p1 = FlexibleDatePost.objects.create(title="Django 1.0 is released",
@@ -131,40 +119,28 @@ def test_unique_for_date_with_nullable_date(self):
131 119
             p.full_clean()
132 120
         except ValidationError, e:
133 121
             self.fail("unique_for_date checks shouldn't trigger when the associated DateField is None.")
134  
-        except:
135  
-            self.fail("unique_for_date checks shouldn't explode when the associated DateField is None.")
136 122
 
137 123
         p = FlexibleDatePost(slug="Django 1.0")
138 124
         try:
139 125
             p.full_clean()
140 126
         except ValidationError, e:
141 127
             self.fail("unique_for_year checks shouldn't trigger when the associated DateField is None.")
142  
-        except:
143  
-            self.fail("unique_for_year checks shouldn't explode when the associated DateField is None.")
144 128
 
145 129
         p = FlexibleDatePost(subtitle="Finally")
146 130
         try:
147 131
             p.full_clean()
148 132
         except ValidationError, e:
149 133
             self.fail("unique_for_month checks shouldn't trigger when the associated DateField is None.")
150  
-        except:
151  
-            self.fail("unique_for_month checks shouldn't explode when the associated DateField is None.")
152 134
 
153 135
     def test_unique_errors(self):
154 136
         m1 = UniqueErrorsModel.objects.create(name='Some Name', number=10)
155 137
         m = UniqueErrorsModel(name='Some Name', number=11)
156  
-        try:
  138
+        with self.assertRaises(ValidationError) as cm:
157 139
             m.full_clean()
158  
-        except ValidationError, e:
159  
-            self.assertEqual(e.message_dict, {'name': [u'Custom unique name message.']})
160  
-        except:
161  
-            self.fail('unique checks should catch this.')
  140
+        self.assertEqual(cm.exception.message_dict, {'name': [u'Custom unique name message.']})
162 141
 
163 142
         m = UniqueErrorsModel(name='Some Other Name', number=10)
164  
-        try:
  143
+        with self.assertRaises(ValidationError) as cm:
165 144
             m.full_clean()
166  
-        except ValidationError, e:
167  
-            self.assertEqual(e.message_dict, {'number': [u'Custom unique number message.']})
168  
-        except:
169  
-            self.fail('unique checks should catch this.')
  145
+        self.assertEqual(cm.exception.message_dict, {'number': [u'Custom unique number message.']})
170 146
             

0 notes on commit da0c7cd

Please sign in to comment.
Something went wrong with that request. Please try again.