Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #18213 -- Allowed empty fixtures (emit a warning rather than ra…

…ising an exception).
  • Loading branch information...
commit 382c53d7d88da70a3645c6db063995382f61dc45 1 parent 70c080f
Przemek Lewandowski authored February 23, 2013 timgraham committed July 29, 2013
12  django/core/management/commands/loaddata.py
@@ -3,6 +3,7 @@
3 3
 import glob
4 4
 import gzip
5 5
 import os
  6
+import warnings
6 7
 import zipfile
7 8
 from optparse import make_option
8 9
 import warnings
@@ -156,12 +157,13 @@ def load_label(self, fixture_label):
156 157
             finally:
157 158
                 fixture.close()
158 159
 
159  
-            # If the fixture we loaded contains 0 objects, assume that an
160  
-            # error was encountered during fixture loading.
  160
+            # Warn if the fixture we loaded contains 0 objects.
161 161
             if objects_in_fixture == 0:
162  
-                raise CommandError(
163  
-                        "No fixture data found for '%s'. "
164  
-                        "(File format may be invalid.)" % fixture_name)
  162
+                warnings.warn(
  163
+                    "No fixture data found for '%s'. (File format may be "
  164
+                    "invalid.)" % fixture_name,
  165
+                    RuntimeWarning
  166
+                )
165 167
 
166 168
     def _find_fixtures(self, fixture_label):
167 169
         """
3  docs/releases/1.7.txt
@@ -113,6 +113,9 @@ Miscellaneous
113 113
   :attr:`~django.forms.formsets.BaseFormSet.can_delete` for instructions on how
114 114
   to manually delete objects from deleted forms.
115 115
 
  116
+* Loading empty fixtures emits a ``RuntimeWarning`` rather than raising
  117
+  :class:`~django.core.management.CommandError`.
  118
+
116 119
 Features deprecated in 1.7
117 120
 ==========================
118 121
 
39  tests/fixtures_regress/tests.py
@@ -181,55 +181,68 @@ def test_invalid_data(self):
181 181
         """
182 182
         Test for ticket #4371 -- Loading a fixture file with invalid data
183 183
         using explicit filename.
184  
-        Validate that error conditions are caught correctly
  184
+        Test for ticket #18213 -- warning conditions are caught correctly
185 185
         """
186  
-        with six.assertRaisesRegex(self, management.CommandError,
187  
-                "No fixture data found for 'bad_fixture2'. \(File format may be invalid.\)"):
  186
+        with warnings.catch_warnings(record=True) as warning_list:
  187
+            warnings.simplefilter("always")
188 188
             management.call_command(
189 189
                 'loaddata',
190 190
                 'bad_fixture2.xml',
191 191
                 verbosity=0,
192 192
             )
  193
+            warning = warning_list.pop()
  194
+            self.assertEqual(warning.category, RuntimeWarning)
  195
+            self.assertEqual(str(warning.message), "No fixture data found for 'bad_fixture2'. (File format may be invalid.)")
193 196
 
194 197
     def test_invalid_data_no_ext(self):
195 198
         """
196 199
         Test for ticket #4371 -- Loading a fixture file with invalid data
197 200
         without file extension.
198  
-        Validate that error conditions are caught correctly
  201
+        Test for ticket #18213 -- warning conditions are caught correctly
199 202
         """
200  
-        with six.assertRaisesRegex(self, management.CommandError,
201  
-                "No fixture data found for 'bad_fixture2'. \(File format may be invalid.\)"):
  203
+        with warnings.catch_warnings(record=True) as warning_list:
  204
+            warnings.simplefilter("always")
202 205
             management.call_command(
203 206
                 'loaddata',
204 207
                 'bad_fixture2',
205 208
                 verbosity=0,
206 209
             )
  210
+            warning = warning_list.pop()
  211
+            self.assertEqual(warning.category, RuntimeWarning)
  212
+            self.assertEqual(str(warning.message), "No fixture data found for 'bad_fixture2'. (File format may be invalid.)")
207 213
 
208 214
     def test_empty(self):
209 215
         """
210  
-        Test for ticket #4371 -- Loading a fixture file with no data returns an error.
211  
-        Validate that error conditions are caught correctly
  216
+        Test for ticket #18213 -- Loading a fixture file with no data output a warning.
  217
+        Previously empty fixture raises an error exception, see ticket #4371.
212 218
         """
213  
-        with six.assertRaisesRegex(self, management.CommandError,
214  
-                "No fixture data found for 'empty'. \(File format may be invalid.\)"):
  219
+        with warnings.catch_warnings(record=True) as warning_list:
  220
+            warnings.simplefilter("always")
215 221
             management.call_command(
216 222
                 'loaddata',
217 223
                 'empty',
218 224
                 verbosity=0,
219 225
             )
  226
+            warning = warning_list.pop()
  227
+            self.assertEqual(warning.category, RuntimeWarning)
  228
+            self.assertEqual(str(warning.message), "No fixture data found for 'empty'. (File format may be invalid.)")
220 229
 
221 230
     def test_error_message(self):
222 231
         """
223  
-        (Regression for #9011 - error message is correct)
  232
+        Regression for #9011 - error message is correct.
  233
+        Change from error to warning for ticket #18213.
224 234
         """
225  
-        with six.assertRaisesRegex(self, management.CommandError,
226  
-                "^No fixture data found for 'bad_fixture2'. \(File format may be invalid.\)$"):
  235
+        with warnings.catch_warnings(record=True) as warning_list:
  236
+            warnings.simplefilter("always")
227 237
             management.call_command(
228 238
                 'loaddata',
229 239
                 'bad_fixture2',
230 240
                 'animal',
231 241
                 verbosity=0,
232 242
             )
  243
+            warning = warning_list.pop()
  244
+            self.assertEqual(warning.category, RuntimeWarning)
  245
+            self.assertEqual(str(warning.message), "No fixture data found for 'bad_fixture2'. (File format may be invalid.)")
233 246
 
234 247
     def test_pg_sequence_resetting_checks(self):
235 248
         """

0 notes on commit 382c53d

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