Fixed #18213 -- Allowed empty fixtures (emit a warning rather than raising an exception).

This commit is contained in:
Przemek Lewandowski 2013-02-23 20:34:59 +01:00 committed by Tim Graham
parent 70c080fcdb
commit 382c53d7d8
3 changed files with 36 additions and 18 deletions

View File

@ -3,6 +3,7 @@ from __future__ import unicode_literals
import glob import glob
import gzip import gzip
import os import os
import warnings
import zipfile import zipfile
from optparse import make_option from optparse import make_option
import warnings import warnings
@ -156,12 +157,13 @@ class Command(BaseCommand):
finally: finally:
fixture.close() fixture.close()
# If the fixture we loaded contains 0 objects, assume that an # Warn if the fixture we loaded contains 0 objects.
# error was encountered during fixture loading.
if objects_in_fixture == 0: if objects_in_fixture == 0:
raise CommandError( warnings.warn(
"No fixture data found for '%s'. " "No fixture data found for '%s'. (File format may be "
"(File format may be invalid.)" % fixture_name) "invalid.)" % fixture_name,
RuntimeWarning
)
def _find_fixtures(self, fixture_label): def _find_fixtures(self, fixture_label):
""" """

View File

@ -113,6 +113,9 @@ Miscellaneous
:attr:`~django.forms.formsets.BaseFormSet.can_delete` for instructions on how :attr:`~django.forms.formsets.BaseFormSet.can_delete` for instructions on how
to manually delete objects from deleted forms. to manually delete objects from deleted forms.
* Loading empty fixtures emits a ``RuntimeWarning`` rather than raising
:class:`~django.core.management.CommandError`.
Features deprecated in 1.7 Features deprecated in 1.7
========================== ==========================

View File

@ -181,55 +181,68 @@ class TestFixtures(TestCase):
""" """
Test for ticket #4371 -- Loading a fixture file with invalid data Test for ticket #4371 -- Loading a fixture file with invalid data
using explicit filename. using explicit filename.
Validate that error conditions are caught correctly Test for ticket #18213 -- warning conditions are caught correctly
""" """
with six.assertRaisesRegex(self, management.CommandError, with warnings.catch_warnings(record=True) as warning_list:
"No fixture data found for 'bad_fixture2'. \(File format may be invalid.\)"): warnings.simplefilter("always")
management.call_command( management.call_command(
'loaddata', 'loaddata',
'bad_fixture2.xml', 'bad_fixture2.xml',
verbosity=0, verbosity=0,
) )
warning = warning_list.pop()
self.assertEqual(warning.category, RuntimeWarning)
self.assertEqual(str(warning.message), "No fixture data found for 'bad_fixture2'. (File format may be invalid.)")
def test_invalid_data_no_ext(self): def test_invalid_data_no_ext(self):
""" """
Test for ticket #4371 -- Loading a fixture file with invalid data Test for ticket #4371 -- Loading a fixture file with invalid data
without file extension. without file extension.
Validate that error conditions are caught correctly Test for ticket #18213 -- warning conditions are caught correctly
""" """
with six.assertRaisesRegex(self, management.CommandError, with warnings.catch_warnings(record=True) as warning_list:
"No fixture data found for 'bad_fixture2'. \(File format may be invalid.\)"): warnings.simplefilter("always")
management.call_command( management.call_command(
'loaddata', 'loaddata',
'bad_fixture2', 'bad_fixture2',
verbosity=0, verbosity=0,
) )
warning = warning_list.pop()
self.assertEqual(warning.category, RuntimeWarning)
self.assertEqual(str(warning.message), "No fixture data found for 'bad_fixture2'. (File format may be invalid.)")
def test_empty(self): def test_empty(self):
""" """
Test for ticket #4371 -- Loading a fixture file with no data returns an error. Test for ticket #18213 -- Loading a fixture file with no data output a warning.
Validate that error conditions are caught correctly Previously empty fixture raises an error exception, see ticket #4371.
""" """
with six.assertRaisesRegex(self, management.CommandError, with warnings.catch_warnings(record=True) as warning_list:
"No fixture data found for 'empty'. \(File format may be invalid.\)"): warnings.simplefilter("always")
management.call_command( management.call_command(
'loaddata', 'loaddata',
'empty', 'empty',
verbosity=0, verbosity=0,
) )
warning = warning_list.pop()
self.assertEqual(warning.category, RuntimeWarning)
self.assertEqual(str(warning.message), "No fixture data found for 'empty'. (File format may be invalid.)")
def test_error_message(self): def test_error_message(self):
""" """
(Regression for #9011 - error message is correct) Regression for #9011 - error message is correct.
Change from error to warning for ticket #18213.
""" """
with six.assertRaisesRegex(self, management.CommandError, with warnings.catch_warnings(record=True) as warning_list:
"^No fixture data found for 'bad_fixture2'. \(File format may be invalid.\)$"): warnings.simplefilter("always")
management.call_command( management.call_command(
'loaddata', 'loaddata',
'bad_fixture2', 'bad_fixture2',
'animal', 'animal',
verbosity=0, verbosity=0,
) )
warning = warning_list.pop()
self.assertEqual(warning.category, RuntimeWarning)
self.assertEqual(str(warning.message), "No fixture data found for 'bad_fixture2'. (File format may be invalid.)")
def test_pg_sequence_resetting_checks(self): def test_pg_sequence_resetting_checks(self):
""" """